|
|
|
|
运行 xUnit 单元测试
|
|
|
|
|
=====================
|
|
|
|
|
Polgylot Notebooks 并没有直接支持单元测试框架。不能像VS里那样方便的进行单元测试。简单远行的话,可以使用下面的方案!
|
|
|
|
|
## 1、引入必要的NuGet包到.NET Interactive环境中
|
|
|
|
|
```csharp
|
|
|
|
|
// 默认包源
|
|
|
|
|
#i "nuget:https://api.nuget.org/v3/index.json"
|
|
|
|
|
|
|
|
|
|
//Nuget 包引用
|
|
|
|
|
#r "nuget:xunit"
|
|
|
|
|
#r "nuget: xunit.assert"
|
|
|
|
|
#r "nuget:xunit.runner.visualstudio"
|
|
|
|
|
#r "nuget:xunit.runner.console"
|
|
|
|
|
```
|
|
|
|
|
## 2、定义测试类和测试方法
|
|
|
|
|
```csharp
|
|
|
|
|
using Xunit;
|
|
|
|
|
|
|
|
|
|
public class CalculatorTests
|
|
|
|
|
{
|
|
|
|
|
[Fact]
|
|
|
|
|
public void Add_ReturnsCorrectSum()
|
|
|
|
|
{
|
|
|
|
|
var calculator = new Calculator();
|
|
|
|
|
var result = calculator.Add(2, 2);
|
|
|
|
|
Assert.Equal(4, result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void Subtract_ReturnsCorrectDifference()
|
|
|
|
|
{
|
|
|
|
|
var calculator = new Calculator();
|
|
|
|
|
var result = calculator.Subtract(5, 3);
|
|
|
|
|
Assert.Equal(2, result);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class Calculator
|
|
|
|
|
{
|
|
|
|
|
public int Add(int x, int y) => x + y;
|
|
|
|
|
public int Subtract(int x, int y) => x - y;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
## 3、执行测试并获取结果。在.NET Interactive中,直接运行xUnit测试并不像在Visual Studio或通过命令行那样直接。你需要手动实例化测试类并调用测试方法,或者使用反射来自动发现和执行测试方法。以下是一个简化的例子,展示如何手动执行测试方法并输出结果。
|
|
|
|
|
+ 手动调用测试
|
|
|
|
|
```csharp
|
|
|
|
|
// 手动执行测试方法
|
|
|
|
|
var calculatorTests = new CalculatorTests();
|
|
|
|
|
|
|
|
|
|
// 执行Add_ReturnsCorrectSum测试
|
|
|
|
|
calculatorTests.Add_ReturnsCorrectSum();
|
|
|
|
|
|
|
|
|
|
// 执行Subtract_ReturnsCorrectDifference测试
|
|
|
|
|
calculatorTests.Subtract_ReturnsCorrectDifference();
|
|
|
|
|
|
|
|
|
|
Console.WriteLine("Tests executed successfully.");
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
+ 使用反射的例子
|
|
|
|
|
```csharp
|
|
|
|
|
#r "nuget: xunit"
|
|
|
|
|
#r "nuget: xunit.assert"
|
|
|
|
|
|
|
|
|
|
using Xunit;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
```csharp
|
|
|
|
|
//被测试类
|
|
|
|
|
public class Calculator
|
|
|
|
|
{
|
|
|
|
|
public int Add(int a, int b) => a + b;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//测试类
|
|
|
|
|
public class CalculatorTests
|
|
|
|
|
{
|
|
|
|
|
[Fact]
|
|
|
|
|
public void Add_TwoNumbers_ReturnsSum()
|
|
|
|
|
{
|
|
|
|
|
var calculator = new Calculator();
|
|
|
|
|
int result = calculator.Add(1, 2);
|
|
|
|
|
Assert.Equal(3, result); //:ml-citation{ref="3,4" data="citationList"}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//使用反射,调用被测试类
|
|
|
|
|
var testInstance = new CalculatorTests();
|
|
|
|
|
var testMethods = testInstance.GetType()
|
|
|
|
|
.GetMethods()
|
|
|
|
|
.Where(m => m.GetCustomAttributes(typeof(FactAttribute), false).Length > 0);
|
|
|
|
|
|
|
|
|
|
foreach (var method in testMethods)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
method.Invoke(testInstance, null);
|
|
|
|
|
Console.WriteLine($"✅ {method.Name} 通过");
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine($"❌ {method.Name} 失败: {ex.InnerException?.Message}");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|