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

 

 

出处:https://www.cnblogs.com/gaochundong/p/mstest_nunit_xunit_comparisons.html

posted on   jack_Meng  阅读(157)  评论(0编辑  收藏  举报

编辑推荐:
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
阅读排行:
· DeepSeek “源神”启动!「GitHub 热点速览」
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· NetPad:一个.NET开源、跨平台的C#编辑器
历史上的今天:
2020-10-13 bat中如何处理参数(文件路径)的引号

导航

< 2025年2月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 1
2 3 4 5 6 7 8
点击右上角即可分享
微信分享提示

喜欢请打赏

扫描二维码打赏

支付宝打赏

主题色彩