cod*_*uit 8 c# testing integration continuous-integration xunit.net
我有使用xUnit.net设置的集成测试.
有没有办法配置集成测试应该持续多长时间?我是指一个门槛.
小智 9
举一个具体的例子:
你可以只使用
[Fact(Timeout = 2000)]
Run Code Online (Sandbox Code Playgroud)
例如。
提示:超时以毫秒为单位指定。
对于较新的xunit版本,我使用的方法似乎运行良好:
public static class AssertAsync
{
public static void CompletesIn(int timeout, Action action)
{
var task = Task.Run(action);
var completedInTime = Task.WaitAll(new[] { task }, TimeSpan.FromSeconds(timeout));
if (task.Exception != null)
{
if (task.Exception.InnerExceptions.Count == 1)
{
throw task.Exception.InnerExceptions[0];
}
throw task.Exception;
}
if (!completedInTime)
{
throw new TimeoutException($"Task did not complete in {timeout} seconds.");
}
}
}
Run Code Online (Sandbox Code Playgroud)
您可以像这样使用它:
[Fact]
public void TestMethod()
{
AssertAsync.CompletesIn(2, () =>
{
RunTaskThatMightNotComplete();
});
}
Run Code Online (Sandbox Code Playgroud)
但是,请务必先阅读之所以将其删除的原因,因为该项目的维护者之一确实对可能导致测试运行死锁的问题提出了一个很好的观点.我在单线程模式下运行这些测试,但似乎不会导致问题.