Although there are already a large number of software development methodologies and auxiliary tools to help the development team improve the software quality, prevent and detect bugs, some very simple and practical means are still necessary to improve the software quality, for example, unit test, such as code review.
Unit testing is a basic software quality assurance method. With the popularity of agile development and continuous integration, a high-quality software cannot be imagined without unit testing. For example, Java and C # all have very mature unit testing frameworks, and Google also open source their C ++ unit testing frameworks and mock frameworks.
This articleArticleSeveral questions will be discussed. What is unit testing? Why is unit testing so important? When should I perform a unit test? How to perform unit testing? What problems should we avoid in unit testing?
What is unit testing? In fact, the essence of unit testing is assert, suchC LanguageThere is a built-in assert function that returns error codes when a condition is not met, while the built-in assert and verify of MFC are more powerful. If you have already used the assert function, you have already entered the initial stage of unit test. You can learn more about the unit test method. For C ++ programming, Google's googletest framework can be used for unit testing. This unittest framework integrates common requirements for unit testing and avoids repeated development.
There are two common development tasks: maintaining the legacy code or occasionally adding some small feature, and building a new software. Both tasks inevitably need to repeatedly modify a feature.CodeOr modify the bug based on the feedback from the tester or user. How can we ensure that the modified Code does not introduce new problems? If you say that you use your character to guarantee your service, I will accept it. For a formal process, there should be a formal means to ensure that the modification of a bug does not introduce two or three new bugs, or does not cause errors in the previous correct functions, this is the importance of unit testing. With enough unit testing, you can confidently say that the new Code is okay.
Another importance of unit testing is to help you sort out the design. A common excuse against unit tests is that our applications are too complex to write unit tests. It is not complicated to use. In fact, it is a problem in software design that leads to the inability to test. testability is also an essential feature of software. If the design ensures that a function can only fulfill a single responsibility for an interface without too many coupling dependencies, testing is actually very simple.
Another advantage of unit testing is that it can be integrated into the continuous integration process, or simply and quickly run repeatedly through scripts without manual intervention, which is very important for improving development efficiency.
When should we write unit tests? I have finished writing all the software code. Can I write it again when I run it? In fact, the unit test should be written at the design stage. After the unit test is completed, the actual function part of the Code should be written. The design should be based on interface design, unit testing should also be based on interface testing, and for some complicated internal logic, there should be a lot of unit testing to ensure coverage. For some core components, the unit test code should even exceed the actual code. It should be noted that the Code in the unit test part should be treated like the working code. Similarly, version management should be performed in clearcase or SVN, and the code in the unit test part should be reviewed, ensure that the test code is correct. I personally feel that unit testing (including part of the integrated test code) accounts for 30% to 40% of the tasks in the entire code implementation, which is normal. The introduction of unit testing will lead to some delays in the early stage, which is unavoidable and will greatly reduce the maintenance work in the later stage. This is my own experience.
So how do I write unit tests? Can I test the unit test GUI and click input? The first thing to be clear is that unit testing will not replace other testing methods. unit testing is just a white box testing. Unit testing is a testing method written by developers to ensure the correct completion of a logic function. In addition, unit testing should not involve other external dependencies or other modules, such as GUI clicks, network communication, database communication, or the need to install a third-party software. This requires developers to do a good job of design, try to isolate the parts that may have coupling dependencies and check again during integration or other tests.
Let's explain it with a simple example. On a GUI, when you press a button, it will change to another color. After the function is completed, it will restore the original state, or a control allows users to input and verify the input, save or remind users of problems based on the verification results. These are common procedures. These processes are obviously not atomic and involve model, view, and control.ProgramMembers often implement all these functions in classes such as cxxxdialog, and they still feel very well encapsulated. "Isn't this object-oriented encapsulation? I encapsulated them all in the class ?!"
Let's take the input validation to illustrate how to break down the MVC process. Step 1: Enter the user and click OK. This part is obviously in view and control aspects. This part can be checked and tested by tester. developers need to ensure that the functions are complete and simple to run correctly. Step 2: Check the input data and perform logical operations. This part is obviously complicated logic, involving model and control. Generally, it does not involve interface display. The input part is data, and the output part is the result of the check. Obviously, unit tests should be performed. Step 3: if an error occurs, report the error to the user. This part is basically based on the Interface display and needs to be tested by tester. Step 4: The data is correct and the user input is saved. This part involves data operations and unit tests. It involves databases and may require integration tests.
From the previous analysis, we can see that there can be unit tests for tests involving GUI interfaces. developers need to do more work and abstract part of the logic computing code. This is not easy to implement, but it is worth doing.
When a user or Tester finds a problem in the later development stage, the developer should convert the problem into a test case. This ensures that the modified Code does not cause other bugs to reappear, it is also a good coverage of the Code logic. For functions that depend on other modules, we can use the mock interface or compile and implement integration tests between modules. In addition, developers can further customize the unit test framework functions they need. For example, I have designed a flexible solution to add test cases for the current project, the test cases are written in a format similar to ini or XML. The unit test program reads the test cases for testing. This makes it easy to introduce a new test case without modifying the compilation code. Of course, this design is also customized for the output of our project mainly for the COM interface, more like an integration test.
The above is a general discussion about the advantages of unit testing, which does not involve practical technical methods. We hope that programmers or technical leaders will pay more attention to unit testing and use unit testing in their work to make it a tool for daily work to ensure high-quality software development.