I'm trying to run asynchronous test methods with .NET 4.0 BCL Async and MsTest.
It seems that this setup is not able to deal with [TestMethod] async Task TestSth() due to a missing entry in the test case explorer. After changing the signature to async void, I can run the the test case but with the wrong outcome (no errors will be reported at all).
I have seen an attemt at Running Async Task unit tests with TFS 2010 but I think there should be a prettier way to tackle the problem.
Any suggestions?
Here's a workaround that is working for me. It was a bit tricky to figure out, but finally all unit tests against my .NET 4.0 libraries are being detected and showing up in Test Explorer, running and passing, and they're all written as normal
async Task
methods, without any special test runners, wrappers or third-party dependencies.Your unit test project file should now contain something similar to the following.
And that's it!
Just keep in mind that your unit test project targets .NET 4.5 (or a higher version, if you'd like) and so unit tests can use
async
methods and any other .NET 4.5 features. There shouldn't be any conflicts with the .NET 4.0 assemblies that you're testing, but if you do find conflicts, it's probably because you've redefined some types for newer Framework/C# features and made them public, thus causing conflicts when you try to use those same types in your unit tests. The best solution is to simply make those types internal to the projects you're testing.Edit:
After following those steps, you may get some build warnings:
To avoid these warnings, simply edit the unit test project and add the following metadata element to each project reference that points to a project that references Microsoft.Bcl.Build.
For example:
The full explanation can be found in the noted .targets file included with the Microsoft.Bcl.Build package. Here's the full comment, for your convenience.
You can only use the
async
keyword with an MSTest-referencing class library targeting .NET 4.5.If you can't use .NET 4.5 for whatever reason, then you'll just have to live with waiting on the tasks manually.
And even if the production code (i.e. the code under test) can't use .NET 4.5, why can't the test project do so? If you already have VS 2012+ available to you, then .NET 4.5 will be installed on your development machine.