Many people have introduced the ten essential tools developed under. net. The unit test tool is nunit. I don't know how many people have practical project experience on it. After some time of use, I am very annoyed with a defect of it. I will introduce it in detail below:
Unit Tests for database-related projectsCodeTo test the addition, deletion, and modification functions. However, this will affect each other in various functional tests, damage the test database, and generate junk data so that each unit test runs in an unknown state, in short, it is a very bad thing.
In this case, the general solution is to prepare a database for testing and restore the database after each unit test is completed. This can only be called a solution, but it is not beautiful.
I found another clean solution by checking the information: "using COM +'s automatic transaction !"
The basic solution is as follows:
1. Write a class inherited from servicedcomponent, declare transactionoption. required, and roll back the transaction in the teardown method. The specific code is as follows:
Using System;
Using Nunit. Framework;
Using System. incluiseservices;
Namespace Transactiontesting
{
[Testfixture]
[Transaction (transactionoption. Required)]
Public Class Databasefixture: servicedcomponent
{
[Teardown]
Public Void Transactionteardown ()
{< br> If (contextutil. isintransaction)
{< br> contextutil. setabort ();
}
}
}
}
2. modify the original unit test code so that all tests are inherited from the class just written.
After reading this method, I am very excited. It's really nice! However, after careful tests, we found that nunit has very poor support for this method and many exceptions will be thrown during runtime. Is there a natural defect in calling COM + through reflection? (I didn't see the nunit code. I guess it)