Since it is from scratch, first introduce nunit (http://www.nunit.org/):. the unit test framework under the. NET Framework provides functions similar to JUnit.. NET Framework ).
The basic principle is to use the reflection mechanism of. Net to identify the unit tests in the code using the metadata (attribute. Unit test is an important part of test-driven development (TDD). TDD is also an agile development method (such as extreme programming-eXtreme Programming) important components ...... In short, unit testing is very important. Pai_^ (details about TDD and XP are available on many blogs in the blog Park, and more on Google)
Example:
1. Download and install nunit (the latest version may be 2.2.0)
2. Important Step: test whether nunit is successfully installed.
Method: Open nunit, file -- open -- select nunit. Tests. dll in the bin directory of the nunit installation directory. At this time, the tree list on the left of the nunit main window will show many test names. Click the run button and the test starts to run, until all the tests in the tree list on the left of the nunit main window are green, it is successful. (A red prompt appears for failed tests and a yellow prompt appears for non-running tests. In this step, problems may occur in the test set of the console runner. In case of a problem, restart nunit and run again. Generally, it is okay)
2. How to Use the nunit framework in development?
1) Open vs. NET 2003, create a C # console project, and add nunit. Framework in the references window of the project (in the Solution Explorer window, right-click and add reference ...)
2) Write a class public class account // bank account class
{
Private float balance; // account balance
Public void deposit (float amount) // save money
{
Balance + = amount;
}
Public void withdraw (float amount) // get the money
{
Balance-= amount;
}
Public void transferfunds (Account destination, float amount) // Transfer
{
Destination. Deposit (amount );
Withdraw (amount );
}
Public float balance
{
Get {return balance ;}
}
Public static void main (string [] ARGs)
{
Account source = new account (); // create an account
Source. Deposit (200.00f); // store 200
Account destination = new account (); // another
Destination. Deposit (150.00f); // save 150
Source. transferfunds (destination, 100.00f); // transfer the first account to the second 100
}
}
This class is very simple. It is compiled, run, and everything is OK.
3) In the same project, add a test class to test the methods in the account class (several attributes are the most critical)
Using nunit. Framework; // do not forget this line
[Testfixture] // This attribute indicates that the accounttest class contains a test
Public class accounttest
{
[Test] // This attribute indicates that the testtransferfunds () method is used for testing.
// Generally, the name of the test method is to add test before the name of the method to be tested.
Public void testtransferfunds ()
{
// Preparations
Account source = new account ();
Source. Deposit (200.00f );
Account destination = new account ();
Destination. Deposit (150.00f );
Source. transferfunds (destination, 100.00f); // Transfer
// Use the assert class in nunit. Framework to check whether the balance of the two accounts after the transfer is correct.
Assert. areequal (250.00f, destination. Balance );
Assert. areequal (100.00f, source. Balance );
}
}
Compile and generate an EXE file (if you want to generate a DLL, change the output type attribute of this project to class library. In the Solution Explorer window, right-click the project name and choose Properties. In this example, the main () method is not required to generate the DLL ).
4) Open nunit, file -- open, and find the compiled exe. Then run, the eye-catching green shows that the test has been successful ^_^.
If you want to see if the test fails, you can change the value in assert. areequal ......
In this example, onlyTest Fixture and TestAttribute,Other usage instructions are clearly written in the nunit document, and there are some better examples in the document ......
What is the purpose of Automated unit testing? A: save time and effort. When a system needs to test thousands of classes/methods, the efficiency of manual testing methods (printing information on the console, etc.) is relatively low.
Conclusion: nunit makes good use of the reflection mechanism, making unit testing very convenient. However, for complex objects, writing low-coupling test code may be difficult. In this case, mockobject can be used. net is widely used in nmock, To be continued ......