由於越來越多人開始都改使用 NUnit 與 xUnit ,所以練習一下創建一個 xUnit 測試專案,以備不時之需。
還無緣使用 Net Core ,所以先在 NET Framework 專案練習。
- 建立 Class Library 專案

2. Nuget 引用 xUnit

3. Class1 替換程式碼測試( 此時測試還不會有反應 )
using Xunit;
namespace XNuitTest_Framework
{
public class Class1
{
[Fact]
public void Add_5_And_2_ShouldBe_7()
{
//Arrange
int x = 5, y = 2;
//Act
int actual = Add(x, y);
//Assert
Assert.Equal(7, actual);
}
[Fact]
public void Add_5_And_5_Fail()
{
//Arrange
int x = 5, y = 5;
//Act
int actual = Add(x, y);
//Assert
Assert.Equal(7, actual);
}
int Add(int x, int y)
{
return x + y;
}
}
}
4. 在 Nuget 引用 xunit.runner.console

此時可以使用命令列的方式來執行測試
在 Visual Studio 找到 Command 或 PowerShell

執行命令列 : (專案名稱路徑與 自行替換)
packages\xunit.runner.console.2.4.1\tools\net47\xunit.console XNuitTest_Framework\bin\Debug\XNuitTest_Framework.dll

5. 在 Nuget 引用 xunit.runner.visualstudio

此時可以使用 Test Explorer 進行測試


