Unit test the AIR principles
Good unit tests must follow the AIR principles: Automatic, Independent, Repeatable.
Automatic
Unit tests should be fully automated and non-interactive. Test cases are usually executed regularly, and execution must be fully automated to make sense. A test whose output needs to be checked manually is not a good unit test. Do not use System.out for human validation in unit tests, you must use assert.
Independent people are Independent.
Keep unit tests independent. To ensure that unit tests are stable, reliable and easy to maintain, unit test cases should never call each other or depend on the order in which they are executed.
Repeatable(Repeatable)
Unit tests are repeatable and cannot be influenced by external circumstances. Because unit tests are usually put into continuous integration, they are executed every time code is checked in. If the single test is dependent on the external environment (network, service, middleware, etc.), continuous integration mechanism may be unavailable.
To be context-free, you need to design your code to inject SUT dependencies and use a DI framework like Spring to inject a local (in-memory) implementation or Mock implementation during testing.
Unit test granularity
For unit tests, make sure the test granularity is small enough to help pinpoint problems. Single-test granularity is at most class level and generally method level. Only small test granularity can locate the error position as soon as possible. Mono-testing is not responsible for checking interaction logic across classes or systems, which is the domain of integration testing.
Unit test scope
Incremental code for core business, core application, and core modules ensures unit tests pass. The new code should supplement the unit test in time. If the new code affects the original unit test, please correct it in time.
Other specification
1, the unit test code must be written in the following project directory SRC /test/ Java, not in the business code directory.
2. Basic goal of unit test: statement coverage reaches 70%; The core modules should have 100% statement coverage and 100% branch coverage.
3. The DAO layer, Manager layer and reusable Service mentioned in the application layer of the engineering specification should all be unit tested.
4. Write unit test code in accordance with BCDE principles to ensure the delivery quality of the tested modules.
9. Write unit test code in accordance with BCDE principles to ensure the delivery quality of the tested modules.
-
Border: Border value test, including loop boundary, special value, special time point, data order, etc.;
-
Correct: Typing correctly and getting the desired result;
-
Design: Writing unit tests in conjunction with Design documents
-
Error: Force the input of Error information (such as invalid data, abnormal flow, and non-service allowed input) and obtain the expected result.
5, for the database related query, update, delete and other operations, can not assume the existence of data in the database, or directly operate the database to insert data, please use procedures to insert or import data to prepare data.
6, and database related unit tests, you can set the automatic rollback mechanism, do not cause dirty data to the database. Or have an explicit prefix or suffix identification for the data generated by the unit tests.
7. It is recommended to do necessary refactoring for the untestable code to make the code testable and avoid writing non-standard test code to meet the test requirements.
8. During the design review phase, developers and testers need to determine the scope of unit tests, preferably covering all test cases.
9. Unit test, as a means of quality assurance, is not recommended to supplement unit test cases after the project is released. It is recommended to complete unit test before the project is tested.
10. To facilitate unit testing, business code should avoid the following:
-
There is too much to do in the constructor
-
There are too many global variables and static methods
-
There are too many external dependencies
-
Too many conditional statements exist. Note: It is recommended to use guard statement, policy mode, state mode and other methods to reconstruct multi-layer conditional statements
11. Don’t misunderstand unit tests as follows:
- That’s what students do
- Unit test code is redundant. The overall function of the system is strongly related to the normal testing of each unit component
- Unit test code doesn’t need to be maintained, and after a year and a half, unit tests are almost obsolete
- There is no dialectical relationship between unit test and online failure. Good unit test can avoid online failure to the maximum extent