VSTS Build - Choose which unit tests run depending on which files were modified in a pull request

非 Y 不嫁゛ 提交于 2020-12-11 04:33:18

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!