For some experienced developers feel that writing unit tests is very necessary, but also just feel, may not write too much, but some of the novice code is not well written to let them write unit tests, it is estimated that their hearts are quite broken, but the unit test is really necessary, and the quality of their code and coding capabilities are improved. One, what is unit testing.
Unit testing is to test the correctness of the program's smallest unit, the program unit is the smallest testable part of the application, a unit may be a single program, class, object, method, etc. Ii. What are the benefits of using unit testing?
1. Reduce bugs
by running unit tests you can directly test the correctness of each function, there are bugs can be directly identified and resolved, if you want to wait for the other functions to connect, to conduct a coherent test, the test is more troublesome, Also, bugs cannot be found and resolved earlier
2. Quickly locate a bug
If it is a feature of the Web project, we usually locate the bug may be the page input value, the background breakpoint, step by step need the bug location, if there is writing unit test, You can modify the data directly, run unit tests, and quickly have a limited
3. Improve code quality
If every part is perfect, it must be perfectly combined. Overall code quality is guaranteed
4. Reduce debug Time
When you don't know where the problem is, you may need to debug and run it, and if you write unit tests, you can run unit tests directly to locate the problem location. Iii. Some considerations for unit testing The test method must be decorated with the @test adornment test method, and the package that cannot have the parameter test code should be consistent with the code package structure being tested Each method of the test unit must be independently tested, and there can be no dependent test class between methods generally use test as the suffix of the class name testing methods generally use test as the prefix of the method name four, unit test commonly used annotations @Test The modification of a common method into a test method @BeforeClass executed once before all methods are executed (must be static) @AfterClass executed once after all the methods have been executed (must be static) @Before Executes before each method executes @After is executed after each method executes @Ignore the modified method is ignored by the test runner @RunWith modify the test runner (useful with spring) v. Unit Test use Example
1. Create a new MAVEN project to introduce the JUnit package
2. Create a new operation class with the subtraction method
3. Create a new unit test class
4. Select the test method you want to create
5. Unit Test class
6. Add @ignore to one of the methods, run the test
7. See the difference between @before and Beforeclass
8. When the unit test is written, try to test the scene completely, The division must be done to take into account the divisor is zero, the more comprehensive the test scenario, the higher the quality of the code.