Unit testing and prior testing and development

Source: Internet
Author: User
Unit testing and prior testing and development
Author:
Abstract: Eric Gunnerson introduces the idea of pre-test and development, and provides a practical example to demonstrate how to apply it in your own application.
There is my introduction at the end of this column. If you have read it, you will know that I was the test leader of the C # compiler before I became the program manager, before that, he was the test leader of the C ++ compiler. These work experiences have made me particularly interested in analyzing and avoiding software errors.

To reduce the number of errors in the software, one way is to have a professional test group, and its job is to do everything possible to cause the software to crash. Unfortunately, with a test group, even experienced developers tend to spend less time ensuring code reliability.

There is a saying in the software field: "developers should not test their own code ". This is because developers know their code well and they know how to test the code in an appropriate way. Although this phrase makes sense, it ignores a very important point-if developers do not test their own code, how can they know whether the code can run as expected?

Simply put, they have no idea. Writing code that is not running normally or that runs normally only in some cases is a serious problem. They usually only test whether the code can run normally in rare cases, rather than verifying that the code can run normally in all cases.

Software error detected

There are many software errors:

1. Found by developers who wrote code for the first time.
2. Found by the developer who attempted to run the code.
3. Discovered by other developers or Testers in the group.
4. As part of large-scale product testing.
5. discovered by the end user.

If a software error is found in the first case, it is easier to fix the error and the cost is low. The lower the case, the higher the cost of fixing software errors. Fixing a software error discovered by an end user may take 100 or 1000 times the cost. Not to mention that users often fail to continue their work due to software errors, and they will not be able to solve the problem until the next version.

If developers can discover all software errors during code writing, it would be better. To do this, you must write a test that can run when writing code. There is a good way to do this.

Test and develop in advance

The so-called pre-test development refers to writing a test before writing the code. If all tests run properly, you can determine that the code is running normally. When new features are added, these tests continue to verify that you have destroyed any part of the code.

This concept was born in the Smalltalk world in the early 1990s S. Kent Beck wrote smalltalkunit at that time. In the past few years, most environments have been equipped with unit testing tools. Among them, there is an excellent tool for the. NET Framework field, that is, nunit ).

Example

Next, I will compile an integerlist class to introduce the working principle of the test and Development in advance. Integerlist is a variant of the arraylist class used to store integers locally. Therefore, there is no overhead for packing and unboxing.

The first step is to create a console project and add an integerlist. Cs source file to it. To connect to the nunit framework, you must add a reference to the nunit framework. In my system, they are located at D:/program files/nunit V2.0/bin.

The second step is to take some time to consider how to test the class. This is similar to the process of determining which functions the class should have, but focuses on the specific purpose of the function (add value 1 to the list and check whether it is successful ), instead of the function itself (add a project to the list ). To generate this class, we first need to provide a list of tests to be used:

1. The test class can be constructed.
2. Add two integers to the list and ensure that both the number and project are correct.
3. perform the same operation for more projects.
4. Convert the list to a string.
5. Use foreach to enumerate this list.

To some extent, this example represents the idea at the beginning, that is, the operation that I want this class to execute. Most classes only create a small part at a time, and the test should be added as the class grows.

Now I can start. I create a new C # class file named integerlisttest. CS to store all tests. The following is a file containing the first test:

Using system;
Using system. collections;
Using nunit. Framework;

Namespace integerlist
{
/// <Summary>
/// Summary of integerclasstest.
/// </Summary>
[Testfixture]
Public class integerclasstest
{
[Test]
Public void listcreation ()
{
Integerlist list = new integerlist ();
Assertion. assertnotnull (list );
}
}
}

The [testfixture] attribute marks this class as a test class, and the [test] attribute marks the listcreation () method as a test method. In this method, I created a list and created it using the assertion class test object gets.

I started the nunit GUI testing program, opened executable files, and executed these tests. The following figure is displayed.


Figure 1 nunit GUI showing test results

This indicates that all tests have passed. Now I want to add some real functions. The first operation is to add an integer to the list. This test is as follows:

[Test]
Public void testsimpleadd ()
{
Integerlist list = new integerlist ();
List. Add (5 );
List. Add (10 );
Assertion. assertequals (2, list. Count );
Assertion. assertequals (5, list [0]);
Assertion. assertequals (10, list [1]);
}

In this test, I chose to test two operations at the same time:

1. The list correctly maintains the Count attribute.
2. The list can contain two items.

Some testing-driven developers advocate that testing should be as many as possible, but it is incredible for me to test only the number, not the number of projects, so I chose to test both.

During compilation of this Code, the compilation failed because the integerlist class does not have a method. Therefore, I added the following code for compilation:

Public int count
{
Get
{
Return-1;
}
}

Public void add (INT value)
{
}

Public int this [int Index]
{
Get
{
Return-1;
}
}

Then I return and run the test, and they are displayed in red, indicating that the test failed. This is good because it means that the test has actually tested a program error. Now I can execute this implementation. I can do some simple work, although it is not very efficient:

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 <elements. length;
Index ++)
{
Newelements [Index] = elements [Index];
}
Newindex = elements. length;
Elements = newelements;
}
Else
{
Elements = new int [1];
Newindex = 0;
}
Elements [newindex] = value;
}

Public int this [int Index]
{
Get
{
Return elements [Index];
}
}

I have completed a small part of the class and have compiled a test to ensure that it works normally, but I only tested a small part of the project. Next, I want to write a test to check the 1000 items:

[Test]
Public void testonethousanditems ()
{
List = new integerlist ();

For (INT I = 0; I <1000; I ++)
{
List. Add (I );
}

Assertion. assertequals (1000, list. Count );
For (INT I = 0; I <1000; I ++)
{
Assertion. assertequals (I, list);
}
}

This test runs normally, so no changes are required.

Add tostring () method

Next, I will add the test code to test whether tostring () can run normally:

[Test]
Public void testtostring ()
{
Integerlist list = new integerlist ();
List. Add (5 );
List. Add (10 );
String T = List. tostring ();
Assertion. assertequals ("5, 10", T. tostring ());
}

Failed. It does not matter. The following code passes:

Public override string tostring ()
{
String [] items = new string [elements. Length];
For (INT Index = 0; index <elements. length; index ++)
{
Items [Index] = elements [Index]. tostring ();
}
Return string. Join (",", items );
}

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.