Recently, I need to introduce unit testing practice into the team. I specially bought the book "The Art of Unit Testing" to learn. This book is well written in the field of unit testing, very systematic and perfect. Students who hope to have a deeper understanding of unit testing can find it to read it.
This article briefly summarizes the basic concepts of UT. In the next improvement article, we will summarize how to use PowerMock and Mockito to create test stakes and mock objects in Java projects to avoid the problem of code dependence on external modules during testing.
What is unit testing
A unit test is a piece of code that calls a unit of work and checks whether the final result of that unit of work is consistent with the expected one.
The work unit can be a method, class or functional unit.
To enhance the maintainability of test code, we usually write test cases for functional units that are more visible to the user.
What are the characteristics of unit testing
- Can be run automatically
- Easy to implement
- Runs very fast
- The test results are stable
- Able to fully control the unit being tested and does not contain external dependencies
- Each test case is independent of each other and has no dependencies
The value of unit testing
- Helps discover code flaws
- Advance the time for defect discovery, developers can run test code frequently locally
- Make sure that existing functions are not affected when modifying or refactoring the code
- Make developers more confident in submitting code
Although unit testing increases the time required for demand development to a certain extent, unit testing greatly improves the quality and maintainability of the code, shortening the overall delivery cycle of the product.
When to write a unit test
In order to maximize the value of unit testing, TDD (test-driven development) needs to be introduced to write unit tests before the product code is written.
TDD process: First write a test that will fail, then write the product code, then debug the use case to ensure it passes, then refactor the code or create another test.
How to write unit tests
Use the unit test framework to write test code. It is recommended to use Junit or TestNG framework in Java projects.
A simple unit test code structure is as follows:
public class AppTest {
@Before
public void setUp() {
// Perform pre-run preparations
}
@After
public void tearDown() {
// Execute the destruction work after operation
}
@Test
public void unitOfWorkName_Scenario_ExpectedBehavior() {
// Preparation phase
// Execution phase
// Verification phase
}
}
Each test case needs to include a preparation phase, an execution phase and a final verification phase.
Forged objects
During the process of writing test code, we will find that the tested code usually interacts with some uncontrollable external dependencies or modules that have not yet been implemented. We usually use fake objects to avoid dependency problems.
Forged objects usually include Stub and Mock, the differences are as follows:
Stub: Test stakes or stubs, define the interface request and response in advance during testing, and serve as a forged object so that the tested code can block the dependencies and execute smoothly.
Mock: Mock objects, also fake objects, used when making assertions in test cases to verify the interaction between the code and other dependencies.
It is recommended to use PowerMock or JMockit framework for the creation of test piles and mock objects in Java projects.
How to organize and run unit tests
For easy management, the test code and product code are placed in different folders in the same project. The Java project product code managed using Maven tools is located in the src/main directory, and the unit test code is located in the src/test directory. In order to enhance the readability of the test code, each test case should be located in the same package as the work unit under test. The naming specification for each test case is: unitOfWorkName_Scenario_ExpectedBehavior, which needs to clearly describe the work unit under test, the test scenario and the expected behavior.
Any developer or tester can run unit test code very quickly locally.
Developers need to run unit tests locally before submitting the code, and they can only submit the code after all test cases are passed.
Unit tests need to be integrated into the CI platform. Each time the code is built, unit tests need to be automatically run. If any use cases cannot be passed, they need to be fixed as soon as possible.
Reference: "The Art of Unit Testing"