Skip to Content
Async in C# 5.0
book

Async in C# 5.0

by Alex Davies
September 2012
Beginner to intermediate
104 pages
2h 19m
English
O'Reilly Media, Inc.
Content preview from Async in C# 5.0

Chapter 11. Unit Testing Async Code

I’d like to look briefly at how to unit test async code. The simplest approach doesn’t work well, but it can be easy to write good tests that call async methods, depending on support from your unit test framework.

The Problem with Unit Testing in Async

Async methods return quickly, usually returning a Task that completes at some point in the future. To consume them, we’d normally use await, so let’s experiment with that approach in a unit test.

[TestMethod]
public async void AlexsTest()
{
    int x = await AlexsMethod();
    Assert.AreEqual(3, x);
}

To allow us to use await, I’ve also marked my test method async. But that has a very important side effect. Now, the test method also returns quickly and completes at some point in the future. In fact, the test method returned without throwing any exceptions as soon as it reaches the await, so the test framework, in this case MSTest, marks it as a pass.

Because the test method is async void, any exceptions that happen in it are rethrown on the calling SynchronizationContext, where they are either ignored, or cause an unrelated future test to fail.

The real danger here is that all your tests will appear to pass, irrespective of the actual result.

Writing Working Async Tests Manually

One way to avoid this problem is to avoid making your test methods async. Instead, we have to wait synchronously for the result of any async calls they make.

[TestMethod]
public void AlexsTest()
{
    int x = AlexsMethod().Result;
    Assert.AreEqual ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Hands-On Design Patterns with C# and .NET Core

Hands-On Design Patterns with C# and .NET Core

Gaurav Aroraa, Jeffrey Chilberto

Publisher Resources

ISBN: 9781449337155Errata Page