xUnit随笔
XUnit入门
1.如果之前安装了xUnit.net Visual Studio Runner扩展包,通过"工具"菜单下的"扩展和更新"先将该扩展包卸载。
2.删除临时目录中的指定文件夹:%TEMP%\VisualStudioTestExplorerExtensions
安装Xunit:
Xunit的安装现在不需要插件支持了,直接使用NuGet安装如下两个库即可:
• PM> Install-Package xunit
• PM> Install-Package xunit.runner.visualstudio -Pre (Visual Studio测试浏览器支持, VS2015目前必须装Pre的)
Comparing xUnit.net to other frameworks
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 |
|
|
|
|
Marks a test method. |
|
|
|
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. |
|
|
|
|
xUnit.net |
|
|
|
Constructor |
We |
|
|
|
|
We |
|
|
|
|
To get |
|
|
|
|
To get |
|
n/a |
n/a |
|
To get |
|
|
|
|
Set the |
|
|
|
|
Set |
|
n/a |
|
|
Theory |
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 |
MSTest |
xUnit.net |
Comments |
|
|
|
|
MSTest |
|
|
|
|
MSTest |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
n/a |
n/a |
|
|
n/a |
|
|
|
|
n/a |
n/a |
|
Ensures |
|
|
|
n/a |
xUnit.net |
|
|
n/a |
n/a |
xUnit.net |
|
|
|
n/a |
|
|
n/a |
n/a |
|
Ensures |
|
|
n/a |
|
|
|
|
n/a |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
n/a |
n/a |
xUnit.net |
|
|
n/a |
n/a |
xUnit.net |
|
|
n/a |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
n/a |
n/a |
xUnit.net |
|
n/a |
n/a |
|
Ensures |
|
n/a |
n/a |
|
Ensures |
Copyright © 2015 Outercurve Foundation. Contributions
welcomed at https://github.com/xunit/xunit.github.io.
测试方式说明:
声明测试用例:
Xunit里面不需要TestClass之类Attribute来标记测试用例类,只需要满足如下条件即可:
- 测试类必须是public的
- 测试用例用
FactAttribute 标记
断言:
Assert类用来验证测试测试函数的输出结果。
Assert.Equal(3, Math.Max(3, 2));
也可以使用一些扩展的断言库,常用的就是xunit.should库,它是以扩展函数的方式进行验证,看起来更加舒服。
PM> Install-Package xunit.should
Math.Max(3, 2).ShouldBe(3);
构建和析构:
Xunit里面并不是通过SetUp和TearDown标记来表明测试用例的构建和析构操作,它每次执行测试用例的时候都会插件测试用例类,执行完成后,如果其实现了IDispose接口,则会调用Dispose函数,更加简洁明了。也就是说:
- 在测试用例类的构造函数指向数据构建操作,
- 在Dispose函数中指向数据清理操作
异常测试
Xunit并不是通过Attribute来标记异常捕获的,而是直接使用Assert.Throws断言函数来验证异常。
public class TestClass1
{
[ Fact]
public void testException()
{
Assert.Throws<InvalidOperationException>(()
=> operation());
}
void operation()
{
throw
new InvalidOperationException();
}
}
更改测试用例名称:
[ Fact(DisplayName = "Max 函数测试 " )]
跳过测试用例:
[ Fact(Skip = " 重构未完成 " )]
分组:
[ Trait("Group", "Category")]
xUnit随笔的更多相关文章
- AI人工智能系列随笔
初探 AI人工智能系列随笔:syntaxnet 初探(1)
- 【置顶】CoreCLR系列随笔
CoreCLR配置系列 在Windows上编译和调试CoreCLR GC探索系列 C++随笔:.NET CoreCLR之GC探索(1) C++随笔:.NET CoreCLR之GC探索(2) C++随笔 ...
- C++随笔:.NET CoreCLR之GC探索(4)
今天继续来 带大家讲解CoreCLR之GC,首先我们继续看这个GCSample,这篇文章是上一篇文章的继续,如果有不清楚的,还请翻到我写的上一篇随笔.下面我们继续: // Initialize fre ...
- C++随笔:从Hello World 探秘CoreCLR的内部(1)
紧接着上次的问题,上次的问题其实很简单,就是HelloWorld.exe运行失败,而本文的目的,就是成功调试HelloWorld这个控制台应用程序. 通过我的寻找,其实是一个名为TryRun的文件出了 ...
- 舍弃Nunit拥抱Xunit
前言 今天与同事在讨论.Net下测试框架的时候,说到NUnit等大多数测试框架的SetUp以及TearDown方法并不是显得那么完美,所以在公司内部的项目中采用了Xunit框架.那么究竟是什么样的原因 ...
- ASP.NET MVC 系列随笔汇总[未完待续……]
ASP.NET MVC 系列随笔汇总[未完待续……] 为了方便大家浏览所以整理一下,有的系列篇幅中不是很全面以后会慢慢的补全的. 学前篇之: ASP.NET MVC学前篇之扩展方法.链式编程 ASP. ...
- 使用xUnit,EF,Effort和ABP进行单元测试(C#)
返回总目录<一步一步使用ABP框架搭建正式项目系列教程> 本篇目录 介绍 创建测试项目 准备测试基类 创建第一个测试 测试异常 在测试中使用仓储 测试异步方法 小结 介绍 在这篇博客中,我 ...
- 使用Beautiful Soup编写一个爬虫 系列随笔汇总
这几篇博文只是为了记录学习Beautiful Soup的过程,不仅方便自己以后查看,也许能帮到同样在学习这个技术的朋友.通过学习Beautiful Soup基础知识 完成了一个简单的爬虫服务:从all ...
- 利用Python进行数据分析 基础系列随笔汇总
一共 15 篇随笔,主要是为了记录数据分析过程中的一些小 demo,分享给其他需要的网友,更为了方便以后自己查看,15 篇随笔,每篇内容基本都是以一句说明加一段代码的方式, 保持简单小巧,看起来也清晰 ...
随机推荐
- SVN CommandLine
要是SVN命令行用不了的话: 1.SVN装了没? 2.SVN安装时,选项“command line client tools”选了没. 检出: svn checkout [-depth ARG] [- ...
- 洛谷 3106 [USACO14OPEN]GPS的决斗Dueling GPS's 3720 [AHOI2017初中组]guide
[题解] 这两道题是完全一样的. 思路其实很简单,对于两种边权分别建反向图跑dijkstra. 如果某条边在某一种边权的图中不是最短路上的边,就把它的cnt加上1.(这样每条边的cnt是0或1或2,代 ...
- lua报错,看到报错信息有tail call,以为和尾调用有关,于是查了一下相关知识
尾调用是指在函数return时直接将被调函数的返回值作为调用函数的返回值返回,尾调用在很多语言中都可以被编译器优化, 基本都是直接复用旧的执行栈, 不用再创建新的栈帧, 原理上其实也很简单, 因为尾调 ...
- nyoj_758_分苹果
分苹果 时间限制:1000 ms | 内存限制:65535 KB 难度:2 描述 把M个同样的苹果放在N个同样的盘子里,允许有的盘子空着不放,问共有多少种不同的分法? (注意:假如有3个盘子7 ...
- UVA 1347_Tour
题意: 给定一系列按x坐标升序排列的点,一个人从左向右走到终点再从终点走回起点,要求每个点恰好经过一次,问所走过的最短路径长度. 分析: 可以看成是两个人同时从起点向终点走,且除起点终点外每个点恰有一 ...
- Delphi XE4 inline 的用法
为了提高应用程序的性能,可以用inline声明一个函数过过程,对于声明为inline的过程,编译器在编译时,会取代正常的过程调用方式,取而代之是直接把这个函数的编译代码拿过来生成到调用该过程的地方,有 ...
- [bzoj5101][POI2018]Powódź_并查集
Powódź bzoj-5101 POI-2018 题目大意:在地面上有一个水箱,它的俯视图被划分成了$n$行$m$列个方格,相邻两个方格之间有一堵厚度可以忽略不计的墙,水箱与外界之间有一堵高度无穷大 ...
- 百度UEditor富文本上传图片
项目中使用UEditor发现设置图片自定义保存路径会出现<请求后台配置项http错误,上传功能将不能正常使用!错误> /* 上传图片配置项 */ "imageActionName ...
- HttpClientUtil 工具类 实现跨域请求数据
package com.xxx.common.util; import java.io.IOException; import java.net.URI; import java.util.Array ...
- Python学习系列之反射
反射的定义 根据字符串的形式去某个对象中操作成员 根据字符串的形式去某个对象中寻找成员 根据字符串的形式去某个对象中设置成员 根据字符串的形式去某个对象中删除成员 根据字符串的形式去某个对象中判断成员 ...