使用此任务可以使用 Visual Studio 测试(VSTest)运行单元和功能测试(Selenium、Appium、Coded UI 测试等)。 可以运行具有 Visual Studio 测试适配器的测试框架。 示例框架包括 MSTest、xUnit、NUnit、Chutzpah(适用于使用 QUnit、Mocha 和 Jasmine 的 JavaScript 测试),等等。可以使用此任务在多个代理上分发测试。
注释
VSTest@3是任务的最新版本,应在管道中使用。
注释
VSTest Azure 任务特定于 VSTest 平台。 它不支持较新的 Microsoft.Testing.Platform (MTP)。
语法
# Visual Studio Test v3
# Run unit and functional tests (Selenium, Appium, Coded UI test, etc.) using the Visual Studio Test (VsTest) runner. Test frameworks that have a Visual Studio test adapter such as MsTest, xUnit, NUnit, Chutzpah (for JavaScript tests using QUnit, Mocha and Jasmine), etc. can be run. Tests can be distributed on multiple agents using this task (version 2 and later).
- task: VSTest@3
inputs:
#azureSubscription: # string. Alias: ConnectedServiceName. Azure Resource Manager connection.
# Test selection
testSelector: 'testAssemblies' # 'testAssemblies' | 'testPlan' | 'testRun'. Required. Select tests using. Default: testAssemblies.
testAssemblyVer2: # string. Required when testSelector = testAssemblies. Test files.
#testPlan: # string. Required when testSelector = testPlan. Test plan.
#testSuite: # string. Required when testSelector = testPlan. Test suite.
#testConfiguration: # string. Required when testSelector = testPlan. Test configuration.
#tcmTestRun: '$(test.RunId)' # string. Optional. Use when testSelector = testRun. Test Run. Default: $(test.RunId).
searchFolder: '$(System.DefaultWorkingDirectory)' # string. Required. Search folder. Default: $(System.DefaultWorkingDirectory).
#resultsFolder: '$(Agent.TempDirectory)\TestResults' # string. Test results folder. Default: $(Agent.TempDirectory)\TestResults.
#testFiltercriteria: # string. Optional. Use when testSelector = testAssemblies. Test filter criteria.
#runOnlyImpactedTests: False # boolean. Optional. Use when testSelector = testAssemblies. Run only impacted tests. Default: False.
#runAllTestsAfterXBuilds: '50' # string. Optional. Use when testSelector = testAssemblies && runOnlyImpactedTests = true. Number of builds after which all tests should be run. Default: 50.
#uiTests: false # boolean. Test mix contains UI tests. Default: false.
# Execution options
#vstestLocationMethod: 'version' # 'version' | '___location'. Select test platform using. Default: version.
#vsTestVersion: 'latest' # 'latest' | '17.0' | '16.0' | '15.0' | '14.0' | 'toolsInstaller'. Optional. Use when vstestLocationMethod = version. Test platform version. Default: latest.
#vstestLocation: # string. Optional. Use when vstestLocationMethod = ___location. Path to vstest.console.exe.
#runSettingsFile: # string. Settings file.
#overrideTestrunParameters: # string. Override test run parameters.
#pathtoCustomTestAdapters: # string. Path to custom test adapters.
#runInParallel: False # boolean. Run tests in parallel on multi-core machines. Default: False.
#runTestsInIsolation: False # boolean. Run tests in isolation. Default: False.
#codeCoverageEnabled: False # boolean. Code coverage enabled. Default: False.
#otherConsoleOptions: # string. Other console options.
#diagnosticsEnabled: false # boolean. Collect advanced diagnostics in case of catastrophic failures. Default: false.
#collectDumpOn: 'onAbortOnly' # 'onAbortOnly' | 'always' | 'never'. Optional. Use when diagnosticsEnabled = true. Collect process dump and attach to test run report. Default: onAbortOnly.
#rerunFailedTests: False # boolean. Rerun failed tests. Default: False.
#rerunType: 'basedOnTestFailurePercentage' # 'basedOnTestFailurePercentage' | 'basedOnTestFailureCount'. Optional. Use when rerunFailedTests = true. Do not rerun if test failures exceed specified threshold. Default: basedOnTestFailurePercentage.
#rerunFailedThreshold: '30' # string. Optional. Use when rerunFailedTests = true && rerunType = basedOnTestFailurePercentage. % failure. Default: 30.
#rerunFailedTestCasesMaxLimit: '5' # string. Optional. Use when rerunFailedTests = true && rerunType = basedOnTestFailureCount. # of failed tests. Default: 5.
#rerunMaxAttempts: '3' # string. Optional. Use when rerunFailedTests = true. Maximum # of attempts. Default: 3.
# Advanced execution options
#distributionBatchType: 'basedOnTestCases' # 'basedOnTestCases' | 'basedOnExecutionTime' | 'basedOnAssembly'. Batch tests. Default: basedOnTestCases.
#batchingBasedOnAgentsOption: 'autoBatchSize' # 'autoBatchSize' | 'customBatchSize'. Optional. Use when distributionBatchType = basedOnTestCases. Batch options. Default: autoBatchSize.
#customBatchSizeValue: '10' # string. Required when distributionBatchType = basedOnTestCases && batchingBasedOnAgentsOption = customBatchSize. Number of tests per batch. Default: 10.
#batchingBasedOnExecutionTimeOption: 'autoBatchSize' # 'autoBatchSize' | 'customTimeBatchSize'. Optional. Use when distributionBatchType = basedOnExecutionTime. Batch options. Default: autoBatchSize.
#customRunTimePerBatchValue: '60' # string. Required when distributionBatchType = basedOnExecutionTime && batchingBasedOnExecutionTimeOption = customTimeBatchSize. Running time (sec) per batch. Default: 60.
#dontDistribute: False # boolean. Replicate tests instead of distributing when multiple agents are used in the job. Default: False.
# Reporting options
#testRunTitle: # string. Test run title.
#platform: # string. Build platform.
#configuration: # string. Build configuration.
#publishRunAttachments: true # boolean. Upload test attachments. Default: true.
#donotPublishTestResults: false # boolean. Optional. Use when runInParallel = false. Disable publishing test results. Default: false.
#failOnMinTestsNotRun: False # boolean. Fail the task if a minimum number of tests are not run. Default: False.
#minimumExpectedTests: '1' # string. Optional. Use when failOnMinTestsNotRun = true. Minimum # of tests. Default: 1.
输入
azureSubscription
-
Azure 资源管理器连接
输入别名: ConnectedServiceName
.
string
。
指定配置有工作负荷标识联合身份验证的 Azure 资源管理器服务连接,以在集成测试中使用 AzurePipelinesCredential。 有关详细信息,请参阅 在集成测试中使用 AzurePipelinesCredential。
注释
此输入仅支持配置为使用工作负荷标识联合的 ARM 服务连接。
testSelector
-
使用 选择测试
string
。 必填。 允许的值:testAssemblies
(测试程序集)、testPlan
(测试计划)、testRun
(测试运行)。 默认值:testAssemblies
。
- 测试程序集: 指定一个或多个包含测试的测试程序集。 可以选择指定筛选条件以仅选择特定测试。
- 测试计划: 运行与测试计划关联的自动测试方法的测试计划。 若要详细了解如何将测试与测试用例工作项相关联,请参阅 将自动测试与测试用例关联。
- 测试运行: 设置环境以从 测试计划运行测试时使用此选项。 在持续集成/持续部署(CI/CD)管道中运行测试时,不应使用此选项。
testAssemblyVer2
-
测试文件
string
。 当 testSelector = testAssemblies
时,需要此选项。 默认值:**\bin\**\*test.dll\n**\bin\**\*tests.dll
。
从指定的文件运行测试。 可以通过分别指定 .orderedtest
和 .webtest
文件来运行有序测试和 Web 测试。 若要运行 .webtest
,需要 Visual Studio 2017 Update 4 或更高版本。 文件路径相对于搜索文件夹。 此输入支持多行 微型模式。
# Example
- task: VSTest@3
inputs:
testSelector: 'testAssemblies'
testAssemblyVer2: |
**\*test*.dll
!**\*TestAdapter.dll
!**\obj\**
testPlan
-
测试计划
string
。 当 testSelector = testPlan
时,需要此选项。
指定包含具有自动测试用例的测试套件的测试计划。
testSuite
-
测试套件
string
。 当 testSelector = testPlan
时,需要此选项。
指定一个或多个包含自动测试用例的测试套件。 测试用例工作项必须与 自动化测试方法相关联。
testConfiguration
-
测试配置
string
。 当 testSelector = testPlan
时,需要此选项。
指定测试配置。
tcmTestRun
-
测试运行
string
。 可选。 当 testSelector = testRun
时使用。 默认值:$(test.RunId)
。
指定从 测试计划触发自动测试运行时使用的基于测试运行的基于测试的选择。 此选项不能用于在 CI/CD 管道中运行测试。
searchFolder
-
搜索文件夹
string
。 必填。 默认值:$(System.DefaultWorkingDirectory)
。
指定要搜索测试程序集的文件夹。
resultsFolder
-
“测试结果”文件夹
string
。 默认值:$(Agent.TempDirectory)\TestResults
。
指定要存储测试结果的文件夹。 使用默认目录时,会在管道运行结束时清理该目录。 在运行测试之前,在 vstest
任务开始时,将始终清理结果目录。 如果提供,则相对文件夹路径将被视为相对于 $(Agent.TempDirectory)
。
testFiltercriteria
-
测试筛选器条件
string
。 可选。 当 testSelector = testAssemblies
时使用。
指定从测试程序集筛选测试的其他条件。 例如: Priority=1|Name=MyTestMethod
。 可用于 -
否定筛选器。 有关筛选条件和选项的更多信息,请参阅 搜索和筛选测试列表。
runOnlyImpactedTests
-
仅运行受影响的测试
boolean
。 可选。 当 testSelector = testAssemblies
时使用。 默认值:False
。
自动指定并运行验证代码更改所需的测试。 了解如何使用 测试影响分析。
runAllTestsAfterXBuilds
-
生成数,之后应运行所有测试
string
。 可选。 当 testSelector = testAssemblies && runOnlyImpactedTests = true
时使用。 默认值:50
。
指定要在自动运行所有测试之前执行的生成数。 测试影响分析存储测试用例和源代码之间的映射。 建议定期运行所有测试来重新生成映射。
uiTests
-
测试组合包含 UI 测试
boolean
。 默认值:false
。
若要运行 UI 测试,请确保代理设置为在启用了 Autologon 的情况下 交互模式运行。 在对生成/发布进行排队之前,必须设置代理以交互方式运行。 选中此框不会自动在交互模式下配置代理。 此选项提醒你适当配置代理以避免故障。 VS 2015 和 2017 池中的托管 Windows 代理可用于运行 UI 测试。
vstestLocationMethod
-
使用 选择测试平台
string
。 允许的值:version
、___location
(特定位置)。 默认值:version
。
指定要使用的测试平台。
vsTestVersion
-
测试平台版本
string
。 可选。 当 vstestLocationMethod = version
时使用。 允许的值:latest
、17.0
(Visual Studio 2022)、16.0
(Visual Studio 2019)、15.0
(Visual Studio 2017)、14.0
(Visual Studio 2015)、toolsInstaller
(由工具安装程序安装)。 默认值:latest
。
指定要使用的 Visual Studio 测试版本。 如果指定了最新 ,则此输入将从已安装的允许值列表中选择最新版本。 若要在代理上运行测试而无需 Visual Studio,请使用工具安装程序 选项 安装。 请务必包括 Visual Studio 测试平台安装程序 任务,以便从 NuGet 获取测试平台。
vstestLocation
-
vstest.console.exe 的路径
string
。 可选。 当 vstestLocationMethod = ___location
时使用。
指定 VSTest 的路径。
runSettingsFile
-
设置文件
string
。
指定要用于测试的 runsettings
或 testsettings
文件的路径。 对于 Visual Studio 15.7 及更高版本,请对所有测试类型使用 runsettings
。 了解有关 将 .testsettings
文件转换为 .runsettings
文件的详细信息。
overrideTestrunParameters
-
替代测试运行参数
string
。
替代 TestRunParameters
文件的 runsettings
节或 Properties
文件的 testsettings
节中定义的参数。 例如: -key1 value1 -key2 value2
。
属性。
pathtoCustomTestAdapters
-
自定义测试适配器的路径
string
。
指定自定义测试适配器的目录路径。 将自动发现驻留在测试程序集所在的同一文件夹中的适配器。
runInParallel
-
在多核计算机上并行运行测试
boolean
。 默认值:False
。
如果设置为 true
,则并行运行测试,并利用计算机的可用核心。 如果 MaxCpuCount
文件中指定,这将覆盖 runsettings
。 详细了解如何并行运行 测试。
runTestsInIsolation
-
隔离运行测试
boolean
。 默认值:False
。
在独立进程中运行测试。 这可能会导致 vstest.console.exe 测试过程中的错误更少,但测试可能会运行较慢。 使用多代理作业设置运行时,当前无法使用此选项。
已启用 codeCoverageEnabled
- 代码覆盖率
boolean
。 默认值:False
。
从测试运行中收集代码覆盖率信息。
otherConsoleOptions
-
其他控制台选项
string
。
可以传递给 vstest.console.exe的其他控制台选项。
不支持这些选项,在使用代理作业的 多代理并行 设置运行测试、使用 测试计划运行测试 或 测试运行 选项或选择自定义批处理选项时,将忽略这些选项。 可以使用设置文件来指定这些选项。
distributionBatchType
-
Batch 测试
string
。 允许的值:basedOnTestCases
(基于测试和代理的数量),basedOnExecutionTime
(基于过去的测试运行时间),basedOnAssembly
(基于测试程序集)。 默认值:basedOnTestCases
。
批处理是一组测试。 一批测试同时运行其测试,并针对该批发布结果。 如果任务运行的作业设置为使用多个代理,则每个代理都会选取任何可用的测试批次以并行运行。 可以运行批处理:
基于测试和代理的数量。 基于参与测试运行的测试和代理数的简单批处理。
基于过去运行的测试时间。 此批处理考虑过去运行时间,以创建一批测试,其中每个批处理的运行时间大致相等。
基于测试程序集。 程序集中的测试将一起批处理。
batchingBasedOnAgentsOption
-
Batch 选项
string
。 可选。 当 distributionBatchType = basedOnTestCases
时使用。 允许的值:autoBatchSize
(自动确定批大小)、customBatchSize
(指定批大小)。 默认值:autoBatchSize
。
根据参与测试运行的测试和代理的数量指定简单的批处理。 自动确定批大小后,每个批都包含 (total number of tests / number of agents)
测试。 如果指定了批大小,则每个批将包含指定数量的测试。
customBatchSizeValue
-
每个批处理 的测试数
string
。 当 distributionBatchType = basedOnTestCases && batchingBasedOnAgentsOption = customBatchSize
时,需要此选项。 默认值:10
。
指定批大小。
batchingBasedOnExecutionTimeOption
-
Batch 选项
string
。 可选。 当 distributionBatchType = basedOnExecutionTime
时使用。 允许的值:autoBatchSize
(自动确定批时间),customTimeBatchSize
(指定每个批处理的运行时间)。 默认值:autoBatchSize
。
此批处理考虑过去的运行时间,以创建一批测试,其中每个批处理的运行时间大致相等。 快速运行的测试将一起批处理,而长时间运行的测试可能属于单独的批处理。 当此选项与多代理作业设置一起使用时,总测试时间将减少到最小值。
每个批处理 customRunTimePerBatchValue
- 运行时间(秒)
string
。 当 distributionBatchType = basedOnExecutionTime && batchingBasedOnExecutionTimeOption = customTimeBatchSize
时,需要此选项。 默认值:60
。
指定每个批处理的运行时间(以秒为单位)。
dontDistribute
-
复制测试,而不是在作业 中使用多个代理时进行分发
boolean
。 默认值:False
。
当任务在多代理作业中运行时,选择此选项不会在代理之间分配测试。 每个选定的测试(s)都会在每个代理上重复。 如果代理作业配置为不并行运行或具有多配置选项,则此选项不适用。
testRunTitle
-
测试运行游戏
string
。
指定测试运行的名称。
platform
-
生成平台
string
。
指定应报告测试的生成平台。 如果已在生成任务中为平台定义了变量,请将此变量用于此输入。
configuration
-
生成配置
string
。
指定应报告测试的生成配置。 如果已为生成任务中的配置定义了变量,请将此变量用于此输入。
publishRunAttachments
-
上传测试附件
boolean
。 默认值:true
。
选择加入或退出发布运行级别附件。
donotPublishTestResults
-
禁用发布测试结果
boolean
。 可选。 当 runInParallel = false
时使用。 默认值:false
。
如果启用此选项,则不会将测试结果作为此任务的一部分发布。 请改用 Publish Test Results 任务。
failOnMinTestsNotRun
-
如果未运行最小数量的测试,则任务失败。
boolean
。 默认值:False
。
如果未运行最少数量的测试,则任务失败。 如果对任务输入或基础测试适配器依赖项所做的任何更改仅导致找到所需测试的子集,这可能很有用。
minimumExpectedTests
-
最低测试数#
string
。 可选。 当 failOnMinTestsNotRun = true
时使用。 默认值:1
。
指定要为任务成功运行的最小测试数。 执行的总测试计算为通过、失败和中止的测试的总和。
diagnosticsEnabled
-
在发生灾难性故障时收集高级诊断
boolean
。 默认值:false
。
收集诊断数据以排查灾难性故障,例如测试崩溃。 选中此选项后,将生成序列 XML 文件并将其附加到测试运行。 序列文件包含有关测试运行顺序的信息,因此可以识别潜在的罪魁祸首测试。
collectDumpOn
-
收集进程转储并附加到测试运行报告
string
。 可选。 当 diagnosticsEnabled = true
时使用。 允许的值:onAbortOnly
(仅中止)、always
、never
。 默认值:onAbortOnly
。
收集可用于进一步分析的小型转储。
- onAbortOnly - 仅当测试运行中止时才会收集小型转储。
- 始终 - 无论测试运行是否完成,都将始终收集小型转储。
- 从不 - 无论测试运行是否完成,都不会收集小型转储。
rerunFailedTests
-
重新运行失败的测试
boolean
。 默认值:False
。
重新运行任何失败的测试,直到通过或达到最大尝试次数为止。
rerunType
-
如果测试失败超过指定的阈值,则不要重新运行
string
。 可选。 当 rerunFailedTests = true
时使用。 允许的值:basedOnTestFailurePercentage
(% 失败)、basedOnTestFailureCount
(失败的测试数)。 默认值:basedOnTestFailurePercentage
。
避免在失败率超过指定的阈值时重新运行测试。 如果环境问题导致大规模故障,则这适用。 可以将失败百分比或失败的测试数指定为阈值。
rerunFailedThreshold
-
% 故障
string
。 可选。 当 rerunFailedTests = true && rerunType = basedOnTestFailurePercentage
时使用。 默认值:30
。
当失败的测试用例百分比超过指定的阈值时,避免重新运行测试。 如果环境问题导致大规模故障,则这适用。
失败的测试 rerunFailedTestCasesMaxLimit
- #
string
。 可选。 当 rerunFailedTests = true && rerunType = basedOnTestFailureCount
时使用。 默认值:5
。
当失败的测试用例数量超过指定的限制时,避免重新运行测试。 如果环境问题导致大规模故障,则这适用。
rerunMaxAttempts
-
最大尝试次数 #
string
。 可选。 当 rerunFailedTests = true
时使用。 默认值:3
。
指定重试失败测试的最大次数。 如果测试在达到最大尝试次数之前通过,则不会再次重新运行。
任务控制选项
除了任务输入,所有任务都有控制选项。 有关详细信息,请参阅 “控制”选项和常见任务属性。
输出变量
没有。
注解
使用此任务可以使用 Visual Studio 测试运行程序运行单元和功能测试(Selenium、Appium、Coded UI 测试等)。 除了基于 MSTest 的测试,还可以执行具有 Visual Studio 测试适配器的测试框架,例如 xUnit、NUnit 或 Chutzpah。
通过在 .runsettings 文件中指定适当的目标框架值,测试是否可以执行目标 .NET core 框架。
可以使用此任务的版本 2 在多个代理上分发测试。 有关详细信息,请参阅 使用 Visual Studio 测试任务并行运行测试。
检查先决条件
如果使用的是 Windows 自承载代理,则必须安装以下必备组件:
- .NET Framework 4.6.2 或更高版本
需求
代理必须具有以下功能:
VS测试
可以通过两种方式满足 vstest 需求:
- Visual Studio 安装在代理计算机上。
- 通过使用管道定义中的 Visual Studio 测试平台安装程序任务。
如何运行使用 TestCase 作为数据源的测试?
若要运行使用 TestCase 作为数据源的自动测试,需要以下各项:
- 代理计算机上必须具有 Visual Studio 2017.6 或更高版本。 Visual Studio 测试平台安装程序任务不能用于运行使用 TestCase 作为数据源的测试。
- 创建 PAT,该范围已获得“工作项(完整)”的授权。
- 添加名为
Test.TestCaseAccessToken
的安全生成或发布变量,并将值设置为上一步中创建的 PAT。
使用某些任务选项运行数据驱动的 xUnit 和 NUnit 测试时,我遇到了问题。 是否存在已知限制?
使用 xUnit 和 NUnit 测试框架的数据驱动测试具有一些已知限制,不能用于以下任务选项:
- 重新运行失败的测试。
- 在多个代理和批处理选项上分发测试。
- 测试影响分析。
上述限制是由于这些测试框架的适配器如何发现和报告数据驱动测试。
VSTest 任务是否支持一次运行面向多个目标框架的测试?
是的,从版本 17.3
VSTest 开始,支持一次运行面向多个目标框架的测试。
在此之前,由于 VSTest 平台 端存在限制,因此无法执行此作。
如果要运行属于多个目标框架的测试,则需要通过 Visual Studio 测试平台安装程序安装兼容版本的 VSTest,并将 vsTestVersion
设置为 toolsInstaller
才能使用它。
发布测试结果时,收到此错误:未能发布测试结果:指定的优先级无效?
如果任何测试方法的优先级都设置为 255 以上,请修复代码中的测试方法优先级,然后再次执行测试,则会发生此错误。 可以查看生成的 trx 文件,以查看优先级大于 255 的所有测试。
在集成测试中使用 AzurePipelinesCredential
适用于 .NET、C++、Go、Java、JavaScript 和 Python 的 Azure 标识库 支持工作负荷联合身份验证,因此,从 AzureCLI@2 和 AzurePowerShell@5 任务执行的代码可以使用 Microsoft Entra ID(例如,访问 Azure)使用 AzurePipelinesCredential
类进行身份验证。
许多客户都在从其他任务调用的集成测试中使用 Azure SDK 客户端库。
DotNetCoreCLI@2、Maven@4 和 VSTest@3 任务可以使用 AzurePipelinesCredential
类访问 Azure 资源。
可以将 connectedServiceName
属性设置为使用工作负荷标识联合配置的 Azure 服务连接。
AzurePipelinesCredential
需要设置 SYSTEM_ACCESSTOKEN。
以下示例演示 connectedServiceName
任务上的 DotNetCoreCLI@2
输入。
VSTest@3
和 Maven@4
用法类似。
- task: DotNetCoreCLI@2
inputs:
command: 'run'
connectedServiceName: <Azure service connection configured with workload identity federation>
env:
SYSTEM_ACCESSTOKEN: $(System.AccessToken)
有关 AzurePipelinesCredential
的详细信息,请参阅 使用 AzurePipelinesCredential改进 Azure 服务连接中的安全状况。