1

I'm still in .NET 4.0 and I was wondering whether this pattern will hold up well in various kinds of async unit testing situations:

/// <summary>
/// Noaa weather test: should read remote XML.
/// </summary>
[TestMethod]
public void ShouldReadRemoteXml()
{
    var uri = new Uri("http://www.weather.gov/xml/current_obs/KLAX.xml");
    var wait = HttpVerbAsyncUtility.GetAsync(uri)
        .ContinueWith(
        t =>
        {
            Assert.IsFalse(t.IsFaulted, "An unexpected XML read error has occurred.");
            Assert.IsTrue(t.IsCompleted, "The expected XML read did not take place.");

            if(t.IsCompleted && !t.IsFaulted)
                FrameworkFileUtility.Write(t.Result, @"NoaaWeather.xml");
        })
        .Wait(TimeSpan.FromSeconds(7));

    Assert.IsTrue(wait, "The expected XML read did not take place within seven seconds.");
}

Will this Task.ContinueWith()...Task.Wait() pattern hold up in the real world? I'm relatively new to formally thinking about Unit Testing (especially asynchronous unit testing) so please don't hold back on the basics :)

rasx
  • 5,288
  • 2
  • 45
  • 60
  • 1
    any final solution with full source code working ? maybe more complex sample for notify errors in each thread and after WaitAll shows a summary ? – Kiquenet Jan 01 '14 at 10:56
  • @Kiquenet all the stuff you ask for should be out of the box; I think async support in MSTest is just not there so XUnit might be the alternative [http://sravi-kiran.blogspot.com/2012/11/UnitTestingAsynchronousMethodsUsingMSTestAndXUnit.html] – rasx Jan 03 '14 at 00:03

1 Answers1

10

Unit test frameworks typically do not work really well out of the box with asynchronous code, which is of great concern to us as we are adding await to C# and Visual Basic.

Typically what happens is the test method hits the first "await", which immediately returns. The test is then marked as having succeeded because it returned without errors, even if errors are going to be produced in the continuation.

We're working with unit test framework providers to improve this story for the upcoming version of Visual Studio. MSTest and XUnit.NET now handle async methods properly in VS 11 Beta. My advice is: get ahold of the VS 11 beta and try out the asynchronous unit testing support in it. If you do not like it, now would be a great time to give that feedback on the async forum.

Eric Lippert
  • 647,829
  • 179
  • 1,238
  • 2,067