4

I'm new to the async/await world and I'm trying to figure out the benefits of writing asynchronous unit tests for async methods. That is, must a unit test for an async method call that async method asynchronously? If it calls the async method synchronously using Task.Run(), what is lost? In the latter case, code coverage isn't impacted as far as I can see.

The reason I'm asking this is because our mocking software (we use TypeMock) can't support async/await. (They say there is a legitimate reason for this lack of support, and I don't disagree with them.) By calling the async methods synchronously in the unit tests, we can workaround this issue. However, I'd like to know whether we are cutting any corner by doing this.

For example, let's say I have the following async method:

public async Task<string> GetContentAsync(string source)
{
    string result = "";
    // perform magical async IO bound work here to populate result
    return result;
}

The following is the ideal unit test which doesn't work:

[TestMethod()]
public async Task GetContentAsyncTest()
{
    string expected = "thisworks";
    var worker = new Worker();
    // ...mocking code here that doesn't work!
    string actual = await worker.GetContentAsync();
    Assert.AreEqual(expected, actual);
}

But this works, and it does provide the code coverage we need. Is this OK?

[TestMethod()]
public void GetContentAsyncTest()
{
    string expected = "thisworks";
    var worker = new Worker();
    // mocking code here that works!
    string actual = Task.Run(() => worker.GetContentAsync()).Result;
    Assert.AreEqual(expected, actual);
}
14
  • I'm not really a fan of this question; the title is a bit broad. In most cases, you aren't testing whether or not something took X long or Y long, you're just saying "If it's done, make sure this happened" or "Make sure this happened if it failed". Whether it's Async or not doesn't come into play in the vast majority of cases. Commented May 29, 2015 at 21:50
  • Async processing is mainly to free up the the thread pool or IIS worker processes
    – fuzzybear
    Commented May 29, 2015 at 21:52
  • @George Stocker I know, but the fact that I see async unit test examples out there makes me think there is a reason why people write them. What do you suggest for the title then? Can't you give me a chance to improve it before downvoting? A bit trigger happy?
    – Zoomzoom
    Commented May 29, 2015 at 21:52
  • 1
    Also TypeMock's reasons are bad.
    – AK_
    Commented May 29, 2015 at 22:06
  • 1
    @Zoomzoom hmmm... Not really. async is like poison, we just make everything up the stack async as well :p
    – Ryan Chu
    Commented May 29, 2015 at 22:37

2 Answers 2

4

must a unit test for an async method call that async method asynchronously?

No, but it is most natural to do so.

If it calls the async method synchronously using Task.Run(), what is lost?

Nothing really. It's slightly less performant, but to a degree that you will probably never notice.

You'd probably want to use GetAwaiter().GetResult() instead of Result to avoid AggregateException wrappers in your failure tests. And you can also just call the method directly; no need to wrap it in a Task.Run.

They say there is a legitimate reason for this lack of support, and I don't disagree with them.

Oh, I certainly disagree with them. :)

Does that mean they can't unit test iterator blocks, either? The exact same reasoning would apply...


The only more serious problem with not supporting async unit tests is if the code under test assumed its context would handle synchronization. This is common, for example, in moderately complex View Models.

In that case, you'd need to install a context in which to execute the async code (e.g., my AsyncContext type) unless you're using a unit test framework that automatically provides one (as of this writing, only xUnit does AFAIK).

4
  • To your second point regarding the loss of performance - even if it was significant, it still wouldn't be a concern at all for unit tests.
    – Zoomzoom
    Commented May 30, 2015 at 0:56
  • @Zoomzoom: I disagree. To a certain degree, performance of unit tests is important. Commented May 30, 2015 at 3:41
  • Stephen - I see. Hard to argue with that! For my situation though, test performance was nowhere near as important as code coverage.
    – Zoomzoom
    Commented May 30, 2015 at 5:48
  • @Zoomzoom Be careful putting too much weight on code coverage. Sometimes it's nowhere near as important as one might think stackoverflow.com/questions/90002/… Commented May 30, 2015 at 13:01
2

If you use xUnit instead of MSTest, your ideal solution (async test) will work.

1
  • I'm using xUnit, but I am getting weird deadlocks when testing async methods. I even tried ConfigureAwait(false). Commented Aug 22, 2018 at 18:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.