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
has done away with the ExpectedException attribute in favor of |
|
|
Constructor |
We
believe that use of |
|
|
|
We
believe that use of |
|
|
|
To get
per-class fixture setup, implement |
|
|
|
To get
per-class fixture teardown, implement |
n/a |
n/a |
|
To get
per-collection fixture setup and teardown, implement |
|
|
|
Set the
Skip parameter on the |
|
|
|
Set arbitrary metadata on a test |
n/a |
|
|
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 |
|
|
|
MSTest and xUnit.net support generic versions of this method |
|
|
|
MSTest and xUnit.net support generic versions of this method |
|
|
|
|
|
|
|
|
|
|
|
|
|
n/a |
n/a |
|
n/a |
|
|
|
n/a |
n/a |
|
Ensures that the code does not throw any exceptions |
|
|
n/a |
xUnit.net
alternative: |
|
n/a |
n/a |
xUnit.net
alternative: |
|
|
n/a |
|
n/a |
n/a |
|
Ensures
that a value is in a given inclusive range (note: NUnit and MSTest have
limited support for |
|
n/a |
|
|
|
n/a |
|
|
|
|
|
|
|
|
|
|
|
n/a |
n/a |
xUnit.net
alternative: |
|
n/a |
n/a |
xUnit.net
alternative: |
|
n/a |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
n/a |
n/a |
xUnit.net
alternative: |
n/a |
n/a |
|
Ensures that a value is not in a given inclusive range |
n/a |
n/a |
|
Ensures that the code throws an exact exception |
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")]