首页 > 技术文章 > .NET开发单元测试工具 【xUnit】

gangle 2018-07-26 08:46 原文

xUnit.Net介绍

xUnit.net是其创造者Jim Newkirk和Brad Wilson从包括NUnit及其它单元测试框架的使用经验中总结出来的一个新框架,相比于NUnit,xUnit.net有如下特点:

  • 为每个测试方法产生一个对象实例
  • 取消了[SetUp]和[TearDown]
  • 取消了[ExpectedException]
  • 类似于Aspect的功能
  • 减少了自定义属性(Attribute)的数目
  • 采用泛型
  • 匿名委托
  • 可扩展的断言
  • 可扩展的测试方法
  • 可扩展的测试类

 

xUnit.Net下载与安装

官方网站:https://github.com/xunit/xunit 

学习文档:https://xunit.github.io/

运行界面: 

               

在下载的xUnit.net压缩包内有4个支持GUI方式运行的exe文件,分别是:

  • xunit.gui.clr4.exe:用于在x64及.Net4.0下运行xUnit.net。
  • xunit.gui.clr4.x86.exe:用于在x86及.Net4.0下运行xUnit.net。
  • xunit.gui.exe:用于在x64及.Net4.0以下版本运行xUnit.net。
  • xunit.gui.x86.exe:用于在x86及.Net4.0以下版本运行xUnit.net。

 

xUnit.Net的常用Attribute标记

下面是与NUnit和VS自带的MSTest测试工具的对比:

NUnit 2.2

MSTest

xUnit.net

备注

[Test]

[TestMethod]

[Fact]

标记为测试方法

[TestFixture]

[TestClass]

n/a

包含有测试方法的类,在xUnit.net中无需标记,它会查找程序集中所有的public的测试方法

[ExpectedException]

[ExpectedException]

Assert.Throws/ Record.Exception

期望抛出异常

[SetUp]

[TestInitialize]

Constructor(即构造函数)

在每个测试方法执行之前用于初始化的方法

[TearDown]

[TestCleanup]

IDisposable.Dispose

在每个测试方法执行之后用于结束的方法

[TestFixtureSetUp]

[ClassInitialize]

IUseFixture<T>

在所有测试方法执行之前用于初始化的方法

[TestFixtureTearDown]

[ClassCleanup]

IUseFixture<T>

在所有测试方法执行之后用于结束的方法

[Ignore]

[Ignore]

[Fact(Skip="reason")]

临时忽略被标记的方法

n/a

[Timeout]

[Fact(Timeout=n)]

用于指定被测试方法的最大执行时间(单位毫秒),如果超过指定时间则会被标记为测试失败

[Property]

[TestProperty]

[Trait]

Set arbitrary metadata on a test

n/a

[DataSource]

[Theory], [XxxData]

 

 

xUnit.Net的断言(Assertions

下面是一个关于NUnitMSTest及xUnit.Net断言的对比: 

NUnit 2.2

MSTest

xUnit.net

备注

AreEqual

AreEqual

Equal

相等比较

AreNotEqual

AreNotEqual

NotEqual

不相等比较

AreNotSame

AreNotSame

NotSame

不相同比较

AreSame

AreSame

Same

相同比较

Contains

Contains (on CollectionAssert)

Contains

 

DoAssert

n/a

n/a

 

n/a

DoesNotContain (on CollectionAssert)

DoesNotContain

 

n/a

n/a

DoesNotThrow

 

Fail

Fail

n/a

可用Assert.True(false, "message")替代

Greater

n/a

n/a

可用Assert.True(x > y)替代

Ignore

Inconclusive

n/a

 

n/a

n/a

InRange

 

IsAssignableFrom

n/a

IsAssignableFrom

 

IsEmpty

n/a

Empty

 

IsFalse

IsFalse

False

 

IsInstanceOfType

IsInstanceOfType

IsType

 

IsNaN

n/a

n/a

可用Assert.True(double.IsNaN(x))替代

IsNotAssignableFrom

n/a

n/a

可用Assert.False(obj is Type)替代

IsNotEmpty

n/a

NotEmpty

 

IsNotInstanceOfType

IsNotInstanceOfType

IsNotType

 

IsNotNull

IsNotNull

NotNull

 

IsNull

IsNull

Null

 

IsTrue

IsTrue

True

 

Less

n/a

n/a

可用Assert.True(x < y)替代

n/a

n/a

NotInRange

确保数据在某个范围内

n/a

n/a

Throws

确保会抛出异常

 

 

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

 

 

 

 

 

 

xUnit.Net的项目文件结构

xunit实际上也是一个xml文件,它的根节点是<xunit>,<xunit>有<assemblies>子节点,<assemblies>下可以有多个<assembly>节点。<assembly>节点包含以下属性:

  • filename:这是必须属性,用于指定包含在项目中的绝对或者相对路径的文件。
  • config-filename:这个是非必须属性,用于指定测试时所使用的config文件,默认是none,表示不适用任何配置文件。
  • shadow-copy:运行测试时是否对dll进行shadow-copy,默认是true,这个咱还不清楚true/false对程序的影响。

下面就是一个例子,在例子中执行指明了测试中使用的config文件:

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

 

xUnit.Net的使用

xUnit.Net的常见用法很简单,下面就是一个简单例子

 1 using System;
 2 using Xunit;
 3 using System.Configuration;
 4 namespace XunitDemo
 5 {
 6     public class XunitDemo:IDisposable
 7     {
 8         public XunitDemo()
 9         {
10             //在这里可以做测试开始前的初始化工作
11             System.Console.WriteLine("Init");
12         }
13         [Fact]
14         public void TestAdd()
15         {
16             Assert.Equal<int>(5, 2 + 3);
17         }
18         [Fact(Timeout=900)]//指定超时为900ms
19         public void TestTimeout()
20         {
21             System.Threading.Thread.Sleep(1000);
22             Assert.InRange<double>(new Random().NextDouble()*10,5,10);
23         }
24         [Fact]
25         public void Test0_51CTOBlog()
26         {
27             //不区分大小写等值判断
28             Assert.Equal<bool>(true,string.Equals(ConfigurationManager.AppSettings["51ctoBlog"],
29                     "http://gangle.blog.51cto.com", StringComparison.InvariantCultureIgnoreCase);
30         }
31         [Fact]
32         public void Test0_CNBlog()
33         {
34             Assert.Equal<string>(ConfigurationManager.AppSettings["CNBlog"], 
35                     "http://www.cnblogs.com/gangle/");
36         }
37         [Fact]
38         public void Test0_SinaWeiBo()
39         {
40             Assert.Equal<string>(ConfigurationManager.AppSettings["SinaWeiBo"], 
41                     "http://weibo.com/gangle");
42         }
43         public void Dispose()
44         {
45             //在这里可以做测试结束后的收尾工作
46             System.Console.WriteLine("Dispose");
47         }
48     }
49 }

 

xUnit.Net对VS的支持

可以使用xUnit.Net同一目录下的xunit.installer.exe来配置对VS的支持。

 

 

 

 
*****************************
 *** Keep learning and growing. ***
 *****************************

推荐阅读