-->

运行异步任务单元测试与2010年TFS(Running Async Task unit tests

2019-08-06 12:26发布

我写是写在VS 2012使用异步库针对.NET 4.0的应用程序。

我的汽车建立在有VS 2012和.NET 4.5安装TFS 2010生成代理运行。

我到处看,如果你的单元测试是异步它必须有async Task TestMethod()签名(而不是async void TestMethod()

然而,当我这样做,我的构建服务器给了我这个错误该方法:

标有[TestMethod的]属性必须是非静态试验方法,公开,没有返回值,不应该采取任何参数。 例如:公共无效Test.Class1.Test()。

我已阅读这个和这个指示,如果你有一个.testsetting文件时,它可能会导致这个错误。 但是,这两个说,他们是TFS / VS 2012的beta版本。

另外,我需要我的测试设置文件打开代码覆盖率。

这是TFS 2012嘛? 可以在TFS 2010生成代理不使用VS 2012才能正确运行这些?

有没有什么办法,使这项工作不升级到2012 TFS? (我们仍然几个月了从升级)。

Answer 1:

您可以使用类似于我描述的方法在我的博客 :只是包装在每个单元测试代码AsyncContext.Run (你可以得到AsyncContext从我AsyncEx的NuGet库 )。



Answer 2:

我有同样的问题,我有一个丑陋的解决办法,多数民众赞成在构建服务器工作解决了它(只是,直到您升级到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
}

更新:更好的解决方案,使用http://nuget.org/packages/Nito.AsyncEx/与AsyncContext.Run包裹(...)

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

    //Act
    await Provider.StartAsync();

    //Assert
  }
}


文章来源: Running Async Task unit tests with TFS 2010