gpt4 book ai didi

c# - xUnit.net 我如何指定超时测试最多需要多长时间

转载 作者:太空狗 更新时间:2023-10-29 18:07:24 24 4
gpt4 key购买 nike

我使用 xUnit.net 设置了集成测试。

有没有办法配置集成测试最多应该持续多长时间?我的意思是阈值。

最佳答案

对于较新的 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.");
}
}
}

你可以这样使用它:

[Fact]
public void TestMethod()
{
AssertAsync.CompletesIn(2, () =>
{
RunTaskThatMightNotComplete();
});
}

但是,一定要read the reason关于为什么首先将其删除,因为该项目的维护者之一确实很好地说明了可能使您的测试运行陷入僵局。我在单线程模式下运行这些测试,它似乎没有引起问题。

关于c# - xUnit.net 我如何指定超时测试最多需要多长时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20282111/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com