When it comes to unit testing, almost everyone knows that it's done by the developer. But why do we have to do unit testing ?
Developers write unit tests almost as long as he writes the product code, so it is reasonable to take the unit test into consideration as a project plan. Although unit testing adds considerable development effort, it seems that the development time is prolonged, but in fact for a long-term continuous improvement and maintenance of the project, we can not ignore the cascade effect, from the overall perspective of the project.
Unit tests ensure that the most basic defects are identified and resolved early , so that the defect time that is used to resolve the test phase that is transferred to the later stage is actually shortened.
And if you ask the developers if they have a unit test, they usually say, yes, the unit tests have been done. If you ask the developer, the steps of their unit test, the answer is likely to be:
After the development of the code, actually run the program, simple to do some functional testing, no problem
Code Tracking through breakpoint debugging
Have to say that this is right, but also has a certain value, but did not pay attention to the core unit testing value, then, what kind of test is effective unit testing it?
Effective unit testing requires the writing of simple, automated test code, and is done almost simultaneously with the development code .
It is not only necessary and important for developers to do unit testing, but it is the responsibility of each developer to unit-test the code they develop.
So what are the features and functions of unit testing? Lower cost of ensuring code quality, easier discovery of defects, repeatable execution, easier code maintenance, and defect resolution
Why do unit tests make it easier to spot defects ?
Because unit tests are tested at the lowest level of the system and are isolated from other methods or modules, the flaw in unit testing is easier to identify and resolve than other levels of defects.
One of the main reasons for unit testing is that the cost of fixing the defect is lower because the defect is resolved in the unit test as the shortest period of time to solve.
Testing in the eyes of developers--strangling defects in the cradle
1. What is unit testing?
Unit testing is an independent test developed by the developer while developing the product code, validating each unit of code it develops.
2. What is the purpose of unit testing?
Ensure that the logic of the program and the developer are consistent with its expectations.
3. Why should unit testing be performed by developers?
Developers are more aware of the expected results of the program than others, so writing effective unit tests is the most suitable candidate for the developer.
4. When do unit tests take place?
Unit testing and development of product code should be done at the same time, in fact, after the introduction of agile development theory, higher requirements are test-driven development, that is, unit test code to be written before the product code.
5. What are the unit test units? How does the test object understand?
From a practical point of view, a unit usually refers to a member method of a class, or it can be any combination of a program code module that has a definite function, a specification definition, an explicit interface definition, and a generally smaller size.
6. All say unit tests are independent tests, so what is independent testing?
Independence refers to isolating the code from the original program, isolating it as much as possible from the rest of the program or the outside world, and testing it separately for each unit.
Here, make a small summary to grasp the key points of unit testing:
A. Unit Test records expected behavior
B. Each unit test is tested for a single behavior
C. Isolate as much as possible from other parts of the program or outside
D. Once failed, you can clearly locate the cause of the failure
E. Can be run repeatedly, and each run has determined behavior, not affected by the last run
F. Can be executed quickly (about 10 seconds), simple, practical, efficient
G. Effective unit testing is automated
With the above introduction, you can have a general understanding of unit testing, what unit tests are, why unit testing, but what the unit tests need to overwrite the content? Unit tests, tests performed as white boxes, tests include major processes and faulted branches, and boundary conditions that measure the coverage of the test using the amount of code.
1. What does the unit test measure?
Unit testing needs to be guaranteed: Overwrite all newly developed code, modified code, and existing affected code.
A. Overwrite all branches of code, including normal path and error path
B. Overwrite all valid input/output conditions
C. Overwrite all input/output conditions that are not valid or expected
D. Overwrite all log files and return codes
E. If there is an error recovery step, make sure it is correct
F. Verify that the code is logically correct
G. Passing tests in a virtual translation build environment
H. Testing the performance of sensitive code (may need to describe the code path and access to the database)
I. Unit testing needs to be done in the development environment
J. Modify all translatable code snippets to make sure they are correct
2. The process of unit testing?
Design documents-Design unit tests-Create/modify code and related documents-unit test for new or modified code-code review-integration into code repositories
3. What is the starting mark for unit testing?
When you receive a new design document or a flaw, you need to start thinking about unit testing.
4. Mark of end of unit test?
The code is completed, resolves a known issue, or has proposed a next plan to resolve the remaining issues, and is then integrated into the code repository after code review and execution through unit testing.
5. Audit Unit Test Report:
Manual unit test reports require detailed steps, including the data set to be used
The Automated unit test report not only has a running result, but also gives a description of what part of the code is measured by this set of unit tests
Next, learn about test-driven development (TDD)
TDD is usually small to a single method for a very small function point at a time.
1. TDD process:
A. Before implementing a new feature, consider the code's usage requirements (including features, procedures, interfaces, etc.), and write test code for it
B. Let the newly written test code run with the existing test code
C. Writing a minimal implementation code for new features
D. Let the new test code run with the existing test code and modify the implementation code to run the result until the test passes
E. In this process, actively deal with code refactoring, making the underlying design and implementation more optimized to make the interface simpler
F. Repeat the above steps
A summary of TDD: Design code--write code--code not running---------refactoring
2. What is the purpose of TDD?
Not to validate the implementation of the code, but to describe the design specification for the purpose and usage of a piece of code, a description that is non-literal and executable.
3. What are the advantages of TDD?
TDD requires testing to be a priority, so you can make your code inherently testable, and thus guarantee almost 100% of your unit test coverage, with low defect density in your code, which facilitates early detection of defects and facilitates debugging.
The biggest benefit of TDD is not to end up with unit test assets, or to make unit tests pass through, but to continually refactor the code to make improvements to the code at the design level.
4. Summary of the relationship between TDD and agile development:
Unit tests for Software testing: Developer Testing