简体   繁体   English

异步测试在Nunit 2.6.2中挂起

[英]Async test hangs in Nunit 2.6.2

I have this simple test method below. 我下面有这个简单的测试方法。

[Test]
        public async Task OneSimpleTest1()
        {
            var eightBall = new EightBall();
            var answer = await eightBall.WillIWin();

            Assert.That(answer, Is.True);
        }

The test class looks like this 测试类看起来像这样

public class EightBall
    {
        public Task<bool> WillIWin()
        {
            return new Task<bool>(() => true);
        }
    }

I run the tests in Nunit 2.6.2 using the below command. 我使用以下命令在Nunit 2.6.2中运行测试。

nunit-console.exe EightBall.dll /framework:net-4.5 nunit-console.exe EightBall.dll /framework:net-4.5

However, the test does not seem to return and hangs forever. 但是,测试似乎并没有返回并永久挂起。 Is there a special way to run async tests with Nunit 2.6.2. 是否有使用Nunit 2.6.2运行异步测试的特殊方法。 I thought async was supported using Nunit 2.6.2 我以为使用Nunit 2.6.2支持异步

return new Task<bool>(() => true); creates a task but does't start it. 创建一个任务,但不启动它。 Better use return Task.Run(()=> true); 最好使用return Task.Run(()=> true); or return Task.FromResult<bool>(true) return Task.FromResult<bool>(true)

You can also change your code to 您也可以将代码更改为

public Task<bool> WillIWin()
{
    var task = new Task<bool>(() => true);
    task.Start();
    return task;
}

to make it work 使它起作用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM