Test: Visual Studio Test

Run unit and functional tests (Selenium, Appium, Coded UI test, and more) using the Visual Studio Test Runner. Other than MSTest-based tests, test frameworks that have a Visual Studio test adapter, such as xUnit, NUnit, Chutzpah, can also be run.

Tests that target the .NET core framework can be run by specifying the appropriate target framework value.

Tests can be distributed on multiple agents using version 2 of this task.

Demands

The agent must have the following capability:

vstest

The vstest demand can be satisfied in two ways:

  1. Visual Studio is installed on the agent machine.

  2. By using the Visual Studio Test Platform Installer task in the pipeline definition.

YAML snippet

# Visual Studio Test
# 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).
- task: VSTest@2
  inputs:
    #testSelector: 'testAssemblies' # Options: testAssemblies, testPlan, testRun
    #testAssemblyVer2: '**\*test*.dll!**\*TestAdapter.dll!**\obj\**' # Required when testSelector == TestAssemblies
    #testPlan: # Required when testSelector == TestPlan
    #testSuite: # Required when testSelector == TestPlan
    #testConfiguration: # Required when testSelector == TestPlan
    #tcmTestRun: '$(test.RunId)' # Optional
    #searchFolder: '$(System.DefaultWorkingDirectory)' 
    #testFiltercriteria: # Optional
    #runOnlyImpactedTests: False # Optional
    #runAllTestsAfterXBuilds: '50' # Optional
    #uiTests: false # Optional
    #vstestLocationMethod: 'version' # Optional. Options: version, location
    #vsTestVersion: 'latest' # Optional. Options: latest, 15.0, 14.0, toolsInstaller
    #vstestLocation: # Optional
    #runSettingsFile: # Optional
    #overrideTestrunParameters: # Optional
    #pathtoCustomTestAdapters: # Optional
    #runInParallel: False # Optional
    #runTestsInIsolation: False # Optional
    #codeCoverageEnabled: False # Optional
    #otherConsoleOptions: # Optional
    #distributionBatchType: 'basedOnTestCases' # Optional. Options: basedOnTestCases, basedOnExecutionTime, basedOnAssembly
    #batchingBasedOnAgentsOption: 'autoBatchSize' # Optional. Options: autoBatchSize, customBatchSize
    #customBatchSizeValue: '10' # Required when distributionBatchType == BasedOnTestCases && BatchingBasedOnAgentsOption == CustomBatchSize
    #batchingBasedOnExecutionTimeOption: 'autoBatchSize' # Optional. Options: autoBatchSize, customTimeBatchSize
    #customRunTimePerBatchValue: '60' # Required when distributionBatchType == BasedOnExecutionTime && BatchingBasedOnExecutionTimeOption == CustomTimeBatchSize
    #dontDistribute: False # Optional
    #testRunTitle: # Optional
    #platform: # Optional
    #configuration: # Optional
    #publishRunAttachments: true # Optional
    #diagnosticsEnabled: True # Optional
    #collectDumpOn: 'onAbortOnly' # Optional. Options: onAbortOnly, always
    #rerunFailedTests: False # Optional
    #rerunType: 'basedOnTestFailurePercentage' # Optional. Options: basedOnTestFailurePercentage, basedOnTestFailureCount
    #rerunFailedThreshold: '30' # Optional
    #rerunFailedTestCasesMaxLimit: '5' # Optional
    #rerunMaxAttempts: '3' # Optional

Arguments

ArgumentDescription
testSelector
Select tests using
(Required)
  • Test assembly: Use this option to specify one or more test assemblies that contain your tests. You can optionally specify a filter criteria to select only specific tests.
  • Test plan: Use this option to run tests from your test plan that have an automated test method associated with it. To learn more about how to associate tests with a test case work item, see Associate automated tests with test cases.
  • Test run: Use this option when you are setting up an environment to run tests from test plans. This option should not be used when running tests in a continuous integration /continuous deployment (CI/CD) pipeline.
testAssemblyVer2
Test assemblies
(Required) Run tests from the specified files.
Ordered tests and webtests can be run by specifying the .orderedtest and .webtest files respectively. To run .webtest, Visual Studio 2017 Update 4 or higher is needed.

