问题
I would like to control which unit tests run in a VSTS build pipeline based on which files were modified in the PR that triggered the pipeline.
Is there a way to detect which files were modified in a pull request during a VSTS build triggered for that PR, and then choose which tests to run based on that information?
回答1:
Yes, this is possible:
Manual Approach
One approach to this is to have a custom Powershell task in your pipeline that sets a variable based on whether a certain file was changed in the last commit. So you might have a pipeline like this:
Your 'Set criteria for files in last commit' step would contain an inline powershell script to set a Build variable depending on whether a certain string was within the list of files from the last commit, where 'YOURFILE.cs' is the file you are interested in (you could replace this with any logic):
$ChangedFiles = & git show --pretty="" --name-only
if ($ChangedFiles -like '*YOURFILE.cs*') {
Write-Output ("##vso[task.setvariable variable=RunTests;]true")
}
else{
Write-Output ("##vso[task.setvariable variable=RunTests;]false")
}
Then, in your 'Run tests' step, you can set a conditional control option which will look at this variable to decide whether or not to run that set of tests:
Visual Studio Test Impact Analysis
If you are using the Visual Studio Test task to run your tests, there is also a built in option to only run the tests that are impacted by the build, via the Test Impact Analysis feature.
This may be of use to you but doesn't directly answer your specific question and has a wider scope. More information is available on the Azure Devops documentation pages
来源:https://stackoverflow.com/questions/53331777/vsts-build-choose-which-unit-tests-run-depending-on-which-files-were-modified