How to unit test?

unit test

1. [forced] a good unit tests must comply with the principle of AIR.

Description: When running the unit tests online, feels like air (AIR) as does not exist, but in the quality assurance testing, but it is very critical.

On a good unit test macro level, with automated, independence, characteristics can be repeated.

⚫ A: Automatic (Automation) ⚫ I: Independent (independent) ⚫ R: Repeatable (repeat)

2. [force] Unit testing is fully automatic execution and non-interactive. Test is usually performed on a regular basis, the implementation process must be fully automated meaningful. The output test requires manual inspection is not a good unit test. System.out allowed to use the test unit to verify the human flesh, must assert to verify.

3. [forced] to maintain the independence of the unit tests. To ensure stable and reliable unit testing and easily maintained between unit test must not call each other, we can not rely on the order of execution. Anti Example: performing method2 need to rely on the method1, method2 as input the result of execution.

[4] unit test force is performed repeatedly, not affected by the external environment. Description: Unit testing will usually be placed in continuous integration, every time when the code check in unit tests will be performed. If a single measurement of the external environment (network services, middleware, etc.) that depend, not easily lead to a continuous integration mechanism. Positive Example: In order to influence from the external environment, requires the designer to put the code into the SUT dependency injection, a local injection (memory) in the test frame with DI this spring Mock implemented or realized.

[5] for the unit test force, a sufficiently small particle size to ensure that the test to help pinpoint the problem. Measuring a particle size of at most a single class level, a method is generally level. Description: Only small size test to locate the error in the error location as soon as possible. Single measurement is not responsible for cross-checking interaction logic class or cross-system, that is the field of integration testing.

[6] Forced core business, core applications, the core module incremental code ensures unit test. Description: New replenish the code unit testing, if the new code is affecting the original unit testing, please amendments.

7. [mandatory] unit test code must be written in the project directory as follows: src / test / java, not allowed to write the code in the business directory. Description: This directory is skipped when compiling source code, and the default scan unit test framework for this directory.

[8] The basic object unit recommended test: statement coverage of 70%; statement coverage and branch coverage of the core module to be 100% Note: the DAO in the application layer is mentioned a layered construction of the statute, Manager layer, a high-Service reusable, unit testing should be performed. Java Development Manual 29/44

9. [Recommended] to write unit test code compliance BCDE principle, in order to ensure the delivery of quality test module.

⚫ B: Border, boundary value tests, including cyclic boundary, special values, specific time point, the data sequence and the like.

⚫ C: Correct, correct input, and the results expected.

⚫ D: Design, combined with the design documents to write unit tests.

⚫ E: Error, forced error input (eg: illegal data, exception process, allowing traffic outside the like), and the expected results.

10. [Recommended] for database-related query, update, delete, etc., we can not assume that data in the database is there, or direct operation of the database is inserted into the data, use the program insert mode or import data to prepare data. Counter-example: unit testing delete a row of data in the database, the first increase in a row as deleted manually direct target, but this line of business does not meet the new data insertion rules, resulting in abnormal test results.

11. [Recommended] and database unit tests, you can set up automatic rollback mechanism, does not cause dirty data to the database. Or the test data generating unit have a clear identification of the prefix and suffix. Positive examples: the internal unit testing intelligent division of the enterprise, the use of the prefix ENTERPRISE_INTELLIGENCE _UNIT_TEST_ unit tests to identify the relevant code.

[12] For recommended to make the necessary code is unpredictable reconstituted in an appropriate timing, so the code becomes measurable avoid testing requirements in order to achieve non-standard writing test code.

[13] In the recommended design review stage, testers and developers need to test range determination unit together, the test unit is preferably covered all test cases.

14. [Recommended] unit testing as a means of quality assurance, unit testing is completed before the project mentioned test is not recommended supplementary unit test project after release.

Reference [15] In order to more easily unit testing, service code should avoid:

Excessive ⚫ constructor do. There are too many global variables and static methods ⚫. ⚫ there are too many external dependencies. ⚫ there is too much of a conditional statement. Description: Multilayer recommend the use of conditional statements Wei statement, strategy pattern, state pattern, etc. reconstruction. 16. [reference] there is not a misunderstanding of unit testing: ⚫ that test students to do. This paper is the development of manuals, all the contents of this article are strongly associated with the development of the students. ⚫ unit test code is unnecessary. Test system's overall function of each unit cell is normal or not strongly correlated. ⚫ unit test code does not require maintenance. After a year or so, then the cell is discarded almost test state. ⚫ unit testing and fault line is not dialectical relationship. Good unit test line maximum to avoid failure.

Guess you like

Origin www.cnblogs.com/zhulimin/p/11986067.html