Xunit
Attributes
Note: This table was written back when xUnit.net 1.0 has shipped, and needs to be updated with information regarding the latest versions of the unit testing frameworks.
NUnit 2.2 | MSTest 2005 | xUnit.net 2.x | Comments |
---|---|---|---|
[Test] |
[TestMethod] |
[Fact] |
Marks a test method. |
[TestFixture] |
[TestClass] |
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 . See Note 1 |
[SetUp] |
[TestInitialize] |
Constructor | We believe that use of [SetUp] is generally bad. However, you can implement a parameterless constructor as a direct replacement. See Note 2 |
[TearDown] |
[TestCleanup] |
IDisposable.Dispose |
We believe that use of [TearDown] is generally bad. However, you can implement IDisposable.Dispose as a direct replacement. See Note 2 |
[TestFixtureSetUp] |
[ClassInitialize] |
IClassFixture<T> |
To get per-class fixture setup, implement IClassFixture<T> on your test class. See Note 3 |
[TestFixtureTearDown] |
[ClassCleanup] |
IClassFixture<T> |
To get per-class fixture teardown, implement IClassFixture<T> on your test class. See Note 3 |
n/a | n/a | ICollectionFixture<T> |
To get per-collection fixture setup and teardown, implement ICollectionFixture<T> on your test collection. See Note 3 |
[Ignore] |
[Ignore] |
[Fact(Skip="reason")] |
Set the Skip parameter on the [Fact] attribute to temporarily skip a test. |
[Property] |
[TestProperty] |
[Trait] |
Set arbitrary metadata on a test |
n/a | [DataSource] |
[Theory] [XxxData] |
Theory (data-driven test). See Note 4 |
Attribute Notes
Note 1: Long-term use of [ExpectedException]
has uncovered various problems with it. First, it doesn’t specifically say which line of code should throw the exception, which allows subtle and difficult-to-track failures that show up as passing tests. Second, it doesn’t offer the opportunity to fully inspect details of the exception itself, since the handling is outside the normal code flow of the test. Assert.Throws
allows you to test a specific set of code for throwing an exception, and returns the exception during success so you can write further asserts against the exception instance itself.
Note 2: The xUnit.net team feels that per-test setup and teardown creates difficult-to-follow and debug testing code, often causing unnecessary code to run before every single test is run. For more information, see http://jamesnewkirk.typepad.com/posts/2007/09/why-you-should-.html.
Note 3: xUnit.net provides a new way to think about per-fixture data with the use of the IClassFixture<T>
and ICollectionFixture<T>
interfaces. The runner will create a single instance of the fixture data and pass it through to your constructor before running each test. All the tests share the same instance of fixture data. After all the tests have run, the runner will dispose of the fixture data, if it implements IDisposable
. For more information, see Shared Context.
Note 4: xUnit.net ships with support for data-driven tests call Theories. Mark your test with the [Theory]
attribute (instead of [Fact]
), then decorate it with one or more [XxxData]
attributes, including [InlineData]
and [MemberData]
. For more information, see Getting Started.
Assertions
Note: this table was written back when xUnit.net 1.0 has shipped, and needs to be updated with information regarding the latest versions of the unit testing frameworks.
NUnit 2.2 | MSTest 2005 | xUnit.net 1.x | 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 |
Contains |
Contains |
|
DoAssert |
n/a | n/a | |
n/a | DoesNotContain |
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") |
Greater |
n/a | n/a | xUnit.net alternative: Assert.True(x > y) |
Ignore |
Inconclusive |
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) |
IsAssignableFrom |
n/a | IsAssignableFrom |
|
IsEmpty |
n/a | Empty |
|
IsFalse |
IsFalse |
False |
|
IsInstanceOfType |
IsInstanceOfType |
IsType |
|
IsNaN |
n/a | n/a | xUnit.net alternative: Assert.True(double.IsNaN(x)) |
IsNotAssignableFrom |
n/a | n/a | xUnit.net alternative: 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 | xUnit.net alternative: Assert.True(x < y) |
n/a | n/a | NotInRange |
Ensures that a value is not in a given inclusive range |
n/a | n/a | Throws |
Ensures that the code throws an exact exception |
原文: Comparing xUnit.net to other frameworks
在.NET开发中的单元测试工具之(2)——xUnit.Net
Xunit的更多相关文章
- 舍弃Nunit拥抱Xunit
前言 今天与同事在讨论.Net下测试框架的时候,说到NUnit等大多数测试框架的SetUp以及TearDown方法并不是显得那么完美,所以在公司内部的项目中采用了Xunit框架.那么究竟是什么样的原因 ...
- 使用xUnit,EF,Effort和ABP进行单元测试(C#)
返回总目录<一步一步使用ABP框架搭建正式项目系列教程> 本篇目录 介绍 创建测试项目 准备测试基类 创建第一个测试 测试异常 在测试中使用仓储 测试异步方法 小结 介绍 在这篇博客中,我 ...
- xUnit入门一
看了下Nhibernate的入门Demo,感觉测试驱动开发会更效率.当然,你可能觉得不是还要额外编程单元测试代码吗?开发怎么会更效率? 一句话解释之,磨刀不误砍柴工. 那就开始入门吧 ~.~ 笔者使用 ...
- 使用Xunit进行单元测试
http://xunit.github.io/docs/getting-started-desktop.html 1. 新建一个类库项目 2. 通过NuGet引入xunit,Shouldly,xuni ...
- XUnit - Shared Context between Tests
原文 单元测试类通常都会有share setup和cleanup的相关代码.xUnit.net根据共享的范围提供了几种share setup和cleanup的方法. Constructor and D ...
- 将 xunit.runner.dnx 的 xml 输出转换为 Nunit 格式
由于目前 DNX 缺乏 XSLT 的转换能力,因此只能使用变通方法.具体参考这个链接 主要内容复制过来是: From @eriklarko on July 14, 2015 7:38 As a wor ...
- xUnit安装及注意事项
前言 对于单元测试,想必大家都已再熟悉不过了,同时单元测试的重要性也越发突出,在招聘中也特别强调单元测试,但是对于微软内置的单元测试还是太过于繁琐,于是都在寻找一种简洁并且更加轻量的测试工具.用的最多 ...
- 【记录】xUnit for vs2012/vs2013
关于 NUint 以及单元测试的相关内容,可以参考:[单元测试]NUint使用详解及Visual Studio配置. xUnit 是 NUint 的进化版本,使用方法和 NUint 类似,首先下载安装 ...
- 那些年用过的xUnit.net的简单用法
0x01 前言 单元测试,总是伴随着我们的开发过程,优劣自行google.当然呢,不排除有些公司是不做单元测试的, 但是呢,学多一点东西用来防身还是可以的. 0x02 简单的Demo 写个两数求和的方 ...
- [小北De编程手记] : Lesson 06 玩转 xUnit.Net 之 定义自己的FactAttribute
xUnit.Net本身提供了标记测试方法的标签Fact和Theory.在前面的文章<Lesson 02 玩转 xUnit.Net 之 基本UnitTest & 数据驱动>中,也对它 ...
随机推荐
- 【Ext.Net学习笔记】03:Ext.Net DirectEvents用法详解、DirectMethods用法详解
Ext.Net通过DirectEvents进行服务器端异步的事件处理.[Ext.Net学习笔记]02:Ext.Net用法概览.Ext.Net MessageBus用法.Ext.Net布局 中已经简单的 ...
- [cocos2dx] 让UIButton支持disable状态
摘要: 主要解决cocos2dx-2.2.2版本中, UIButton显示不了disable状态图的问题. 顺便, 理解了一下cocos2dx中UIWidget的渲染原理. 博客: http://ww ...
- JSP中文乱码问题《转》
之前总是碰到JSP页面乱码的问题,每次都是现在网上搜,然后胡乱改,改完也不明白原因. 这次正好作下总结,中文乱码就是因为编码不符,可能出现乱码有四个地方: 1 JSP编码乱码 2 HTML编码乱码 3 ...
- JavaWeb学习之Servlet(三)----Servlet的映射匹配问题、线程安全问题
[声明] 欢迎转载,但请保留文章原始出处→_→ 文章来源:http://www.cnblogs.com/smyhvae/p/4140529.html 一.Servlet映射匹配问题: 在第一篇文章中的 ...
- Unity3D面试题汇总
1.请描述游戏动画有哪几种,以及其原理. 2.alpha blend 工作原理 3.写光照计算中的diffuse的计算公式 4.lod是什么,优缺点是什么 5.两种阴影判断的方法工作原理 6.MipM ...
- Android TextView中文字通过SpannableString来设置超链接、颜色、字体等属性
在Android中,TextView是我们最常用的用来显示文本的控件. 一般情况下,TextView中的文本都是一个样式.那么如何对于TextView中各个部分的文本来设置字体,大小,颜色,样式,以及 ...
- android窗口泄漏,isInEditMode解决可视化编辑器无法识别自定义控件的问题
android窗口泄漏 在做项目是遇到这个错误:google:WindowManager: Activity has leaked window. 产 生原因:我们知道Android的每一个Activ ...
- Android优化——UI优化(四) 使用stytle
使用style替换背景,防止Activity黑色背景闪过 1.原来的布局 <LinearLayout xmlns:android="http://schemas.android.com ...
- 工作流模式与K2实现--(2)
结构化过程 这两个模式的共同点在于:模式所涉及流程的执行路径是由运行时决定的,而非设计时确定.包括:Arbitrary cycles(强制循环模式) .Implicit termination( ...
- 本地环境,Ecshop安装教程
最近有个项目需要用ECshop来做,之前没接触过ECshop,今天去网上找了下安装教程,现在发出来分享一下. 1. ecshop网店系统最新版本是ECSHOP V2.7.3,去官网下载utf8和gbk ...