1.若是以前安裝了xUnit.net Visual Studio Runner擴展包,經過"工具"菜單下的"擴展和更新"先將該擴展包卸載。html
2.刪除臨時目錄中的指定文件夾:%TEMP%\VisualStudioTestExplorerExtensionsgit
安裝Xunit:github
Xunit的安裝如今不須要插件支持了,直接使用NuGet安裝以下兩個庫便可:瀏覽器
• PM> Install-Package xunitless
• PM> Install-Package xunit.runner.visualstudio -Pre (Visual Studio測試瀏覽器支持, VS2015目前必須裝Pre的)ide
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 2005post |
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 |
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.
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 |
Xunit裏面不須要TestClass之類Attribute來標記測試用例類,只須要知足以下條件便可:
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函數,更加簡潔明瞭。也就是說:
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")]