Unit testing and prior test development
Author:
Summary: Eric Gunnerson introduced the idea of prior test development and provided a practical instance to demonstrate how to apply it in your own application. At the end of this column, I have my profile. If you read, you will know that I have served as the test team leader in the C # compiler before the program manager, which is the test team leader in the C compiler before this. These work experience make me analyze and try to avoid software errors. To reduce the number of errors in software, the method is to have a professional test group, and its job is everything possible to crash the software. Unfortunately, if you have a test group, even if you are an experienced developer, it will also tend to spend less time to ensure the reliability of the code. Software industry has a saying: "Developers should not test their own code." This is because developers have a good code of their own, and they know how to test the code with the appropriate approach. Although this says make sense, it is very important to ignore the very important point - if the developer does not test your code, how can the code run in the expected way? Simply put, they have nothing to know. Developers write a serious problem that runs normal or only in some cases. They usually only test code can run normally in a few cases, rather than verifying that the code can run normally in all cases. It is found that software errors have many cases of software errors: 1. Developers who have written code for the first time. 2, the developer who tried to run the code found. 3. Discover other developers or testers from the group. 4. As part of the product's large scale test. 5, found by the end user. If the software is found in the first case, the repair error is relatively easy, and the cost is also very low. After the situation is more, the higher the cost of repairing the software error; repairing a software error discovered by the end user may cost 100 or 1000 times cost. Moreover, users usually have the next version to solve the problem since the software error has not been able to continue. If the developer can discover all the software errors during the code, then it is fine. To do this, you must write a test that can run when writing code. There is a very good way to do this. Pre-test development so-called prior test development refers to preparation test before writing code. If all tests are running normally, you can conclude that the code is running normally; when adding new features, these tests will continue to verify that you destroy any part of the code. This concept was born in the SMALLTALK world in the early 1990s, and Kent Beck was written at SMALLTALKUNIT at the time. In the past few years, most of the environments have unit test tools, which have a very good tool for tools for .NET Framework, NUNIT (English). Example Below I will write an IntegerList class to introduce the working principle of prior test development. IntegerList is a variant of the ArrayList class that is used locally to store an integer, so there is no cost of packing and unpacking. The first step is to create a console project and add an IntegerList.cs source file to it. To connect the NUnit framework, you need to add a reference to the NUnit framework. In my system, they are located in D: / Program Files / NUNIT V2.0 / BIN. The second step is to spend some time to test how to test this class. This is similar to what functions should have, but focus on the specific use of the function (add value 1 to the list and check if it is successful), not the function itself (add a project to a list).
To generate such, we must first provide a list of tests to use: 1. Test the class to construct 2, add two integers to the list, and ensure that the number and project are correct. 3. Do the same operation, but for more items. 4. Convert this list to a string. 5, use foreach to enumerate this list. This example represents the idea when I start, that is, I want this class to do. Most classes only create a small part, and the test should be added as the class grows. Now I can start. I created a new C # class file called IntegerListTest.cs for all tests. The following is a file containing the first test: use system; use system.collections; use nunit.framework; namespace integerList {///
This is very good because it means that the test actually tested the program error. Now I can do this. I can do some simple work, although this is not very high: public int count {get {return Elements.Length;}} public void add (int value) {INT newIndex; if (Elements! = Null) {int [ ] newElements = new int [elements.Length 1]; for (int index = 0; index