The file paths are relative to the search folder. Supports multiple lines of minimatch patterns. More Information
testPlan
Test plan
(Required) Select a test plan containing test suites with automated test cases.
testSuite
Test suite
(Required) Select one or more test suites containing automated test cases. Test case work items must be associated with an automated test method. Learn more.
testConfiguration
Test configuration
(Required) Select Test Configuration.
tcmTestRun
Test Run
(Optional) Test run based selection is used when triggering automated test runs from test plans. This option cannot be used for running tests in the CI/CD pipeline.
searchFolder
Search folder
(Required) Folder to search for the test assemblies.
testFiltercriteria
Test filter criteria
(Optional) Additional criteria to filter tests from Test assemblies. For example: Priority=1|Name=MyTestMethod. More information
runOnlyImpactedTests
Run only impacted tests
(Optional) Automatically select, and run only the tests needed to validate the code change. More information
runAllTestsAfterXBuilds
Number of builds after which all tests should be run
(Optional) Number of builds after which to automatically run all tests. Test Impact Analysis stores the mapping between test cases and source code. It is recommended to regenerate the mapping by running all tests, on a regular basis.
uiTests
Test mix contains UI tests
(Optional) To run UI tests, ensure that the agent is set to run in interactive mode with autologon enabled. Setting up an agent to run interactively must be done before queueing the build / release. Checking this box does not configure the agent in interactive mode automatically. This option in the task is to only serve as a reminder to configure agent appropriately to avoid failures. Hosted Windows agents from the VS 2015 and 2017 pools can be used to run UI tests.
vstestLocationMethod
Select test platform using
(Optional) Specify which test platform should be used.
vsTestVersion
Test platform version
(Optional) The version of Visual Studio test to use. If latest is specified it chooses Visual Studio 2017 or Visual Studio 2015 depending on what is installed. Visual Studio 2013 is not supported. To run tests without needing Visual Studio on the agent, use the ‘Installed by tools installer’ option. Be sure to include the ‘Visual Studio Test Platform Installer’ task to acquire the test platform from NuGet.
vstestLocation
Path to vstest.console.exe
(Optional) Specify the path to VSTest.
runSettingsFile
Settings file
(Optional) Path to runsettings or testsettings file to use with the tests.Starting with Visual Studio 15.7, it is recommended to use runsettings for all types of tests. To learn more about converting a .testsettings file to a .runsettings file, see this topic.
overrideTestrunParameters
Override test run parameters
(Optional) Override parameters defined in the TestRunParameters section of runsettings file or Properties section of testsettings file. For example: -key1 value1 -key2 value2. Note: Properties specified in testsettings file can be accessed via the TestContext using Visual Studio 2017 Update 4 or higher
pathtoCustomTestAdapters
Path to custom test adapters
(Optional) Directory path to custom test adapters. Adapters residing in the same folder as the test assemblies are automatically discovered.
runInParallel
Run tests in parallel on multi-core machines
(Optional) If set, tests will run in parallel leveraging available cores of the machine. This will override the MaxCpuCount if specified in your runsettings file. Click here to learn more about how tests are run in parallel.
runTestsInIsolation
Run tests in isolation
(Optional) Runs the tests in an isolated process. This makes vstest.console.exe process less likely to be stopped on an error in the tests, but tests might run slower. This option currently cannot be used when running with the multi-agent job setting.
codeCoverageEnabled
Code coverage enabled
(Optional) Collect code coverage information from the test run.
otherConsoleOptions
Other console options
(Optional) Other console options that can be passed to vstest.console.exe, as documented here.

These options are not supported and will be ignored when running tests using the ‘Multi agent’ parallel setting of an agent job or when running tests using ‘Test plan’ option. The options can be specified using a settings file instead.

distributionBatchType
Batch tests
(Optional) A batch is a group of tests. A batch of tests runs at a time and results are published for that batch. If the job in which the task runs is set to use multiple agents, each agent picks up any available batches of tests to run in parallel.

Based on number of tests and agents: Simple batching based on the number of tests and agents participating in the test run.

Based on past running time of tests: This batching considers past running time to create batches of tests such that each batch has approximately equal running time.

Based on test assemblies: Tests from an assembly are batched together.
batchingBasedOnAgentsOption
Batch options
(Optional) Simple batching based on the number of tests and agents participating in the test run. When the batch size is automatically determined, each batch contains (total number of tests / number of agents) tests. If a batch size is specified, each batch will contain the specified number of tests.
customBatchSizeValue
Number of tests per batch
(Required) Specify batch size
batchingBasedOnExecutionTimeOption
Batch options
(Optional) This batching considers past running time to create batches of tests such that each batch has approximately equal running time. Quick running tests will be batched together, while longer running tests may belong to a separate batch. When this option is used with the multi-agent job setting, total test time is reduced to a minimum.
customRunTimePerBatchValue
Running time (sec) per batch
(Required) Specify the running time (sec) per batch
dontDistribute
Do not distribute tests and replicate instead when multiple agents are used in the job
(Optional) Choosing this option will not distribute tests across agents when the task is running in a multi-agent job.
Each of the selected test(s) will be repeated on each agent.
The option is not applicable when the agent job is configured to run with no parallelism or with the multi-config option.
testRunTitle
Test run title
(Optional) Provide a name for the test run.
platform
Build platform
(Optional) Build platform against which the tests should be reported. If you have defined a variable for platform in your build task, use that here.
configuration
Build configuration
(Optional) Build configuration against which the tests should be reported. If you have defined a variable for configuration in your build task, use that here.
publishRunAttachments
Upload test attachments
(Optional) Opt in/out of publishing run level attachments.
rerunFailedTests
Rerun failed tests
(Optional) Selecting this option will rerun any failed tests until they pass or the maximum # of attempts is reached.
rerunType
Do not rerun if test failures exceed specified threshold
(Optional) Use this option to avoid rerunning tests when failure rate crosses the specified threshold. This is applicable if any environment issues leads to massive failures.
You can specify % failures or # of failed tests as a threshold.
rerunFailedThreshold
% failure
(Optional) Use this option to avoid rerunning tests when failure rate crosses the specified threshold. This is applicable if any environment issues leads to massive failures.
rerunFailedTestCasesMaxLimit
# of failed tests
(Optional) Use this option to avoid rerunning tests when number of failed test cases crosses specified limit. This is applicable if any environment issues leads to massive failures.
rerunMaxAttempts
Maximum # of attempts
(Optional) Specify the maximum # of times a failed test should be retried. If a test passes before the maximum # of attempts is reached, it will not be rerun further.
Control options

Open source

This task is open source on GitHub. Feedback and contributions are welcome.

Q & A