Unit Test detailed

Source: Internet
Author: User

Overview of a unit test

The factory tests every component before assembling a television, which is unit testing.

In fact, we are doing unit tests every day. You write a function, in addition to very simple, always to perform, to see if the function is normal, and sometimes to find ways to output some data, such as pop-up information window What, this is also unit test, Lona this unit test as a temporary unit test. Software that only carries out the staging unit tests, testing for code is very incomplete, code coverage is more than 70% difficult, the uncovered code may leave a large number of small errors, these errors will also affect each other, when the bug exposed difficult to debug, significantly improve the late test and maintenance costs, Also reduce the competitiveness of developers. It can be said that adequate unit testing is the only way to improve the quality of software and reduce the cost of development.

For programmers, if you get into the habit of doing unit tests on your own code, not only can you write high quality code, but you can also improve your programming level.

To perform adequate unit testing, you should write the test code specifically and isolate it from the product code. Lona that the simpler approach is to create a corresponding test project for the product engineering, set up a corresponding test class for each class, and set up a test function for each function (except, very simply). First of all, talk about Lona's views on several concepts.

It is generally believed that in the age of Structural procedure, Unit Test refers to the unit is the function, in today's object-oriented era, Unit test unit refers to the class. In Lona practice, the class as a test unit, high complexity, poor operability, and therefore still advocate the function as a unit test unit, but you can use a test class to organize all the test functions of a class. Unit tests should not overemphasize object-oriented, because local code is still structured. Unit test workload is large, simple and practical and efficient is the hard truth.

One idea is to test only the interface (public function) of a class, without testing other functions, it does make sense from an object-oriented perspective, but the purpose of the test is to find the wrong and final error, so as long as it is a more likely function to contain errors, it does not matter whether the function is private or not. For C + +, you can use a simple method to partition the function you want to test: simple functions such as the implementation of data read-write functions in the header file (inline function), all the functions written in the source file to be tested (except constructors and destructors).

What time is the test? The earlier the unit test the better, the earlier to what extent? XP development theory stresses TDD, that is, test-driven development, first write test code, and then develop. In the actual work, you can not overemphasize what first after what, it is important to be efficient and feel comfortable. From the experience of Lona, first write the framework of product functions, and then write test functions, the function of the product to write test cases, and then write the product function code, each write a function point to run the test, at any time to supplement the test cases. The so-called framework for first writing product functions, refers to the first to write the implementation of the empty function, there is return value of a random return of a value, compiled and then write the test code, at this time, function name, parameter table, return type should be determined, the written test code to be modified after the possibility of relatively small.

Who tested it? Unit tests, unlike other tests, can be thought of as part of the coding effort and should be done by the programmer, that is, the code that passes the unit tests is the completed code, and the test code is submitted at the same time when the product code is submitted. The testing department can conduct a certain degree of audit.

On the pile code, Lona that unit testing should avoid writing pile code. Pile code is used to replace some code code, for example, the product function or test function called an unnamed function, you can write a pile function to replace the called function, the pile code is also used to implement test isolation. Developed in a bottom-up manner, the bottom code first develops and tests, avoids writing the pile code, the benefit of this is: reduce the workload, test the upper function is also indirect test of the lower function, when the underlying function is modified, regression test can confirm whether the modification causes the upper function to produce an error.

Two Test code writing

Most of the articles on unit testing are in Java For example, this article takes C + + as an example, and the Unit Test tool introduced in the latter part only introduces C + + unit test tools. The development environment for the following sample code is VC6.0.

Product class:

class CMyClass {
public:
int Add(int i, int j);
CMyClass();
virtual ~CMyClass();
private:
int mAge; //年龄
CString mPhase; //年龄阶段,如"少年","青年"
};

Set up the corresponding test class cmyclasstester, in order to save the amplitude, only the source file code is listed:

void CMyClassTester::CaseBegin(){
//pObj是CMyClassTester类的成员变量,是被测试类的对象的指针,
//为求简单,所有的测试类都可以用pObj命名被测试对象的指针。
pObj = new CMyClass();
}
void CMyClassTester::CaseEnd(){
delete pObj;
}

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.