gpt4 book ai didi

c# - NUnit 3.X.X 异步测试

转载 作者:行者123 更新时间:2023-12-03 13:07:41 26 4
gpt4 key购买 nike

我正在尝试使用 NUnit 3.12.0.0 对异步任务进行单元测试。问题是我不断收到以下消息:消息:当没有预期结果时,异步测试方法必须具有非通用任务返回类型 并且我的测试失败了。下面的代码实际上适用于 Visual Studio 2017 和旧版本的 nUnit。

据我了解,新的 nUnit 框架需要返回 TaskTask<T> .

这是我的测试函数

[Test]
public async Task<string> Login()
{
var url = "http://localhost:xxxx/login";
object[] jsonBody = { "{\"username\":\"devTeam@xxxxxxxxx.com\",\"password\":\"xxxx\"}" };

RestRequestResponse<RestResponse> result = await HttpRestUtil.ExecuteCompleteRestRequest<RestResponse>(url, null, jsonBody, Method.POST);
Assert.IsNotNull(result);
Assert.IsNotNull(result.headers[0].Value.ToString());
return result.headers[0].Value.ToString();
}

最佳答案

我找到了问题的答案。答案来自 NUnit 文档,其中说明如下:

Test methods targeting .Net 4.0 or higher may be marked as async and NUnit will wait for the method to complete before recording the result and moving on to the next test. Async test methods must return Task if no value is returned, or Task if a value of type T is returned.

If the test method returns a value, you must pass in the ExpectedResult named parameter to the Test attribute. This expected return value will be checked for equality with the return value of the test method.

这是一个例子:

// Async test with an expected result
[Test(ExpectedResult = 4)]
public async Task<int> TestAdd()
{
await ...
return 2 + 2;
}


// A simple async test
[Test]
public async Task AddAsync()
{ /* ... */ }

关于c# - NUnit 3.X.X 异步测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57698910/

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