-->

Running Async Task unit tests with TFS 2010

2019-03-31 10:36发布

问题:

I am writing an application that is written in VS 2012 targeting .NET 4.0 using the Async library.

My auto builds run on a TFS 2010 build agent that has VS 2012 and .NET 4.5 installed.

I read everywhere that if your unit test is async it must have the async Task TestMethod() signature (rather than async void TestMethod()).

However when I do that my build server gives me this error for that method:

Test method marked with the [TestMethod] attribute must be non-static, public, does not return a value and should not take any parameter. for example: public void Test.Class1.Test().

I have read this and this that indicate that if you have a .testsetting file it can cause this error. But both of those say they are for beta versions of TFS/VS 2012.

Also, I need my test settings file to turn on code coverage.

Is this a TFS 2012 only thing? Can the TFS 2010 Build agent not use VS 2012 to run these correctly?

Is there any way to make this work without upgrading to TFS 2012? (We are still a few months out from that upgrade).

回答1:

You can use an approach similar to what I described on my blog: just wrap the code for each unit test in an AsyncContext.Run (you can get AsyncContext from my AsyncEx NuGet library).



回答2:

I had the same problem, I solved it with a ugly workaround thats works on the build server (just until you upgrade to tfs 2012)

[TestMethod]
public void TestMethod()
{
  //TODO: fix this after upgrade to TFS2012 build server
  var task = TestMethodInnerMethod();
  task.Wait();
}

private async Task TestMethodInnerMethod()
{
  //Arrange

  //Act
  await Provider.StartAsync();

  //Assert
}

Update: Even better solution, use http://nuget.org/packages/Nito.AsyncEx/ and wrap with AsyncContext.Run(...)

[TestMethod]
public void TestMethod()
{
  //TODO: fix this after upgrade to TFS2012 build server
  AsyncContext.Run(async () =>
  {
    //Arrange

    //Act
    await Provider.StartAsync();

    //Assert
  }
}