Unit test tool in. Net Development (2) -- xunit. net

Source: Internet
Author: User
Tags comparison table xunit

In the previous article. (1) -- nunit describes how to use nunit in. NET development. net Development in the unit test and some shortcomings of nunit, today will show how to use xunit.. net.

Xunit. Net Introduction

The creators of xunit.net are Jim Newkirk and Brad Wilson, who have summed up a new framework from their experiences including nunit and other unit test frameworks. Compared with nunit, xunit.net has the following features:

Generate an object instance for each test method

[Setup] and [teardown] are canceled.

[Expectedexception] canceled

Functions similar to Aspect

Reduced the number of Custom Attributes

Generic

Anonymous Delegation

Extensible assertions

Scalable testing methods

Scalable test classes

The official website of xunit.net is: http://xunit.codeplex.com, below is the run interface of xunit.net:

Note that the downloaded xunit.net package contains four EXE files that support GUI running:

Xunit.gui.clr4.exe: Used to run xunit.net in x64 and. net4.0.

Xunit.gui.clr4.x86.exe: Used to run xunit.net in x86 and. net4.0.

Xunit.gui.exe: Used to run xunit.net in x64 and. net4.0 or earlier versions.

Xunit.gui.x86.exe: Used to run xunit.net in x86 and. net4.0 or earlier versions.

Xunit. Net download and Installation

Common attribute tags of xunit. net

If you have used nunit or mstest testing tool provided by vs before, the following comparison table will make it easy to get started with xunit.net:

Nunit 2.2

Mstest

Xunit.net

Remarks

[Test]

[Testmethod]

[Fact]

Mark as test method

[Testfixture]

[Testclass]

N/

Contains classes with test methods. xunit.net does not need to be marked. it searches for all public test methods in the Assembly.

[Expectedexception]

[Expectedexception]

Assert. Throws/record. Exception

Expected to throw an exception

[Setup]

[Testinitialize]

Constructor (constructor)

Method used for initialization before each test method is executed

[Teardown]

[Testcleanup]

Idisposable. Dispose

Method used to end after each test method is executed

[Testfixturesetup]

[Classinitialize]

Iusefixture <t>

The method used for initialization before all test methods are executed.

[Testfixtureteardown]

[Classcleanup]

Iusefixture <t>

Method used to end after all test methods are executed

[Ignore]

[Ignore]

[Fact (skip = "reason")]

Temporarily ignore tagged Methods

N/

[Timeout]

[Fact (timeout = n)]

Specifies the maximum execution time (in milliseconds) of the method to be tested. If the execution time exceeds the specified time, it is marked as a test failure.

[Property]

[Testproperty]

[Trait]

Set arbitrary metadata on a test

N/

[Datasource]

[Theory], [xxxdata]

Assertions)

The following table compares nunit, mstest, and xunit. Net assertions.

Nunit 2.2

Mstest

Xunit.net

Remarks

Areequal

Areequal

Equal

Equal comparison

Arenotequal

Arenotequal

Notequal

Unequal comparison

Arenotsame

Arenotsame

Notsame

Different comparison

Aresame

Aresame

Same

Same comparison

Contains

Contains (on collectionassert)

Contains

Doassert

N/

N/

N/

Doesnotcontain (on collectionassert)

Doesnotcontain

N/

N/

Doesnotthrow

Fail

Fail

N/

Can be replaced by assert. True (false, "message ")

Greater

N/

N/

Can be replaced by assert. True (x> Y ).

Ignore

Inconclusive

N/

N/

N/

Inrange

Isassignablefrom

N/

Isassignablefrom

Isempty

N/

Empty

Isfalse

Isfalse

False

Isinstanceoftype

Isinstanceoftype

Istype

Isnan

N/

N/

Can be replaced by assert. True (double. isnan (x ).

Isnotassignablefrom

N/

N/

Can be replaced by assert. False (obj is type ).

Isnotempty

N/

Notempty

Isnotinstanceoftype

Isnotinstanceoftype

Isnottype

Isnotnull

Isnotnull

Notnull

Isnull

Isnull

Null

Istrue

Istrue

True

Less

N/

N/

Can be replaced by assert. True (x <Y ).

N/

N/

Notinrange

Make sure that the data is within a certain range

N/

N/

Throws

Make sure that an exception is thrown.

Xunit. Net project file structure

Because xunit. NET is not as good as nunit in terms of visualization, it is necessary to introduce the components of its project file. xunit .. Xunit is actually an XML file. Its root node is <xunit>, <xunit> has <assemblies> subnodes, and <assemblies> can have multiple <Assembly> nodes. <Assembly> A node has the following attributes:

Filename: This is a required attribute to specify the absolute or relative path of the file contained in the project.

Config-filename: This is a non-mandatory attribute. It is used to specify the config file used for testing. The default value is none, indicating that no configuration file is applicable.

Shadow-copy: whether to perform shadow-copy on the DLL during the test. The default value is true. The effect of true/false on the program is unknown.

The following is an example. The example indicates the config file used in the test:

<?xml version="1.0" encoding="utf-8"?><xunit>  <assemblies>    <assembly filename="bin\Debug\XunitDemo.exe" config-filename="bin\Debug\XunitDemo.exe.config" shadow-copy="true"/>    <assembly filename="bin\Debug\xunit.dll" shadow-copy="true" />  </assemblies></xunit>

Use of xunit. net

Xunit. the common usage of net is very simple. For the nunit owner, it is easy to master the common usage of xunit.net. The following is a simple example (adding the config file and configuring it accordingly, for details, see one of the series ):

Using system; using xunit; using system. configuration; namespace xunitdemo {public class xunitdemo: idisposable {public xunitdemo () {// You can initialize the system before the test starts. console. writeline ("init");} [fact] public void testadd () {assert. equal <int> (5, 2 + 3);} [fact (timeout = 900)] // specify the timeout value to 900 ms public void testtimeout () {system. threading. thread. sleep (1000); assert. inrange <double> (new random (). nextdouble () * 10, 5, 10);} [fact] public void test0_51ctoblog () {// assert is case insensitive. equal <bool> (true, String. equals (configurationmanager. appsettings ["51 ctoblog"], "http://zhoufoxcn.blog.51cto.com", stringcomparison. invariantcultureignorecase);} [fact] public void test0_csdnblog () {assert. equal <string> (configurationmanager. deleetask[ "csdnblog"], "http://blog.csdn.net/zhoufoxcn");} [fact] public void test0_sinaweibo () {assert. equal <string> (configurationmanager. appsettings ["sinaweibo"], "http://weibo.com/zhoufoxcn");} public void dispose () {// here you can do the final work after the test is completed system. console. writeline ("dispose ");}}}

The program running effect is as follows:

Summary

As nunit release, xunit. net does overcome many shortcomings of nunit (for the shortcomings and shortcomings of nunit, see the previous article in. (1) -- nunit), which is a unit test tool in net development. Compared with the assert API of nunit, xunit. net's assert is more streamlined but sufficient to meet the needs of unit testing. In contrast, nunit's assert API is slightly bloated (this may be the same as it does. net1.1 has been supported and requires backward compatibility), but xunit is used in the ease of use of the GUI. net is not as good as nunit. nunit GUI provides many configuration interfaces so that the configuration can be completed through the interface settings, but the same work is done in xunit. net, you must configure nodes in the project file (for example, specify the config file ).

Renewal interface:

Zhou Jinqiao

2013-04-06

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.