Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I test an async method with NUnit (or possibly with another framework)?

I have an ASP.NET Web API application, with an ApiController that features asynchronous methods, returning Task<> objects and marked with the async keyword.

public class MyApiController : ApiController {     public async Task<MyData> GetDataById(string id)     {         ...     } } 

How can I write NUnit tests for the ApiController's asynchronous methods? If I need to use another testing framework I'm open for that too. I'm fairly new to .NET unit testing in general, so I'm interested in learning best practices.

like image 629
aknuds1 Avatar asked Aug 30 '12 07:08

aknuds1


People also ask

Can NUnit tests be async?

So, the moral of the story is: you can write async void unit tests in NUnit 2.6. It also works for delegates passed to Assert. Throws , which can have an async modified.

How do you test asynchronous?

To test asynchronous code, we use the XCTestExpectation class and wait for the expected outcome. The workflow is to create an expectation, and then when the asynchronous task completes successfully, we fulfil that expectation. We will wait for a specific amount of time for the expectation to be fulfilled.

What is NUnit testing framework?

NUnit is an evolving, open source framework designed for writing and running tests in Microsoft . NET programming languages. NUnit, like JUnit, is an aspect of test-driven development (TDD), which is part of a larger software design paradigm known as Extreme Programming (XP).

What type of testing does NUnit support?

NUnit is an open-source unit testing framework for the . NET Framework and Mono. It serves the same purpose as JUnit does in the Java world, and is one of many programs in the xUnit family.


1 Answers

As of today (7/2/2014) async testing is supported by:

  • xUnit: with async methods that return a Task (since 1.9)
  • MSTest: with async methods that return a Task (since VS 2012)
  • NUnit: with async methods that return a Task, or even, that return void, since 2.6.2. Since 3.0 th option to return void is not supported, as the doc of the analyzer error NUnit1012 The async test method must have a non-void return type explains.

In the first two frameworks, the test method must have this signature:

[TestMethod] public async Task MyTestMethod() {    ...    var result = await objectUnderTest.TestedAsyncMethod(...);    // Make assertions } 

NUnit v2.6.2+ (but before 3.0), apart from that signature, supports this one:

public async void MyTestMethod() 

Of course, inside any of these test methods you can use await to call and wait on asynchronous methods.

If you're using a testing framework that doesn't support async test methods, then, the only way to do it, is to call the async method and wait until it finishes running using any of the usual ways: await, reading the Result property of the Task<T> returned by an async method, using any of the usual wait methods of Task and so on. After the awaiting, you can do all the asserts as usual. For example, using MSTest:

[TestMethod] public void MyTestMethod() {     ...     Task<MyResultClass> task = objectUnderTest.MyAsyncMethod(...);     // Make anything that waits for the method to end     MyResultClass result = task.Result;      // Make the assertions     Assert.IsNotNull(result);     ... } 
like image 80
JotaBe Avatar answered Sep 19 '22 12:09

JotaBe