Open-source unit testing tool nunit

Source: Internet
Author: User

Nunit is. the unit test framework under the. NET platform is from JUnit. It is a member of the xunit team. The officially released version has been updated to 2.5.10. You can download the unit test framework if it has not been installed. although today's VS has integrated its own unit test, it is quite convenient to use, but we know a little about it. NET platform source project or some examples, the test framework is not nunit, so it is very necessary to understand nunit.

 

Start

After installation, you can run your test in two different ways. The console runs nunit-lele.exe, which is the fastest, but it is not inferior to running nunit.exe in graphic mode. A Win form application provides a graphical interface that allows you to control your test run more intuitively.

After the installation is complete, create a test project in your current project. It is different from the unit test project created in vs integrated test. nunit needs to create a class library project, then reference the project you want to test and nunit Framework Assembly nunit. framework. DLL, so that you can start your own test code.

 

Run and configure

Compile your test project, run nunit.exe, open the graphical interface, select your test DLL, and click run to get a very intuitive feedback without explanation.

When you run nunit.exe extends nunit-console.exe, all of the corresponding configuration files nunit.exe. config and nunit-console.exe.config control their running behavior. A major use of these configuration files is to determine the CLR version based on which nunit runs. Nunit compilation uses versions 1.1 and 2.0. These two versions provide separate downloads, but either of them can be based on other versions.

However, these configurations only do not affect your test project. If you want to control a project, see test. create test in the same directory as DLL. DLL. config configuration file. The format of your file should be as follows:

<?xml version="1.0" encoding="utf-8" ?><configuration>  <configSections>    <sectionGroup name="NUnit">      <section name="TestCaseBuilder"    type="System.Configuration.NameValueSectionHandler"/>      <section name="TestRunner"    type="System.Configuration.NameValueSectionHandler"/>    </sectionGroup>  </configSections>  <NUnit>    <TestCaseBuilder>      <add key="OldStyleTestCases" value="false" />    </TestCaseBuilder>    <TestRunner>      <add key="ApartmentState" value="MTA" />      <add key="ThreadPriority" value="Normal" />  <add key="DefaultLogThreshold" value="Error" /></TestRunner>  </NUnit>  ...</configuration> 

If the value of oldstyletestcases is true, nunit... The start method is test. The prefix is case-insensitive. Apartmentstate is used to set the unit status of the thread that runs the test. Threadpriority sets the priority of the test thread. The defaultlogthreshold setting is the error level captured by nunit for logging. This level must be defined in log4net.

For more running and configuration details, see: http://www.nunit.org/index.php? P = runningtests & R = 2.5.10

 

Example

For a simple example of unit test, paste an example in the official document. It is very representative. After reading it, you can draw a picture for a simple test.

Let us assume that a banking system has implemented the basic functions of saving money, taking money and transferring money.

namespace bank{  public class Account  {    private float balance;    public void Deposit(float amount)    {      balance+=amount;    }    public void Withdraw(float amount)    {      balance-=amount;    }    public void TransferFunds(Account destination, float amount)    {    }    public float Balance    {      get{ return balance;}    }  }}

 

Below is a simple test class:

namespace bank{  using NUnit.Framework;  [TestFixture]  public class AccountTest  {    [Test]    public void TransferFunds()    {      Account source = new Account();      source.Deposit(200.00F);      Account destination = new Account();      destination.Deposit(150.00F);      source.TransferFunds(destination, 100.00F);      Assert.AreEqual(250.00F, destination.Balance);      Assert.AreEqual(100.00F, source.Balance);    }  }}

As we can see, our accounttest class applies a testfixture feature, which indicates that our class contains the test code and can be inherited. It requires that the test class be made public without any constraints on the super class, And a default constructor is required.

Similarly, our test method transferfunds also applies the test feature, which indicates that our method is a test method. This test method must return void and have no parameters. In our test method, we need to initialize the required object, then run the code to be tested, and then verify the results and our expected results. We use assertions to test the results. This is a core feature of nuint and will be introduced in future articles. Here we use assert. areequal, which requires two parameters: the first is the expected result, and the second is the result of code execution.

In this case, the first example is complete and you can run it.

 

E good can see the Unit provided by the official classic example: http://www.nunit.org/index.php? P = Quickstart & R = 2.5.10

Of course there is also a corresponding Chinese version: http://www.36sign.com/nunit/quickStart.html

 

This article first writes this article, and will summarize the important features of nunit: assertions, constraints, and attributes features.

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.