PublishTestResults@2 - 发布测试结果 v2 任务
将测试结果发布到 Azure Pipelines。
将测试结果发布到 Azure Pipelines/TFS。
将测试结果发布到 VSTS/TFS。
语法
# Publish Test Results v2
# Publish test results to Azure Pipelines.
- task: PublishTestResults@2
inputs:
testResultsFormat: 'JUnit' # 'JUnit' | 'NUnit' | 'VSTest' | 'XUnit' | 'CTest'. Alias: testRunner. Required. Test result format. Default: JUnit.
testResultsFiles: '**/TEST-*.xml' # string. Required. Test results files. Default: **/TEST-*.xml.
#searchFolder: '$(System.DefaultWorkingDirectory)' # string. Search folder. Default: $(System.DefaultWorkingDirectory).
#mergeTestResults: false # boolean. Merge test results. Default: false.
#failTaskOnFailedTests: false # boolean. Fail if there are test failures. Default: false.
#testRunTitle: # string. Test run title.
# Advanced
#buildPlatform: # string. Alias: platform. Build Platform.
#buildConfiguration: # string. Alias: configuration. Build Configuration.
#publishRunAttachments: true # boolean. Upload test results files. Default: true.
# Publish Test Results v2
# Publish Test Results to Azure Pipelines/TFS.
- task: PublishTestResults@2
inputs:
testResultsFormat: 'JUnit' # 'JUnit' | 'NUnit' | 'VSTest' | 'XUnit'. Alias: testRunner. Required. Test result format. Default: JUnit.
testResultsFiles: '**/TEST-*.xml' # string. Required. Test results files. Default: **/TEST-*.xml.
#searchFolder: '$(System.DefaultWorkingDirectory)' # string. Search folder. Default: $(System.DefaultWorkingDirectory).
#mergeTestResults: false # boolean. Merge test results. Default: false.
#testRunTitle: # string. Test run title.
# Advanced
#buildPlatform: # string. Alias: platform. Build Platform.
#buildConfiguration: # string. Alias: configuration. Build Configuration.
#publishRunAttachments: true # boolean. Upload test results files. Default: true.
# YAML Syntax is not supported in TFS 2018.
# Use the classic designer to add and configure tasks.
# See the following Inputs section for details on the inputs that this task supports.
输入
testResultsFormat
- 测试结果格式
输入别名: testRunner
。 string
. 必需。 允许的值:JUnit
、NUnit
、、VSTest
XUnit
、CTest
。 默认值:JUnit
。
指定要发布的结果文件的格式。 支持以下格式: CTest、 JUnit、 NUnit 2、 NUnit 3、Visual Studio Test (TRX) 和 xUnit 2。
testResultsFormat
- 测试结果格式
输入别名: testRunner
。 string
. 必需。 允许的值:JUnit
、NUnit
、、VSTest
XUnit
。 默认值:JUnit
。
指定要发布的结果文件的格式。 支持以下格式: CTest、 JUnit、 NUnit 2、 NUnit 3、Visual Studio Test (TRX) 和 xUnit 2。
testResultsFiles
- 测试结果文件
string
. 必需。 默认值:**/TEST-*.xml
。
指定一个或多个测试结果文件。
- 可以使用单文件夹通配符 ()
*
和递归通配符 (**
) 。 例如,**/TEST-*.xml
在所有子目录中搜索其名称以 开头TEST-
的所有 XML 文件。 如果使用 VSTest 作为测试结果格式,应将文件类型更改为.trx
例如**/TEST-*.trx
- 可以指定多个路径,用新行分隔。
- 此外,还接受 微型匹配模式。
例如, !TEST[1-3].xml
排除名为 TEST1.xml
、 TEST2.xml
或 TEST3.xml
的文件。
testResultsFiles
- 测试结果文件
string
. 必需。 默认值:**\TEST-*.xml
。
指定一个或多个测试结果文件。
- 可以使用单文件夹通配符 ()
*
和递归通配符 (**
) 。 例如,**/TEST-*.xml
在所有子目录中搜索其名称以 开头TEST-
的所有 XML 文件。 如果使用 VSTest 作为测试结果格式,应将文件类型更改为.trx
例如**/TEST-*.trx
- 可以指定多个路径,并用换行符分隔。
- 此外,还接受 微型匹配模式。
例如, !TEST[1-3].xml
排除名为 TEST1.xml
、 TEST2.xml
或 TEST3.xml
的文件。
searchFolder
- 搜索文件夹
string
. 默认值:$(System.DefaultWorkingDirectory)
。
可选。 指定要搜索测试结果文件的文件夹。
mergeTestResults
- 合并测试结果
boolean
. 默认值:false
。
当此布尔值为 true
时,任务针对单个 测试运行报告来自所有文件的测试结果。 如果值为 false
,则任务将为每个测试结果文件创建单独的测试运行。
注意
使用合并测试结果设置合并来自同一测试框架的文件,以确保正确计算结果映射和持续时间。
failTaskOnFailedTests
- 如果测试失败,则失败
boolean
. 默认值:false
。
可选。 当此布尔值为 true
时,如果结果文件中的任何测试标记为失败,则任务将失败。 默认值为 false
,它只会从结果文件发布结果。
testRunTitle
- 测试运行标题
string
.
可选。 指定将对其报告结果的测试运行的名称。 可以使用在生成或发布管道中声明的变量名称。
buildPlatform
- 生成平台
输入别名: platform
。 string
.
可选。 指定应针对其报告测试运行的生成平台。 例如 x64
或 x86
。 如果在生成任务中为平台定义了变量,请在此处使用它。
buildConfiguration
- 生成配置
输入别名: configuration
。 string
.
可选。 指定应针对其报告测试运行的生成配置。 例如 Debug
或 Release
。 如果在生成任务中为配置定义了变量,请在此处使用它。
publishRunAttachments
- 上传测试结果文件
boolean
. 默认值:true
。
可选。 当此布尔值为 true
时,任务会将所有测试结果文件作为附件上传到测试运行。
testRunner
- 测试结果格式
string
. 必需。 允许的值:JUnit
、NUnit
、、VSTest
XUnit
。 默认值:JUnit
。
指定要发布的结果文件的格式。 支持以下格式: CTest、 JUnit、 NUnit 2、 NUnit 3、Visual Studio Test (TRX) 和 xUnit 2。
platform
- 平台
string
.
可选。 指定应针对其报告测试运行的生成平台。 例如 x64
或 x86
。 如果在生成任务中为平台定义了变量,请在此处使用它。
configuration
- 配置
string
.
可选。 指定应针对其报告测试运行的生成配置。 例如 Debug
或 Release
。 如果在生成任务中为配置定义了变量,请在此处使用它。
任务控件选项
除任务输入外,所有任务都有控制选项。 有关详细信息,请参阅 控制选项和常见任务属性。
输出变量
无。
备注
执行测试时,此任务会将测试结果发布到 Azure Pipelines 或 TFS,以提供全面的测试报告和分析体验。 可以使用所选支持所需结果格式的测试运行程序。 支持的结果格式包括 CTest、 JUnit (包括 PHPUnit) 、 NUnit 2、 NUnit 3、Visual Studio Test (TRX) 和 xUnit 2。
其他内置任务(例如 Visual Studio 测试任务 和 Dot NetCore CLI 任务 )会自动将测试结果发布到管道。 Ant、Maven、Gulp、Grunt 和 Xcode 等任务将发布结果作为任务中的一个选项提供,或生成库(如 Cobertura 和 JaCoCo)。 如果使用上述任一任务,则不需要在管道中单独 执行“发布测试结果” 任务。
已发布的测试结果显示在管道摘要的 “测试”选项卡中 。 这些结果有助于衡量管道质量、查看可跟踪性、排查故障以及驱动故障所有权。
以下示例演示任务配置为发布测试结果。
还可以在生成管道中使用此任务,将运行测试时生成的 代码覆盖率结果发布到 Azure Pipelines 或 TFS,以获取覆盖率报告。
先决条件
如果使用 Windows 自承载代理,计算机必须安装以下先决条件:
- .NET Framework 4.6.2 或更高版本
任务默认值
默认选项使用 JUnit 格式发布测试结果。 使用 VSTest 作为 testRunner 时, testResultsFiles 选项应更改为 **/TEST-*.trx
。
testResultsFormat 是 testRunner 输入名称的别名。 结果文件可以由多个运行器生成,而不仅仅是一个特定的运行器。 例如,许多运行器支持 jUnit 结果格式,而不仅仅是 jUnit。
若要使用 YAML 发布 Python 的测试结果,请参阅这些主题的生态系统部分中的 Python,其中还包括其他语言的示例。
结果格式映射
此表列出了生成或发布摘要中的 “测试”选项卡中 报告的字段,以及具有支持测试结果格式的属性的对应映射。
作用域 | 字段 | Visual Studio 测试 (TRX) |
---|---|---|
测试运行 | Title | 任务中指定的测试运行标题 |
开始日期 | /TestRun/Times.Attributes[“start”]。价值 | |
完成日期 | /TestRun/Times.Attributes[“finish”]。价值 | |
持续时间 | 完成日期 - 开始日期 | |
Attachments | 请参阅下面的 附件支持 部分 | |
测试结果 | Title | /TestRun/Results/UnitTestResult.Attributes[“testName”]。值或 /TestRun/Results/WebTestResult.Attributes[“testName”]。Value Or /TestRun/Results/TestResultAggregation.Attributes[“testName”]。价值 |
开始日期 | /TestRun/Results/UnitTestResult.Attributes[“startTime”]。Value Or /TestRun/Results/WebTestResult.Attributes[“startTime”]。值或 /TestRun/Results/TestResultAggregation.Attributes[“startTime”]。价值 | |
完成日期 | /TestRun/Results/UnitTestResult.Attributes[“startTime”]。Value + /TestRun/Results/UnitTestResult.Attributes[“duration”]。Value Or /TestRun/Results/WebTestResult.Attributes[“startTime”]。Value + /TestRun/Results/WebTestResult.Attributes[“duration”]。值或 /TestRun/Results/TestResultAggregation.Attributes[“startTime”]。Value + /TestRun/Results/TestResultAggregation.Attributes[“duration”]。价值 | |
持续时间 | /TestRun/Results/UnitTestResult.Attributes[“duration”]。值或 /TestRun/Results/WebTestResult.Attributes[“duration”]。值或 /TestRun/Results/TestResultAggregation.Attributes[“duration”]。价值 | |
所有者 | /TestRun/TestDefinitions/UnitTest/Owners/Owner.Attributes[“name”]。价值 | |
业务成效 | /TestRun/Results/UnitTestResult.Attributes[“outcome”]。值或 /TestRun/Results/WebTestResult.Attributes[“result”]。值或 /TestRun/Results/TestResultAggregation.Attributes[“result”]。价值 | |
错误消息 | /TestRun/Results/UnitTestResult/Output/ErrorInfo/Message.InnerText 或 /TestRun/Results/WebTestResultOutput/ErrorInfo/Message.InnerText or /TestRun/results/TestResultAggregation/Output/ErrorInfo/Message.InnerText | |
堆栈跟踪 | /TestRun/Results/UnitTestResult/Output/ErrorInfo/StackTrace.InnerText 或 /TestRun/Results/WebTestResultOutput/ErrorInfo/StackTrace.InnerText 或 /TestRun/Results/TestResultAggregation/Output/ErrorInfo/StackTrace.InnerText | |
Attachments | 请参阅下面的 附件支持 部分 | |
控制台日志 | /TestRun/Results/UnitTestResult/Output/StdOut.InnerText 或 /TestRun/results/WebTestResultOutput/Output/stdOut.InnerText or /TestRun/results/TestResultAggregation/Output/StdOut.InnerText | |
控制台错误日志 | /TestRun/Results/UnitTestResult/Output/StdErr.InnerText 或 /TestRun/Results/WebTestResultOutput/Output/stdErr.InnerText or /TestRun/Results/TestResultAggregation/Output/stdErr.InnerText | |
代理名称 | /TestRun/Results/UnitTestResult.Attributes[“computerName”]。值或 /TestRun/Results/WebTestResult.Attributes[“computerName”]。Value 或 /TestRun/Results/TestResultAggregation.Attributes[“computerName”]。价值 | |
测试文件 | /TestRun/TestDefinitions/UnitTest.Attributes[“storage”]。价值 | |
优先级 | /TestRun/TestDefinitions/UnitTest.Attributes[“priority”]。价值 |
注意
仅 当 “开始日期” 和“ 完成日期 ”不可用时,才会使用持续时间。
testName 的完全限定名称格式为 Namespace.Testclass.Methodname,字符限制为 512。 如果测试是数据驱动的,并且具有参数,则字符限制将包括参数。
附件支持
“发布测试结果”任务为以下格式的测试运行和测试结果提供附件支持。 对于公共项目,我们支持 2GB 的总附件。
Visual Studio 测试 (TRX)
作用域 | 类型 | 路径 |
---|---|---|
测试运行 | 数据收集器 | /TestRun/ResultSummary/CollectorDataEntries/Collector/UriAttachments/UriAttachment/A.Attributes[“href”]。价值 |
测试结果 | /TestRun/ResultSummary/ResultFiles/ResultFile.Attributes[“path”]。价值 | |
代码覆盖率 | /TestRun/TestSettings/Execution/AgentRule/DataCollectors/DataCollector/Configuration/CodeCoverage/Regular/CodeCoverageItem.Attributes[“binaryFile”]。Value And /TestRun/TestSettings/Execution/AgentRule/DataCollectors/DataCollector/Configuration/CodeCoverage/Regular/CodeCoverageItem.Attributes[“pdbFile”]。价值 | |
测试结果 | 数据收集器 | /TestRun/Results/UnitTestResult/CollectorDataEntries/Collector/UriAttachments/UriAttachment/A.Attributes[“href”]。值或 /TestRun/Results/WebTestResult/CollectorDataEntries/Collector/UriAttachments/UriAttachment/A.Attributes[“href”]。Value Or /TestRun/Results/TestResultAggregation/CollectorDataEntries/Collector/UriAttachments/UriAttachment/A.Attributes[“href”]。价值 |
测试结果 | /TestRun/Results/UnitTestResult/ResultFiles/ResultFile.Attributes[“path”]。值或 /TestRun/Results/WebTestResult/ResultFiles/ResultFile.Attributes[“path”]。Value 或 /TestRun/Results/TestResultAggregation/ResultFiles/ResultFile.Attributes[“path”]。价值 |
注意
将测试结果文件作为附件上传的选项是任务中的默认选项,适用于所有格式。
示例
Docker
对于基于 Docker 的应用,可通过多种方式生成应用程序和运行测试:
- 在生成管道中生成和测试:生成和测试在管道中执行,测试结果使用 “发布测试结果” 任务发布。
- 使用多阶段 Dockerfile 生成和测试:使用多阶段 Docker 文件在容器内执行生成和测试,因为此类测试结果不会发布回管道。
- 使用 Dockerfile 生成、测试和发布结果:生成和测试在容器内执行,结果将发布回管道。 请参阅以下示例。
使用 Docker 文件生成、测试和发布结果
在此方法中,你将使用 Docker 文件生成代码并在容器中运行测试。 然后将测试结果复制到主机以发布到管道。 若要将测试结果发布到 Azure Pipelines,可以使用 “发布测试结果” 任务。 最终映像将发布到 Docker 或 Azure 容器注册表。
获取代码
Dockerfile.build
使用以下项在项目目录的根目录中创建文件:# Build and run tests inside the docker container FROM mcr.microsoft.com/dotnet/sdk:2.1 WORKDIR /app # copy the contents of agent working directory on host to workdir in container COPY . ./ # dotnet commands to build, test, and publish RUN dotnet restore RUN dotnet build -c Release RUN dotnet test dotnetcore-tests/dotnetcore-tests.csproj -c Release --logger "trx;LogFileName=testresults.trx" RUN dotnet publish -c Release -o out ENTRYPOINT dotnet dotnetcore-sample/out/dotnetcore-sample.dll
此文件包含生成代码和运行测试的说明。 然后,测试将复制到容器内的文件中
testresults.trx
。若要使最终映像尽可能小(仅包含运行时和部署项目),请将现有
Dockerfile
的内容替换为以下内容:# This Dockerfile creates the final image to be published to Docker or # Azure Container Registry # Create a container with the compiled asp.net core app FROM mcr.microsoft.com/dotnet/aspnet:2.1 # Create app directory WORKDIR /app # Copy only the deployment artifacts COPY /out . ENTRYPOINT ["dotnet", "dotnetcore-sample.dll"]
定义生成管道
如果你有 Docker 中心帐户,并且想要将映像推送到 Docker 注册表,请将文件的内容
.vsts-ci.docker.yml
替换为以下内容:# Build Docker image for this app, to be published to Docker Registry pool: vmImage: 'ubuntu-latest' variables: buildConfiguration: 'Release' steps: - script: | docker build -f Dockerfile.build -t $(dockerId)/dotnetcore-build:$BUILD_BUILDID . docker run --name dotnetcoreapp --rm -d $(dockerId)/dotnetcore-build:$BUILD_BUILDID docker cp dotnetcoreapp:app/dotnetcore-tests/TestResults $(System.DefaultWorkingDirectory) docker cp dotnetcoreapp:app/dotnetcore-sample/out $(System.DefaultWorkingDirectory) docker stop dotnetcoreapp - task: PublishTestResults@2 inputs: testRunner: VSTest testResultsFiles: '**/*.trx' failTaskOnFailedTests: true - script: | docker build -f Dockerfile -t $(dockerId)/dotnetcore-sample:$BUILD_BUILDID . docker login -u $(dockerId) -p $pswd docker push $(dockerId)/dotnetcore-sample:$BUILD_BUILDID env: pswd: $(dockerPassword)
或者,如果配置 Azure 容器注册表并想要将映像推送到该注册表,请将文件的内容
.vsts-ci.yml
替换为以下内容:# Build Docker image for this app to be published to Azure Container Registry pool: vmImage: 'ubuntu-latest' variables: buildConfiguration: 'Release' steps: - script: | docker build -f Dockerfile.build -t $(dockerId)/dotnetcore-build:$BUILD_BUILDID . docker run --name dotnetcoreapp --rm -d $(dockerId)/dotnetcore-build:$BUILD_BUILDID docker cp dotnetcoreapp:app/dotnetcore-tests/TestResults $(System.DefaultWorkingDirectory) docker cp dotnetcoreapp:app/dotnetcore-sample/out $(System.DefaultWorkingDirectory) docker stop dotnetcoreapp - task: PublishTestResults@2 inputs: testRunner: VSTest testResultsFiles: '**/*.trx' failTaskOnFailedTests: true - script: | docker build -f Dockerfile -t $(dockerId).azurecr.io/dotnetcore-sample:$BUILD_BUILDID . docker login -u $(dockerId) -p $pswd $(dockerid).azurecr.io docker push $(dockerId).azurecr.io/dotnetcore-sample:$BUILD_BUILDID env: pswd: $(dockerPassword)
将更改推送到存储库中的主分支。
如果使用 Azure 容器注册表,请确保已在 Azure 门户中 预先创建了注册表 。 复制 Azure 门户中注册表设置的 “访问密钥 ”部分中显示的管理员用户名和密码。
使用以下内容更新生成管道
- 代理池:
Hosted Ubuntu 1604
- dockerId:将值设置为 DockerHub 的 Docker ID 或 Azure 容器注册表的管理员用户名。
- dockerPassword:将值设置为 DockerHub 的密码或管理员密码 Azure 容器注册表。
- YAML 文件路径:
/.vsts-ci.docker.yml
- 代理池:
将新版本排队,并监视它创建 Docker 映像并将其推送到注册表,并将测试结果推送到 Azure DevOps。