How to test asynchronous methods with nunit

How to test asynchronous methods using nunit?

+5
source share
2 answers

If you have the .NET version 5 of the C # compiler, you can use the new async keywords and expectations. attach the link: http://simoneb.imtqy.com/blog/2013/01/19/async-support-in-nunit/

If you can use closure with an anonymous lambda function using thread synchronization.

eg)

[TestFixture]
class SomeTests
{
    [Test]
    public void AsyncTest()
    {
        var autoEvent = new AutoResetEvent(false); // initialize to false

        var Some = new Some();
        Some.AsyncFunction(e =>
        {
            Assert.True(e.Result);
            autoEvent.Set(); // event set
        });
        autoEvent.WaitOne(); // wait until event set
    }

}
+2
source

You can use NUnits Delayed Constraint

[TestFixture]
class SomeTests
{
    [Test]
    public void AsyncTest()
    {
        var result = false;
        var Some = new Some();
        Some.AsyncFunction(e =>
        {
            result = e.Result;
        });

        Assert.That(() => result, Is.True.After(1).Minutes.PollEvery(500).MilliSeconds);
    }

}

NUnit 3.6, Delayed Constraint, , .

0

All Articles