Author: Moxie
Test is one of the important links of software development. According to software development process tests can be divided into: unit test, integrated test, system test, domain test (Field Test). We will discuss the unit testing of programmers. This article first introduces the definition of unit testing, why should the unit testing? Unit tests can give us the benefits. Then we will introduce the category of unit testing, and finally discuss many friends who do not write an excuse of the unit test. It is hoped that this article can once again caus you to pay attention to unit testing, and persuade your boss to write a unit test, allowing the beautiful unit test to be truly applied to your project. What is the unit test unit test is a small piece of code written by the developer, which is used to verify that a small and clear function is correct. Typically, a unit test is used to determine a particular function of a particular condition (or scene). For example, you might put a big value into a ordered list, then confirm that this value appears at the tail of the List. Alternatively, you may delete characters that match some mode from the string, and then confirm that the string does no longer contain these characters. The unit test is done by the programmer himself, and the final benefit is also a programmer himself. It can be said that the programmer has the responsibility to write function code, and it is also responsible for writing unit testing for his code. The execution unit test is to prove the behavior of this code and our expectations. Why do you want to use a unit to test the code, you will definocate repeatedly to ensure that it can compile. If it is a code that is not passed, no one will be willing to deliver the boss. However, the code is compiled, but its syntax is correct; we can't guarantee that its semantics must be correct, without anyone can easily promise the behavior of this code must be correct. Lucky, the unit test will guarantee our commitment. Writing unit test is the consistency of behavior to verify this code. With unit testing, we can confidently deliver your own code without any worries. The unit test has the following advantages: 1, it is a verification behavior. Each function in the program is a test to verify its correctness. It provides a slowdown for future development. Even in the late development, we can easily increase the functionality or change program structure without worrying about it in this process. And it provides a guarantee for the reconstruction of the code. This way, we can improve the program more freely. 2, it is a design behavior. Writing unit testing will allow us to observe, think about the caller. Especially write test (Test-first), forcing us to design the program to easily call and test, so that we will force us to release the coupling in the software. 3, it is a kind of behavior of writing documents. Unit testing is an invaluable document that is the best documentation for displaying functions or how to use. This document is compileable, runable, and it maintains up to date and is always synchronized with the code. 4, it has a represession. Automated unit test avoids the code to return, after writing, can run the test with a quick run, anywhere. The category of unit tests If you want to define a clear category to unit testing, it indicates which features are unit tests, which seems difficult. However, the four issues discussed below, basically, the category of unit testing, the unit tests to be done. 1. Is its behavior and I expect? This is the most fundamental purpose of the unit test. We use the code to use the unit test to prove what it does what we expect. 2. Is its behavior always consistent with my expectation? Write a unit test, if only one correct path of the code is tested, let it take it correctly, it is not true to complete.
Software development is a complex project. If you test a certain piece of code, you need to confirm when you consistent with your expectations: In any case, this code is consistent with your expectations; such as parameters are very suspicious, hard disk There is no remaining space, the buffer overflows, when the network is dropped. 3, can I rely on the unit test? Can't rely on code is not much. Since the unit test is used to ensure the correctness of the code, then unit tests must also be worth depending on. 4, the unit test shows my intention? The unit test can help us fully understand the usage of the code. From the effect, the unit test is like the document that can be performed, indicating that the function you can expect this code when you call the code with a variety of conditions. The excuse of the non-test test is here, we have already listed various reasons for using unit tests. Perhaps everyone agrees, yes, do more tests. This kind of human consent is more, yes, eat more vegetables, should quit smoking, there should be more rest ... This doesn't mean that everyone will do this, isn't it? 1. The write unit test is too time. We know that the sooner the BUG, the soon saving more time and lower more risks. The following graph is taken from << Practical Software Measure >> (CAPERS JONES, McGraw-Hill 1991), which lists the time spent on preparation test, execution test, and modification defects (based on a function point), these data display units The cost efficiency of the test is about twice the integrated test, three times the system test (see bar chart). Terminology: Field Test means all test activities made in a certain field after software investment. If you still think that when writing product code, there is still no time to write test code, then consider these questions: 1), for the code written, how much time you spend on debugging. 2) For the previous code you think of the correct code, but in fact, these code exist, how much time you spent re-confiring these codes. 3) For a BUG reported by someone, how much time you spend to find the source location that caused this bug. After answering these questions, you must no longer be "too much time" as an excuse to reject the unit test. 2, the time of running test is too long. Suitable tests will not cause this situation. In fact, most tests are very fast, so you can run thousands of tests within a few seconds. But sometimes some tests will take a long time. At this time, you need to separate these time-consuming tests and other tests. This test can usually be run once a day, or once a few days. 3, the test code is not my job. Your job is to ensure that your code can complete your behavior correctly, just contrary, test code is an indispensable job. 4. I don't know the behavior of the code, so there is no test. If you really don't know the behavior of the code, it is estimated that it is not encoded now. If you don't know the behavior of the code, how do you know that the code you wrote is correct? 5, but these code can be compiled. We have already said before, the code is compiled just to verify its grammar. But it doesn't guarantee that its behavior is certain. 6, the company invited me to write code, not write tests. The company pays you a salary to let you write product code, and unit test is generally a tool, a tool, such as an editor, a development environment, a compiler, etc. in the same location.