使用Xunit进行单元测试

时间:2023-03-09 17:24:45
使用Xunit进行单元测试

http://xunit.github.io/docs/getting-started-desktop.html

1. 新建一个类库项目

2. 通过NuGet引入xunit,Shouldly,xunit.runner.visualstudio三个程序包。

3. 编写代码

public class Class1
{
public int Add(int x, int y)
{
return x + y;
} public string Reverse(string str)
{
return new string(str.Reverse().ToArray());
}
}

  

public class Class1Tests
{
[Fact]
public void AddTest()
{
var class1 = new Class1();
class1.Add(2, 2).ShouldBe(4);
} [Fact]
public void ReverseTest()
{
var class1 = new Class1();
class1.Reverse("hello").ShouldBe("olleh");
}
[Fact]
public void ReverseWithNull_Test()
{
var class1 = new Class1();
class1.Reverse(null).ShouldBe(null);
}
}

  

4. 在测试方法上右键执行

使用Xunit进行单元测试

——————————————————————

ps:测试管理资源器打开方式

使用Xunit进行单元测试

使用Xunit进行单元测试

附: VS插件

——————

.Net Core项目中使用 Xunit进行单元测试  

.NET Core系列 :4 测试

{
"version": "1.0.0-*",
"testRunner": "xunit",
"dependencies": { "xunit":"2.2.0-beta4-build3444",
"Shouldly": "2.8.2",
"xunit.runner.visualstudio": "2.2.0-beta4-build1194",
"dotnet-test-xunit": "2.2.0-preview2-build1029"
}, "frameworks": {
"net452":{}
}
}

版本兼容

{
"version": "1.0.0-*",
"testRunner": "xunit", "dependencies": {
"NSubstitute": "2.0.3",
"Shouldly": "2.8.3",
"xunit": "2.2.0",
"xunit.runner.visualstudio": "2.2.0"
},
"runtimes": {
"win10-x64": {}
},
"frameworks": {
"net46": {
"dependencies": {
"dotnet-test-xunit": "2.2.0-preview2-build1029"
}
},
"netcoreapp1.0": {
"dependencies": {
"Microsoft.NET.Test.Sdk": "15.0.0"
}
}
}
}