MSTest、NUnit、xUnit.net 属性和断言对照表

简介:
MSTest、NUnit、xUnit.net 属性对照表
MSTest NUnit xUnit.net Comments
[TestMethod] [Test] [Fact]

Marks a test method.

[TestClass] [TestFixture] n/a

xUnit.net does not require an attribute for a test class; it looks for all test methods in all public (exported) classes in the assembly.

[ExpectedException] [ExpectedException]

Assert.Throws

Record.Exception

xUnit.net has done away with the ExpectedException attribute in favor of Assert.Throws.

[TestInitialize] [SetUp] Constructor

We believe that use of [SetUp]is generally bad. However, you can implement a parameterless constructor as a direct replacement. 

[TestCleanup] [TearDown] IDisposable.Dispose

We believe that use of[TearDown] is generally bad. However, you can implementIDisposable.Dispose as a direct replacement.

[ClassInitialize] [TestFixtureSetUp] IUseFixture<T>

To get per-fixture setup, implement IUseFixture<T> on your test class.

[ClassCleanup] [TestFixtureTearDown] IUseFixture<T>

To get per-fixture teardown, implement IUseFixture<T> on your test class. 

[Ignore] [Ignore] [Fact(Skip="reason")]

Set the Skip parameter on the[Fact] attribute to temporarily skip a test.

[Timeout] [Timeout] [Fact(Timeout=n)]

Set the Timeout parameter on the [Fact] attribute to cause a test to fail if it takes too long to run. Note that the timeout value for xUnit.net is in milliseconds.

[TestCategory]

[Category]

[Trait]  
[TestProperty] [Property] [Trait]

Set arbitrary metadata on a test

[DataSource] n/a [Theory], [XxxData]

Theory (data-driven test).

MSTest、NUnit、xUnit.net 断言对照表
MSTest NUnit xUnit.net Comments
AreEqual AreEqual Equal

MSTest and xUnit.net support generic versions of this method

AreNotEqual AreNotEqual NotEqual

MSTest and xUnit.net support generic versions of this method

AreNotSame AreNotSame

NotSame

 
AreSame AreSame

Same

 

Contains 

(on CollectionAssert)

Contains

Contains

 
n/a DoAssert

n/a

 

DoesNotContain 

(on CollectionAssert)

n/a

DoesNotContain

 
n/a n/a DoesNotThrow

Ensures that the code does not throw any exceptions

Fail Fail n/a

xUnit.net alternative:

Assert.True(false, "message")

n/a Pass n/a  
n/a Greater n/a

xUnit.net alternative: 

Assert.True(x > y)

n/a

GreaterOrEqual

n/a  
Inconclusive Ignore

n/a

 
n/a n/a InRange

Ensures that a value is in a given inclusive range (note: NUnit and MSTest have limited support for InRange on their AreEqual methods)

n/a IsAssignableFrom

IsAssignableFrom

 
n/a IsEmpty

Empty

 
IsFalse IsFalse

False

 
IsInstanceOfType IsInstanceOfType

IsType

 
n/a IsNaN n/a

xUnit.net alternative:

Assert.True(double.IsNaN(x))

n/a IsNotAssignableFrom n/a

xUnit.net alternative: 

Assert.False(obj is Type);

n/a IsNotEmpty

NotEmpty

 
IsNotInstanceOfType IsNotInstanceOfType

IsNotType

 
IsNotNull IsNotNull

NotNull

 
IsNull IsNull

Null

 
IsTrue IsTrue

True

 
n/a Less n/a

xUnit.net alternative: 

Assert.True(x < y)

n/a

LessOrEqual

n/a  
n/a n/a

NotInRange

Ensures that a value is not in a given inclusive range

n/a Throws Throws

Ensures that the code throws an exact exception

n/a

IsAssignableFrom

n/a  
n/a IsNotAssignableFrom

n/a

 

参考资料

  1. Using Traits with different test frameworks in the Unit Test Explorer
  2. Testing Framework Comparision with xUnit.net




本文转自匠心十年博客园博客,原文链接:http://www.cnblogs.com/gaochundong/p/mstest_nunit_xunit_comparisons.html,如需转载请自行联系原作者



目录
相关文章
|
API
.net core工具组件系列之Autofac—— 第二篇:Autofac的3种依赖注入方式(构造函数注入、属性注入和方法注入),以及在过滤器里面实现依赖注入
本篇文章接前一篇,建议可以先看前篇文章,再看本文,会有更好的效果。前一篇跳转链接:https://www.cnblogs.com/weskynet/p/15046999.html
1023 0
.net core工具组件系列之Autofac—— 第二篇:Autofac的3种依赖注入方式(构造函数注入、属性注入和方法注入),以及在过滤器里面实现依赖注入
|
12月前
|
测试技术 API 开发者
精通.NET单元测试:MSTest、xUnit、NUnit全面解析
【10月更文挑战第15天】本文介绍了.NET生态系统中最流行的三种单元测试框架:MSTest、xUnit和NUnit。通过示例代码展示了每种框架的基本用法和特点,帮助开发者根据项目需求和个人偏好选择合适的测试工具。
420 3
|
测试技术 API 开发者
.NET单元测试框架大比拼:MSTest、xUnit与NUnit的实战较量与选择指南
【8月更文挑战第28天】单元测试是软件开发中不可或缺的一环,它能够确保代码的质量和稳定性。在.NET生态系统中,MSTest、xUnit和NUnit是最为流行的单元测试框架。本文将对这三种测试框架进行全面解析,并通过示例代码展示它们的基本用法和特点。
1363 8
|
API 开发者 Java
API 版本控制不再难!Spring 框架带你玩转多样化的版本管理策略,轻松应对升级挑战!
【8月更文挑战第31天】在开发RESTful服务时,为解决向后兼容性问题,常需进行API版本控制。本文以Spring框架为例,探讨四种版本控制策略:URL版本控制、请求头版本控制、查询参数版本控制及媒体类型版本控制,并提供示例代码。此外,还介绍了通过自定义注解与过滤器实现更灵活的版本控制方案,帮助开发者根据项目需求选择最适合的方法,确保API演化的管理和客户端使用的稳定与兼容。
600 0
|
SQL 开发框架 .NET
(20)ASP.NET Core EF创建模型(必需属性和可选属性、最大长度、并发标记、阴影属性)
(20)ASP.NET Core EF创建模型(必需属性和可选属性、最大长度、并发标记、阴影属性)
|
存储 开发框架 自然语言处理
【.Net底层剖析】3.用IL来理解属性
【.Net底层剖析】3.用IL来理解属性
238 0
【.Net底层剖析】3.用IL来理解属性
|
JSON 物联网 网络性能优化
NET Core 跨平台物联网开发 SDK属性、方法、委托、类(四)
NET Core 跨平台物联网开发 SDK属性、方法、委托、类(四)
392 1
|
存储 JSON 物联网
.NET Core 跨平台物联关网开发:上报属性(三)
.NET Core 跨平台物联关网开发:上报属性(三)
274 0
.NET Core 跨平台物联关网开发:上报属性(三)

热门文章

最新文章