20

Not sure this is a known issue. I’m using VS2012 RC (Ultimate), and Win8 Release Preview. I have created a Unit Test Library (metro style app), and wrote a Unit Test, which include async/await keywords. However when I compile the Unit Test project, Unit Test Explorer does not show up the Test I wrote. If I exclude the async/await keywords, then the Unit Test Explorer shows up in the Test I just wrote. Has anyone come across with this before, or is it just me?

[TestClass]
public class UnitTest1
{
    [TestMethod]
    public async void SomeAsyncTest()
    {
        var result = await StorageFile.GetFileFromPathAsync("some file path");
    }
}
svick
  • 236,525
  • 50
  • 385
  • 514
Spock
  • 7,009
  • 1
  • 41
  • 60
  • 1
    What happens if you make the method return a `Task`? – svick Jun 16 '12 at 13:25
  • That's interesting. I wonder if mstest is filtering methods within the class incorrectly or if the async keyword alters the signature such that it doesn't appear through reflection. – bryanbcook Jun 16 '12 at 13:42
  • svick, you right. Task makes it appear in the UTE. – Spock Jun 16 '12 at 13:47
  • 2
    @Raj: `async void` unit tests are not supported (yet). They don't work correctly. Asynchronous unit tests *must* be `async Task`. – Stephen Cleary Jun 16 '12 at 13:48
  • @svick Can you please add your comment as the answer to this question? Please see http://meta.stackexchange.com/questions/78895/how-to-handle-an-answer-as-a-comment - Thanks. – Spock Jun 17 '12 at 04:13

1 Answers1

29

Unit test methods that are async have to return Task, not void.

That's because async void methods are hard to track: there is no easy way for the unit testing library to find out that the test completed. (It's hard, but I think it's not impossible. You could do this by using a custom SynchronizationContext.)

svick
  • 236,525
  • 50
  • 385
  • 514