问题描述
variables:
- name: BuildParameters.solution
value: '**/*.sln'
- name: buildConfiguration
value: Release
name: $(date:yyyyMMdd)$(rev:.r)
trigger: none
resources:
repositories:
- repository: self
type: git
stages:
- stage: __default
jobs:
- job: Job_1
displayName: Agent job 1
pool:
vmImage: windows-2019
steps:
- task: UseDotNet@2
displayName: '.NET Core 3.1.404'
inputs:
version: '3.1.404'
packageType: sdk
- task: DotNetCoreCLI@2
enabled: true
displayName: dotnet test
continueOnError: true
timeoutInMinutes: 0
inputs:
command: test
projects: '**/*test/*.csproj'
arguments: --configuration $(buildConfiguration) --verbosity n --collect "Code coverage"
publishTestResults: true
- task: PublishTestResults@2
inputs:
testResultsFormat: XUnit
testResultsFiles: '**/TEST.xml'
mergeTestResults: true
failTaskOnFailedTests: true
我故意破坏了测试
此测试失败,但发布测试结果任务并未完全失败
我收到警告
##[warning]No test result files matching **/TEST.xml were found. xunit
有人吃过吗?
令人讨厌的是,如果测试失败,Azure 管道会将其归类为部分成功,这并不理想。我试图陷入这样一种情况:如果测试失败,我的管道就会失败
此管道用于验证拉取请求 - 即在 PR 中构建代码并确保所有测试通过
当我搜索这个时,有提到安装 .NET 4.6.2 或更高版本,但这是在构建管道中使用的,所以不知道该怎么做
我是否遗漏了 dot net 测试任务中的某些内容?
保罗
解决方法
当您为 dotnet 测试任务将 publishTestResults
字段设置为 true
时。请参阅以下来自 here 的摘录。
此选项将 --logger trx --results-directory $(Agent.TempDirectory) 附加到命令行参数。
以上命令会生成VSTest(trx)格式的测试结果并输出到文件夹$(Agent.TempDirectory)
因此您需要像下面这样更改 PublishTestResults@2
任务:
- task: PublishTestResults@2
inputs:
testResultsFormat: VSTest #change the format to VSTest
testResultsFiles: '**/*.trx' #change to find .trx file
mergeTestResults: true
failTaskOnFailedTests: true
searchFolder: '$(Agent.TempDirectory)' #search the .trx file in $(Agent.TempDirectory)
通常 continueOnError
字段默认设置为 false
。如果有任何测试失败,则 dotnet 测试任务将失败,见下文:
如果 continueOnError
字段设置为 true
。 dotnet 测试任务和管道将显示 Warnig
状态。
因此您可以尝试将 continueOnError
字段设置为 false
,如果有任何测试失败,这将使任务和管道失败。并且不需要使用 PublishTestResults
任务。对于 dotnet 测试任务,publishTestResults
设置为 true
时,测试结果将由 dotnet 测试任务自动发布。见下文:
- task: DotNetCoreCLI@2
enabled: true
displayName: dotnet test
continueOnError: false
timeoutInMinutes: 0
inputs:
command: test
projects: '**/*test/*.csproj'
arguments: --configuration $(buildConfiguration) --verbosity n --collect "Code coverage"
publishTestResults: true