what is the real difference between ng test and ng e2e

前端 未结 1 475
庸人自扰
庸人自扰 2021-01-30 13:05

I am afraid someone close my question but I couldn\'t find a satisfying question (maybe because I am very limited in Angular 2+ world and I understood something wrong).

1条回答
  •  礼貌的吻别
    2021-01-30 13:36

    You are on the good road to understand all of it.

    • Ng test (Jasmine + Angular Testing Utilities tests launched through Karma):

    You are using the jasmine framework to write your tests and define them as suites and expect results that you can test, but the main thing is that you are actually using the karma launcher to execute tests directly on the browser. This is normaly configured in the angular app.

    This means there is one server running the tests and that's it. You test with your own values and check that your components are working correctly.

    The aim is to check for single components (unit test) or several modules / components (integration tests) that a single function / small workflow is working correctly as intended without side effects.

    • Ng E2E (Jasmine + Protractor) :

    Protractor is an end-to-end test framework for Angular and AngularJS applications. Protractor runs tests against your application running in a real browser, interacting with it as a user would.

    Here, the tests you have written will act as a user. This means there is your application running in your browser, and another program will run the tests against your application, simulating a user interaction.

    This is very important, because that means two things :

    1. Unit tests and Integration tests are using static, mock data to run the tests.
    2. Protractor tests are using real data, and doing the HTTP (or whatever you are using) calls to fetch the data and consume/test it.

    The aim with protractor is to validate a full operationnal workflow in your application. For example, I'll write my unit test for my login component, write an unit test for my login service, write an integration test for my whole module and whatever behavior I need to test that depends on several components / services. Once this is done, I'll write later a full end-to-end test that will validate my whole authentication process in my application.

    Bear in mind, there is a ratio that is important about testing which is very logical :

    • Unit tests should represent 70% of your tests.
    • Integration tests should represent 20% of your tests.
    • E2E tests should represent 10% of your tests.

    Why is that ? Because if you did unit test correctly a lot of your components, you won't need to test that again in your End-to-End tests.


    Conclusion :

    • They operate differently and their aim is to test different things (unit function / full workflow).
    • They are complementary, which means that if you only realize Unit / Integration tests, you will never have the guarantee that a workflow will work from A to Z; as well as if you only write E2E tests you will never be able to confirm that there are no side-effects in your workflow.

    N.B. : Be also aware of those points :

    • Jasmine, Karma, and Protractor can be customized at will, so you can make them output in an XML file that could be processed by a Jenkins job instead of a command line which is not practical.
    • Yes, you can write the same code for both and effectively test the same thing, but bear in mind that what you want is to be efficient and write maintanable test code. The ratios I talked about are very important.

    Hope this helps.

    0 讨论(0)
提交回复
热议问题