UseNunitGetting started with Unit Testing
Preface:
NunitIs an open-source project.Nunit, You can easily and quickly. NetComponent for unit testing. ExploitationNunitWithout modifying the originalCodeTo test the functions and components to be tested. If you are interestedNunitAndVstsCompare the built-in unit tests:)
Body:
If your machine is not installedNunit, Please firstHttp://www.nunit.org/download.htmlDownload and install it on the machine.
Next, let's take a look at how to useNunitUnit Test
Now, I have suchAccountClass:
NamespaceBank
{
Public Class Account
{
Private FloatBalance;
Public VoidDeposit (FloatAmount)
{
Balance + = amount;
}
Public VoidWithdraw (FloatAmount)
{
Balance-= amount;
}
Public VoidTransferfunds (AccountDestination,FloatAmount)
{
}
Public FloatBalance
{
Get{ReturnBalance ;}
}
}
}
But I don't know if this class can work correctly, so I need to perform a unit test on the account class.
You can create a new class library project and specify account. dll andNunit. Framework. dll. Note that the nunit. Framework. dll file is in the bin directory where you install nunit.
Then I am hereAdd an accounttest class file to the new class library project and compile it. The file is as follows:
NamespaceBank
{
UsingNunit. Framework;
[Testfixture]
Public Class Accounttest
{
[Test]
Public VoidTransferfunds ()
{
Account source =NewAccount ();
Source. Deposit (200.00f );
Account destination =NewAccount ();
Destination. Deposit (150.00f );
Source. transferfunds (destination, 100.00f );
Assert. Areequal (250.00f, destination. Balance );
Assert. Areequal (100.00f, source. Balance );
}
}
}
Note that this test class must be public, otherwise nunit will not work properly.
Next, we start nunit (there is a shortcut icon on your desktop), and then open the newly compiled accounttest. dll in file-> open project. At this time, you will find that there is a "run" button on the right that can be used. Click it. At this time, the Unit Test of the account class starts. After the test is completed, a red flag appears, indicating that the test fails. This indicates that there is a problem with our account class. We need to complete the withdraw method in the account class:
Public VoidWithdraw (FloatAmount)
{
Balance-= amount;
}
After the account class is re-compiled, we click the "run" button again. At this time, the red color turns green, which indicates that the test has passed.
In this way, you have completed the account class test. Next, let's talk about it in detail.AccounttestStrangeAttributes.
LTestfixture
This attribute can only be used on the class. It tells the nunit class that there are methods to be tested.
However, note that this class is guaranteed:
1.This class must be public.
2.This class cannot be abstract.
3.This class must have a default constructor.
4.The attribute marked by methods in this class can only appear once:Setup, teardown, testfixturesetupAnd testfixtureteardown.
LTest
This attribute can only be markedTestfixtureIs Used in the class, it tells nunit that this is a method to be tested.
In the accounttest classTransferfundsMethodAssert.
It is similar to the assert class in. NET Framework. IfAssertThe method in returns false. The test result is failed. Otherwise, the test is successful. In nunitAssertTo determine whether a test is successful.
Now, I believe you have a basic understanding of nunit usage. Congratulations.
Postscript:
NunitIt is a well-developed and free tool, but it also indicates that it is worse than the unit test tool provided by vsts, and his support for vs is also quite good, in addition, many functions can be defined for our testing needs, and support for plug-ins also gives us more space to play.Nunit.
For more details, please refer to the nunit help documentation :)