This project has moved. For the latest updates, please go here.
Let's take those tests that do not work as expected:

[TestClass]
public class SimpleAsyncUnitTests
{
  // A simple test that should pass; it throws an exception from an async continuation.
  [TestMethod]
  [ExpectedException(typeof(InvalidOperationException))]
  public async void ShouldPass()
  {
    await Task.Yield(); // Change this to "TaskEx.Yield" on VS2010.
    throw new InvalidOperationException();
  }

  // A simple test that should fail; it fails an assertion from an async continuation.
  [TestMethod]
  public async void ShouldFail()
  {
    await Task.Yield(); // Change this to "TaskEx.Yield" on VS2010.
    Assert.Fail();
  }
}

To give these tests an async-compatible context, all we have to do is install the NuGet package, and change [TestClass] to [AsyncTestClass] .

using Nito.AsyncEx.UnitTests; // THIS LINE WAS ADDED

[AsyncTestClass] // THIS LINE WAS CHANGED
public class SimpleAsyncUnitTests
{
  // A simple test that should pass; it throws an exception from an async continuation.
  [TestMethod]
  [ExpectedException(typeof(InvalidOperationException))]
  public async void ShouldPass()
  {
    await Task.Yield(); // Change this to "TaskEx.Yield" on VS2010.
    throw new InvalidOperationException();
  }

  // A simple test that should fail; it fails an assertion from an async continuation.
  [TestMethod]
  public async void ShouldFail()
  {
    await Task.Yield(); // Change this to "TaskEx.Yield" on VS2010.
    Assert.Fail();
  }
}

That's it! Two lines of code and we're good to go!

Much Better!

[AsyncTestClass] works for both async and non-async unit tests, so use it everywhere! There is an item template to make it even easier.

Last edited Feb 2, 2012 at 3:57 AM by StephenCleary, version 6

Comments

StephenCleary Apr 25, 2014 at 11:58 PM 
@Zapbbx: This package adds async unit test support to VS2010. VS2012 and newer have async unit test support built-in; just declare your test methods as async and have them return Task.

Zapbbx Apr 25, 2014 at 12:38 AM 
None of the [TestMethod] Methods show up in my Text Explorer when using the [AsyncTestClass] sttribute.. VS 2013 Ultimate

StephenCleary Mar 18, 2012 at 9:10 PM 
@Jav: You need to install the NuGet package. The correct reference will be added automatically.

Jav Mar 18, 2012 at 5:42 PM 
Thank you for providing this - it is just what the doctor prescribed today. One problem, however:
Nito is not recognized in the using statement. What do I need to add to the References?

StephenCleary Mar 13, 2012 at 4:08 PM 
No, the [AsyncTextClass] uses something similar to AsyncContext.Run behind the scenes. It's capable of handling both "async Task" and "async void" methods.

nnblack Feb 9, 2012 at 2:38 PM 
Thanks- this looks great! Quick question though- what is the best practice to test methods that contain async work but don’t return a task? Do we still have to provide a context in this case- e.g. Nito.AsyncEx.AsyncContext.Run(Action action)?