Preparation
1. log on to http://www.testdriven.net/download.aspx. download the personal version (personal version, which is free of charge) on the website, and then install it. you can install it by default.
2. Copy nunit. Framework. DLL to a separate folder according to the installation path, and call it DLL for ease of use. If it is installed by default, the path is as follows:
C: \ Program Files \ testdriven. NET 2.0 \ nunit \ 2.5 \ net-2.0 \ framework
CreateProgramSet
You can name this Assembly testdriven_netsample.
Use testdriven. NET attributes for testing
1. Drag the above DLL folder to the Assembly and reference nunit. Framework. dll
2. Create a new class named domaintest andCodeAdd to new class
// Add reference for the program
Using nunit. Framework;
Namespace testdriven_netsample
{
[Testfixture]
Public class domaintest
{
Private int x = 0;
[Setup]
Public void init_x ()
{
X = 10;
}
[Test]
Public void get_x ()
{
Assert. areequal (10, X );
}
[Teardown]
Public void drop_x ()
{
X =-1;
}
}
}
3. Set a breakpoint at private int x = 0; and right-click the get_x () function in the vertex, for example:
Then follow the F11 command to track the execution sequence of the program and change the value of variable X to see how it works.
Common attributes of testdriven. net
Testdriven. net common attributes, because not all testdriven is described here. net properties, but only introduces a part of the common properties, if you need to use these properties outside of the property, please refer to the following link: http://www.cnblogs.com/lyj/archive/2008/09/03/1283390.html
1) attributes of testfixture
This property is used to modify the test class. It indicates that this class contains the test method. It is a prerequisite for all other test properties, and other attributes without it are invalid.
Note: There are some restrictions on using this attribute to modify a class: the class to be modified must be public and must have a default constructor.
2) setup Properties
This attribute is used to modify the method. After modification, this method is executed before each test method is called.
3) test attributes
This attribute is used to modify the method, indicating that the method is a test method.
Note: The test method cannot contain parameters; otherwise, it cannot be tested.
4) teardown attributes
This attribute is used to modify the method. It indicates that this method is executed after each test method is called.
Assertion-your prosecutor
Ø what is assertion: similar to a breakpoint, we can check whether the execution of the program is consistent with our expectation.
Ø assertions Overview: in the testing framework, assertions are the core of unit testing. We need to assert the program in the testing. If an asserted fails, the program reports an error. If a test contains multiple assertions, those that follow the failure assertions will not be executed, so each test method should have only one asserted.
For example, replace the content in the get_x () function with the following:
Public void get_x ()
{
Assert. areequal (10, X );
Assert. arenotequal (1, x );
// Assert. areequal (1, x );
}
Then execute the statement to see the result. Then, cancel the comment of the commented-out statement and execute the statement to see the result. This is the result when an error occurs (note the red part ):
------ Test started: Assembly: Dal. Test. dll ------
Testcase 'testdriven _ netsample. domaintest. get_x 'failed:
Expected: 1
But was: 10
Testdriven_netsample.cs (26, 0): In testdriven_netsample.domaintest.get_x ()
0 passed, 1 failed, 0 skipped, took 0.80 seconds (nunit 2.5.1 ).
This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/soldierluo/archive/2009/10/10/4651372.aspx