【问题标题】:Running (x)Unit Tests on TFS Build Pipeline在 TFS 构建管道上运行 (x) 单元测试
【发布时间】:2017-11-30 14:26:42
【问题描述】:

我只是一个初学者,仍在尝试了解 TFS 及其持续集成工作流程。话虽如此,这也可能是一个愚蠢的问题,因为我可能会遗漏一个简单的细节,但我们将非常感谢任何帮助或建议。

所以,我有一个使用 .NET Core 2.0 编写的相当简单的单元测试示例,我想在 TFS 服务器的 CI 构建管道上将其作为测试任务运行。它几乎看起来像这样:

using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace MyUnitTest
{

    [TestClass]
    public class MyUnitTest
    {

        [TestMethod]
        public void PassingTest()
        {
            Assert.AreEqual(4, Add(2, 2));
        }

        [TestMethod]
        public void FailingTest()
        {
            Assert.AreEqual(5, Add(2, 2));
        }

        int Add(int x, int y)
        {
        return x + y;
        }

    }

}

当我尝试在 Visual Studio 中运行这些测试时,它会完美构建,并且测试会相应地成功和失败。所以我提交我的项目并将其推送到我们的 TFS git 存储库。现在,我同样想将这些测试集成到我们的构建管道中。

在我们的 CI 构建中使用的构建定义类似于 this。我在构建管道中添加了Visual Studio Test - testAssemblies 任务并配置了搜索模式以查找名为MyUnitTest.dll 的程序集等。当我排队构建时,我在 VSTest 的log 中收到以下警告。

Warning: No test is available in C:\BuildAgent\_work\9\s\MyUnitTest\MyUnitTest\bin\release\netcoreapp1.1\MyUnitTest.dll. Make sure that installed test discoverers & executors, platform & framework version settings are appropriate and try again.

所以,在我看来,VSTest 不知何故无法在目标程序集中找到要运行的测试。我很肯定我可能配置错误,或者忘记适当地设置某些特定参数。对于任何可能指出我可能做错了什么的建议,我将不胜感激。

在网上搜索解决方案,遇到了这个question,好像也有类似的问题。

【问题讨论】:

  • 您是否已将 xUnit 测试运行程序 NuGet 包添加到您的项目中?
  • 是的,我已经包含了 xUnit 测试运行器。
  • @EmreAKI this 问题也有类似的问题,也许它的答案也能帮助到你
  • 您使用的是哪个版本的 TFS?

标签: unit-testing tfs visual-studio-2017 xunit build-definition


【解决方案1】:

首先确保您的构建代理环境与您的本地开发机器相同。如Visual Studio 版本、MsTestAdapter 、xunit runner 版本等。

您可以通过直接在构建代理机器上手动运行测试而不是通过 TFS 来双重确认。

然后在您的构建管道中使用以下任务:

  1. 添加dotnet restore 任务。
  2. 然后是dotnet build 任务。
  3. 使用参数--no-build --logger "trx;LogFileName=tests-log.trx 添加dotnet test 任务
  4. 使用以下设置添加Publish test results 任务

更多详情请参考本教程博客:Running dotnet core xUnit tests on Visual Studio Team Services (VSTS)

【讨论】:

  • 谢谢!这非常有帮助。
猜你喜欢
  • 2010-09-22
  • 1970-01-01
  • 2015-07-17
  • 2016-08-23
  • 2018-05-04
  • 2013-10-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多