【问题标题】:How to run a single test in Azure DevOps build如何在 Azure DevOps 构建中运行单个测试
【发布时间】:2021-01-19 15:15:36
【问题描述】:

我已尝试以下方法在 Azure DevOps 构建脚本中运行单个测试。我只有 2 个测试,我只想运行其中一个。

用于测试的 Azure DevOps 构建脚本

- task: DotNetCoreCLI@2
  displayName: Smoke test
  inputs:
    command: test
    projects: '**/*Test*.csproj'
    arguments: '--configuration $(buildConfiguration) --filter Name~TestMethod2'

测试结果

Starting test execution, please wait...

A total of 1 test files matched the specified pattern.
No test matches the given testcase filter `TestMethod2` in C:\agent-orsa-ssp\_work\2\s\RsSolution7\UnitTestProject1\bin\Release\netcoreapp3.1\UnitTestProject1.dll

【问题讨论】:

  • 这个问题怎么样?下面的答案是否解决了您的问题,如果没有,请告诉我有关此问题的最新信息吗?

标签: azure-devops


【解决方案1】:

如何在 Azure DevOps 构建中运行单个测试

根据报错信息:

没有测试匹配给定的测试用例过滤器TestMethod2 in UnitTestProject1.dll

UnitTestProject1 项目中似乎没有名称包含 TestMethod2 的测试,例如:

    [TestMethod]
    public void TestMethod2()
    {

    }

要解决此问题,请确保您拥有名称包含 TestMethod2 的测试。

例如官方文档Unit testing C# with MSTest and .NET Core

using Microsoft.VisualStudio.TestTools.UnitTesting;
using Prime.Services;

namespace Prime.UnitTests.Services
{
    [TestClass]
    public class PrimeService_IsPrimeShould
    {
        private readonly PrimeService _primeService;

        public PrimeService_IsPrimeShould()
        {
            _primeService = new PrimeService();
        }

        [TestMethod]
        public void IsPrime_InputIs1_ReturnFalse()
        {
            var result = _primeService.IsPrime(1);

            Assert.IsFalse(result, $"1 should not be prime");
        }

        #region Sample_TestCode
        [DataTestMethod]
        [DataRow(-1)]
        [DataRow(0)]
        [DataRow(1)]
        public void IsPrime_ValuesLessThan2_ReturnFalse(int value)
        {
            var result = _primeService.IsPrime(value);

            Assert.IsFalse(result, $"{value} should not be prime");
        }
        #endregion
    }
}

然后我使用--filter Name~IsPrime_InputIs1_ReturnFalse 运行单个测试IsPrime_InputIs1_ReturnFalse,它工作正常:

【讨论】:

    猜你喜欢
    • 2020-01-29
    • 1970-01-01
    • 1970-01-01
    • 2021-01-05
    • 1970-01-01
    • 1970-01-01
    • 2023-02-26
    • 1970-01-01
    • 2019-08-14
    相关资源
    最近更新 更多