.NET開發單元測試工具 【xUnit】


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. ***
 *****************************


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM