govin
govin

Reputation: 6693

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-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. I thought async was supported using Nunit 2.6.2

Upvotes: 2

Views: 992

Answers (1)

L.B
L.B

Reputation: 116098

return new Task<bool>(() => true); creates a task but does't start it. Better use return Task.Run(()=> true); or 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

Upvotes: 5

Related Questions