[Go] unit Test details

Source: Internet
Author: User

1. What is Unit test (Units testing)?

The word test (testing) is easy to understand, so what is a 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, it is generally required to have a clear input and output. Therefore, in general, each method in the source code that contains explicit input and output is considered to be a testable unit. Note that the output referred to here is not limited to the return value of the method or changes to the input parameters, but includes any data that was changed during the execution of the method.

Unit in the program can be easily understood as a module, a method. Unit tests are tests that are performed after each module is completed. Improve overall program quality by ensuring that there are no problems with each module.

2. What is the purpose of unit testing?

The purpose of unit testing is to isolate all source code of the application into the smallest testable unit, guaranteeing the correctness of each unit. Ideally, if each unit is guaranteed to be correct, the overall application is guaranteed to be fairly accurate.

Unit testing, on the other hand, is also a special type of document, and the test script itself is often the actual code of use for the code being tested, relative to the written document, and is useful for helping developers understand the use of the unit being tested.

3. means of unit testing

The test is divided into black box test white box test.

Black box test: Only know what to do under the test, black box testing is generally performed by testers.

Unit testing is a white-box test designed to verify that the class you are designing meets your needs.

When designing test cases, you should include reasonable input conditions and unreasonable input conditions.

White Box testing: Knowing how the software is being tested (how) completes its function and what it does (what), white-box testing is generally done by developers.

4. How to perform unit testing

1) Isolation

For unit testing, the first thing to do is to isolate the unit being tested against all external dependencies. In general, there are two main types of isolation techniques, called stubs, which are called mocks. For the two differences and trade-offs,Martin Fowler has a very classic article:Mocks aren ' t stubs. Simply put, if you want to stub or Mock a method, thestub means, for testing, to manually write a method of the same signature, which, according to my test needs, returns a certain output to the given input parameter, while the Mock the implementation of a method is automatically generated with a certain framework component, and a certain output is returned for a given input. Of course, the so-called stubs or mocksnot only refer to methods, especially in object-oriented programming, but also to attributes, to objects or to interfaces, and so on.

To facilitate isolation, a potential incentive for software design and development is that the designers and developers of software modules have to constantly think about how to make the code written can be easily quarantined in the context of the various features supported by the current language. While this may seem like a limitation, the SOLID principle of software design is in fact the same as it is, so it is not an advantage.

5. content of unit tests

Module interface test: Test the data flow through the module being tested. For this purpose, it is necessary to check the interface of the module, including parameter table, parameters of calling Submodule, whole data, file input / output operation.

Local data structure testing: Design test Cases to check the data type description, initialization, default values and other aspects of the problem, but also to find out the whole process of the impact of data on the module.

Path testing: Select the appropriate test case to test the important execution path in the module. Testing for basic execution paths and loops can uncover a number of path errors.

Error handling test: Check that the module's error handling function contains errors or defects. For example, whether to reject unreasonable input ; Whether the description of the error is difficult to understand, whether the wrong location is incorrect, whether the error is reported incorrectly, and whether the error condition is handled incorrectly ; Whether the error condition has caused systematic intervention before the error is handled.

Boundary testing: Pay particular attention to the possibility of an error in the data flow, in the control flow, just equal to, greater than, or less than the determined comparison value. Carefully select test cases for these places and test them carefully.

In addition, if there is a requirement for the runtime of the module, a critical path test is also required to determine the factors that affect the runtime of the module in the worst-case and mean-time sense. This kind of information is very useful for performance evaluation.

6. Steps for unit Testing

Usually unit tests are performed during the coding phase. After the source code has been compiled, reviewed and validated, the test case design for unit tests is started after the validation of no syntax errors. Use design documentation to design multiple test cases that can validate program functionality and identify program errors. For each set of inputs, the correct results should be expected.

module is not a separate program, when considering the test module, at the same time to consider its contact with the outside world, with some auxiliary modules to simulate the module connected with the other modules. These auxiliary modules are divided into two types:

Drive module: The main program equivalent to the module being tested. It receives the test data, transmits the data to the tested module, and finally outputs the measured results.

Pile module: A sub-module used to replace the module called by the test. Pile module can do a small amount of data operation, do not need to handle all the functions of the module brought in, but do not allow anything to do.

If a module is to perform multiple functions and appear as packages or object classes, such as packages in the Ada , modules inModula , classes inC + + . This module can be seen as consisting of several small programs. For each of these small programs first to do the unit test to do the work, the key modules also do performance testing. For procedures that support some standard procedures, the interconnection test should be initiated. This is specifically referred to as module testing to differentiate unit tests.

6. Importance of testing

Unit testing is the basis of software testing, so the effect of unit testing will directly affect the software post-testing, and ultimately to a large extent affect the quality of the product. The importance of unit testing can be seen in the following ways.

Time: If you do a good unit test, in the system integration is very smooth, so it will save a lot of time, whereas those who do not do unit testing because of time reasons or do not do it in the integration will always encounter those should be in the unit test can be found in the problem, And this problem in the integration often difficult for developers to anticipate, and finally found that this is a very low-level error in the remorse of their own time has been wasted a lot, this time waste is not worth, is the so-called outweigh the costs.

test results: According to the previous experience of testing, unit test results are very obvious, first of all, it is the basis of the test phase, do the unit test, in the late integration test and system testing is very smooth. Second, in the unit testing process can find some deep-seated problems, but also found some very easy to find and in integration testing and system testing is difficult to find problems. Again, unit testing is a special concern, not just to prove what the code does, but the most important thing is how the code does it, whether it does what it does, and doesn't do what it shouldn't.

test Cost: Some problems are easily discovered during unit testing, and the cost of finding a problem in later tests increases exponentially. For example, when the unit test found 1 problems need 1 hours, then the integration test to find that the problem takes 2 hours, when the system test found that it will take 3 hours, The same goes for positioning and problem solving, which is one of the factors that we want to eliminate as many bugs as possible to reduce later costs.

Product quality: unit testing of good and bad directly affect the quality of the product, may be due to a small error in the code caused the quality of the entire product to reduce an indicator, or lead to more serious consequences, if we do a unit test this situation can be completely avoided.

To sum up, unit testing is to build the cornerstone of product quality, we do not because the time to save unit testing do not do unit testing or casually do and let us waste too much in the late time is not worth it, we also do not want to because of the savings of those times lead to the development of the entire product failure or re !

7. How to write a good test code Guidelines:

1) test is better than no test.

2) test as small and fast as possible

3) Automating the test

4) code Coverage detection

5) repair all failed tests before writing any new implementation code or test code

6) The simple code also needs to be tested

7) Special attention to the boundary case of input parameters

8) do not test only normal processes, but also test optional and exception processes

9) for each reported Bug, write a corresponding test case to facilitate regression testing

always note that all tests are passed, not the program is really 100% no problem, the test is always only auxiliary

[Go] unit Test details

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.