Document directory
- What is Unit Testing )?
- Why is unit testing required?
- How to perform unit testing?
- English document
The purpose of this article is to explain in the most refined language what is unit testing, why unit testing, and how to perform unit testing.
What is Unit Testing )?
The term Testing is easy to understand. What is Unit? A unit refers to the smallest set of source code that can be tested in an application. A set of source code can be tested and generally requires explicit input and output. Therefore, in general, each method containing clear input and output in the source code is considered a testable unit. Note that the output here is not limited to the return values of methods or changes to input parameters, but includes any data changed during the execution of methods.
Why is unit testing required?
Unit testing aims to isolate all source code of an application into the smallest testable unit to ensure the correctness of each unit. Ideally, if each unit is correct, the overall correctness of the application can be ensured.
On the other hand, unit testing is also a special type of documentation. Compared with the written documentation, the testing script itself is often the actual use code of the tested code, it is quite helpful for developers to understand the use of tested units.
How to perform unit testing? Isolation
To perform unit tests, you must first isolate the tested units from all external dependencies. Generally, there are two optional technologies for isolation: Stub and Mock. For the two differences and trade-offs, Martin Fowler has a classic article: Mocks Aren't Stubs. To put it simply, if Stub or Mock is to be used as a method, Stub refers to manually writing a method with the same signature for testing. Based on my test needs, A certain amount of output is returned, while Mock automatically generates a method implementation based on certain framework components. A certain amount of output is returned for a given input. Of course, the so-called Stub or Mock does not refer to methods, especially in object-oriented programming. It can also be attributes, objects, or interfaces.
To facilitate isolation, a potential incentive for software design and development is that the designers and developers of the software module have to think about it from time to time, with the features supported by the current language, you can easily isolate the written code. Although this seems like a restriction, it is in line with the SOLID principle of software design. Therefore, it is not an advantage.
Test
After isolation is solved, we can focus on the testing of a unit. The first thing to mention is that a unit is usually tested by writing a test script, giving input and verifying output. However, not all unit tests can be automatically and conveniently completed by computers. Especially the judgment of abstract elements such as the shape UI. Therefore, from a relatively broad perspective, all tests that can be used to verify the correctness of a tested unit can be considered as effective tests.
Automation
The problems of isolation and testing are solved. The next question to be discussed is the efficiency of testing. As mentioned above, all unit tests can be automated by computers, but for those unit tests that can be automated by computers, how can we improve the efficiency of writing test code and running tests? The answer is that many testing frameworks and tools have emerged one after another. These testing frameworks or tools generally provide tools to improve the efficiency of creating test scripts, and provide functions to automate the execution test and view the execution results, it is often easy to integrate with the automated compilation and deployment of code to automate testing.
Guidelines
Here we have sorted out some Guidelines on how to write the test code:
- It is better to have a test than not.
- Test as small and fast as possible
- Automate testing
- Code coverage Detection
- Fix all failed tests before writing any new implementation code or test code.
- Simple code also needs to be tested
- Pay special attention to the input parameter Boundary Case
- Do not only test the normal process, but also test the optional and exceptional processes.
- Write a test case for each reported Bug to facilitate regression testing.
- Always pay attention that all tests pass, does not mean that the program is really 100% OK, the test is always a helper
English document
- Http://en.wikipedia.org/wiki/Unit_testing
- Http://martinfowler.com/articles/mocksArentStubs.html
- Http://geosoft.no/development/unittesting.html
- Http://developer.apple.com/mac/library/documentation/DeveloperTools/Conceptual/UnitTesting/1-Articles/UTGuidelines.html#apple_refdocuidTPEAAACBHC-BBCBGHCJ