无法使用 Azure 在 YAML 中运行自动化测试

Posted

技术标签:

【中文标题】无法使用 Azure 在 YAML 中运行自动化测试【英文标题】:Cannot Get Automated Tests to Run in YAML using Azure 【发布时间】:2021-12-16 11:05:00 【问题描述】:

我正在尝试运行一些自动化测试作为构建管道的一部分,但一直没有返回任何结果。我的测试文件通常看起来像“ApiTests”或“UITests”。我试图通过查找文件的“测试”部分来定位它们。但是,我在自动测试运行程序中不断收到此消息:测试运行检测到为不同框架和平台版本构建的 DLL。以下 DLL 与当前设置不匹配,即 .NETFramework、Version=v4.0 框架和 X86 平台。 根据 csproj 文件,我的版本是 3.1。 这是 YAML 目前的样子:

# ASP.NET Core (.NET Framework)
# Build and test ASP.NET Core projects targeting the full .NET Framework.
# Add steps that publish symbols, save build artifacts, and more:
# https://docs.microsoft.com/azure/devops/pipelines/languages/dotnet-core

trigger:
- main

pool:
  vmImage: 'windows-latest'

variables:
  solution: '**/*.sln'
  buildPlatform: 'Any CPU'
  buildConfiguration: 'Release'

steps:
- task: NuGetToolInstaller@1

- task: NuGetCommand@2
  inputs:
    restoreSolution: '$(solution)'

- task: UseDotNet@2
  displayName: 'Install .NET Core SDK'
  inputs:
    version: 3.0.x
    performMultiLevelLookup: true
- task: VSBuild@1
  inputs:
    solution: '$(solution)'
    msbuildArgs: '/p:DeployOnBuild=true /p:WebPublishMethod=Package /p:PackageAsSingleFile=true /p:SkipInvalidConfigurations=true /p:DesktopBuildPackageLocation="$(build.artifactStagingDirectory)\WebApp.zip" /p:DeployIisAppPath="Default Web Site"'
    platform: '$(buildPlatform)'
    configuration: '$(buildConfiguration)'
    msbuildArchitecture: 'x64'

- task: VSTest@2
  displayName: 'Automated Tests' 
  inputs:
    testSelector: 'testAssemblies'
    testAssemblyVer2: |
      **\*test*.dll
      !**\*TestAdapter.dll
      !**\obj\**
    searchFolder: '$(System.DefaultWorkingDirectory)'
    testFiltercriteria: '/Tests:Tests'
    uiTests: true
    runTestsInIsolation: true
    codeCoverageEnabled: true
    platform: '$(buildPlatform)'
    configuration: '$(buildConfiguration)'

我的文件通常命名为:AppsSigninTests.cs

我的实际测试看起来像这样:

[Fact]
        public void SomethingSomethingSomething()
        
            //Test Code Here
        

我应该在测试标题的末尾添加“测试”吗?我错过了什么让 YAML 错过了这些测试?

编辑 我现在已经到了可以找到文件但没有运行实际测试的地步(仍然没有显示结果)。现在是 YAML:

# ASP.NET Core (.NET Framework)
# Build and test ASP.NET Core projects targeting the full .NET Framework.
# Add steps that publish symbols, save build artifacts, and more:
# https://docs.microsoft.com/azure/devops/pipelines/languages/dotnet-core

trigger:
- main

pool:
  vmImage: 'windows-latest'

variables:
  solution: '**/*.sln'
  buildPlatform: 'Any CPU'
  buildConfiguration: 'Release'

steps:
- task: NuGetToolInstaller@1

- task: NuGetCommand@2
  inputs:
    restoreSolution: '$(solution)'

#- task: UseDotNet@2
  #displayName: 'Install .NET Core SDK'
  #inputs:
    #version: 3.0.x
    #performMultiLevelLookup: true

- task: VSBuild@1
  inputs:
    solution: '$(solution)'
    msbuildArgs: '/p:DeployOnBuild=true /p:WebPublishMethod=Package /p:PackageAsSingleFile=true /p:SkipInvalidConfigurations=true /p:DesktopBuildPackageLocation="$(build.artifactStagingDirectory)\WebApp.zip" /p:DeployIisAppPath="Default Web Site"'
    platform: '$(buildPlatform)'
    configuration: '$(buildConfiguration)'

- task: VSTest@2
  displayName: 'Automated Tests' 
  inputs:
    testSelector: 'testAssemblies'
    testAssemblyVer2: |
      AutomatedRegression.csproj
      !**\*TestAdapter.dll
      !**\obj\**
#**\*test*.dll
    searchFolder: '$(System.DefaultWorkingDirectory)'
    testFiltercriteria: '/Tests:VerySpecificNameOfTest'
    uiTests: true
    runTestsInIsolation: true
    codeCoverageEnabled: true
    platform: '$(buildPlatform)'
    configuration: '$(buildConfiguration)'

test.dll 行似乎成功了,所以 yaml 找不到我要的文件。但我担心它会阻止测试实际运行。 VSTest 任务的输出是这样的:总共有 1 个测试文件与指定的模式匹配。 但后来它说:Publishing test results: 0 然后运行后的Tests选项卡没有结果也没有运行测试

【问题讨论】:

您的测试项目是否针对 .net 核心?从消息来看,它听起来像是针对 .net 框架。 @Connell.O'Donnell 它看起来像是针对 .net 框架,它应该针对 .net 代码吗? $(buildPlatform) 的值是多少?如果是 x64,何不试试 x86。 @Connell.O'Donnell 目前是“任何 CPU”,将其更改为 x86。现在我遇到了这个问题:##[warning]No test sources found matching the given filter '**test.dll,!***TestAdapter.dll,!**\obj**' 为了调试这个,我建议添加一个 powershell 任务来告诉你测试结果文件是否存在,以及它包含什么。您可以稍后删除该任务。 【参考方案1】:

现在我遇到了这个问题:##[警告]没有找到匹配的测试源 给定的过滤器 '**test.dll,!TestAdapter.dll,!\obj*'

“旧”.NET 的文件位于 Release 文件夹中(由 BuildConfiguration 变量描述)。

.NET Core 构建包含一个额外的文件夹:

YourNamespaceHere.Tests > bin > Release > netcoreapp2.1 > dll's

当您添加额外的通配符时,构建将找到测试 dll。

**\$(BuildConfiguration)\*\*test.dll

详情请咨询Running xUnit Tests。

【讨论】:

在第一个 ** 和 / 之间添加 bin 并将 test.dll 替换为我的项目的 dll 后,此方法有效

以上是关于无法使用 Azure 在 YAML 中运行自动化测试的主要内容,如果未能解决你的问题,请参考以下文章

我如何使用curl请求使用CLI通过yaml文件创建Azure管道?

Azure Pipeline 运行不会自动删除

如何在 Azure Pipelines Yaml 上循环多个 Azure 订阅?

Azure DevOps Pipelines - 仅在上一次运行成功时运行 YAML 管道

如何在 Azure Pipelines YAML 构建定义中接收修订

Azure DevOps YAML并行运行未排队