Xunit 异步测试
异步普通例子
public class UnitTest1
{
[Fact]
public async Task Add_Should_Return_Total()
{
var result = await AddByRemote(2, 2);
Assert.Equal(4, result);
}
async Task<int> AddByRemote(int x, int y)
{
await Task.Delay(0);
return x + y;
}
}
异步捕获异常
public class MyTestClass
{
[Fact]
public async Task MyTest()
{
var ex = await Assert.ThrowsAsync<ArgumentException>(async() =>
{
await Add(null, 3);
});
Assert.Equal("a", ex.Message);
}
public async Task<int> Add(int? a, int b)
{
throw new ArgumentException(nameof(a));
}
}