Skip to content

Commit

Permalink
Support parallel test runs
Browse files Browse the repository at this point in the history
  • Loading branch information
cklutz committed Sep 6, 2017
1 parent d60be1e commit e64e7ce
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 8 deletions.
59 changes: 59 additions & 0 deletions Tasks/RunOpenCover/Helpers.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -234,4 +234,63 @@ function CheckIfDirectory($filePath)
return $true
}
return $false
}

function SetupRunSettingsFileForParallel {
[cmdletbinding()]
[OutputType([System.String])]
param(
[string]$runInParallelFlag,
[string]$runSettingsFilePath,
[string]$defaultCpuCount
)

if($runInParallelFlag -eq "True")
{
if([string]::Compare([io.path]::GetExtension($runSettingsFilePath), ".testsettings", $True) -eq 0)
{
Write-Warning "Run in Parallel is not supported with testsettings file."
}
else
{
$runSettingsForParallel = [xml]'<?xml version="1.0" encoding="utf-8"?>'
if([System.String]::IsNullOrWhiteSpace($runSettingsFilePath) `
-Or ([string]::Compare([io.path]::GetExtension($runSettingsFilePath), ".runsettings", $True) -ne 0) `
-Or (Test-Path $runSettingsFilePath -pathtype container)) # no file provided so create one and use it for the run
{
Write-Verbose "No runsettings file provided"
$runSettingsForParallel = [xml]'<?xml version="1.0" encoding="utf-8"?>
<RunSettings>
<RunConfiguration>
<MaxCpuCount>0</MaxCpuCount>
</RunConfiguration>
</RunSettings>
'
}
else
{
Write-Verbose "Adding maxcpucount element to runsettings file provided"
$runSettingsForParallel = [System.Xml.XmlDocument](Get-Content $runSettingsFilePath)
$runConfigurationElement = $runSettingsForParallel.SelectNodes("//RunSettings/RunConfiguration")
if($runConfigurationElement.Count -eq 0)
{
$runConfigurationElement = $runSettingsForParallel.RunSettings.AppendChild($runSettingsForParallel.CreateElement("RunConfiguration"))
}

$maxCpuCountElement = $runSettingsForParallel.SelectNodes("//RunSettings/RunConfiguration/MaxCpuCount")
if($maxCpuCountElement.Count -eq 0)
{
$runConfigurationElement.AppendChild($runSettingsForParallel.CreateElement("MaxCpuCount"))
}
}

$runSettingsForParallel.RunSettings.RunConfiguration.MaxCpuCount = $defaultCpuCount
$tempFile = [io.path]::GetTempFileName()
$runSettingsForParallel.Save($tempFile)
Write-Verbose "Temporary runsettings file created at $tempFile"
return $tempFile
}
}

return $runSettingsFilePath
}
2 changes: 2 additions & 0 deletions Tasks/RunOpenCover/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ Use the following options to select tests and control how the tests are run and

- **Run Settings File:** Path to a runsettings or testsettings file can be specified here. The path can be to a file in the repository or a path to file on disk. Use $(Build.SourcesDirectory) to access the root project folder. [Click here](https://msdn.microsoft.com/library/jj635153.aspx) for more information on these files.

- **Run in Parallel:** If set, tests will run in parallel leveraging available cores of the machine. [Click here](https://aka.ms/paralleltestexecution) to learn more about how tests are run in parallel.

- **OpenCover Filter Criteria:** Filter what should be considered by OpenCover for code coverage. For example, "-\*[\*Tests.\*]".

- **Disable OpenCover:** This allows you to only run the tests and not run OpenCover. Thus, the tasks behaves similar to to stock VSTest task from TFS / VSTS.
Expand Down
10 changes: 5 additions & 5 deletions Tasks/RunOpenCover/RunOpenCover.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,10 @@ try {
# we should escape them by a backslash.
$vsconsoleArgs = $vsconsoleArgs.Replace('"', '\"')

$openCoverReport = "$tempDir\OpenCover.xml"
$coberturaReport = "$tempDir\Cobertura.xml"
$reportDirectory = "$tempDir\CoverageReport"

$openCoverConsoleArgs = "-register:user"
if ($openCoverFilters) {
# Only append filters, if there actually is a value. This way,
Expand All @@ -181,18 +185,14 @@ try {
$openCoverConsoleArgs += " -target:""$vsconsoleExe"""
$openCoverConsoleArgs += " -targetargs:""$vsconsoleArgs"""
$openCoverConsoleArgs += " -mergeoutput"
$openCoverConsoleArgs += " -output:""$tempDir\OpenCover.xml"""
$openCoverConsoleArgs += " -output:""$openCoverReport"""
$openCoverConsoleArgs += " -mergebyhash"
$openCoverConsoleArgs += " -returntargetcode"
if ($openCoverAdditionalCommandLine) {
$openCoverConsoleArgs += " "
$openCoverConsoleArgs += $openCoverAdditionalCommandLine
}

$openCoverReport = "$tempDir\OpenCover.xml"
$coberturaReport = "$tempDir\Cobertura.xml"
$reportDirectory = "$tempDir\CoverageReport"

$coberturaConverterArgs = "-input:""$openCoverReport"""
$coberturaConverterArgs += " -output:""$coberturaReport"""
$coberturaConverterArgs += " -sources:""$sourcesDirectory"""
Expand Down
7 changes: 6 additions & 1 deletion Tasks/RunOpenCover/RunOpenCoverTask.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ try {
$toolsLocationMethod = Get-VstsInput -Name toolsLocationMethod
$toolsBaseDirectory = Get-VstsInput -Name toolsBaseDirectory
$runSettingsFile = Get-VstsInput -Name runSettingsFile
$runInParallel = Get-VstsInput -Name runInParallel -AsBool

Write-Verbose "SourcesDirectory: $sourcesDirectory"
Write-Verbose "testAssembly: $testAssembly"
Expand All @@ -44,6 +45,7 @@ try {
Write-Verbose "runSettingsFile: $runSettingsFile"
Write-Verbose "toolsLocationMethod: $toolsLocationMethod"
Write-Verbose "toolsBaseDirectory: $toolsBaseDirectory"
Write-Verbose "runInParallel: $runInParallel"

if ($toolsLocationMethod -and $toolsLocationMethod -eq "location") {
if (-Not (Test-Path $toolsBaseDirectory)) {
Expand Down Expand Up @@ -101,6 +103,9 @@ try {
}
}

$defaultCpuCount = "0"
$runSettingsFileWithParallel = [string](SetupRunSettingsFileForParallel $runInParallel $runSettingsFile $defaultCpuCount)

. $PSScriptRoot\RunOpenCover.ps1 `
-sourcesDirectory $sourcesDirectory `
-testAssembly $testAssembly `
Expand All @@ -117,7 +122,7 @@ try {
-publishRunAttachments:$publishRunAttachments `
-taskMode `
-toolsBaseDirectory $toolsBaseDirectory `
-runSettingsFile $runSettingsFile
-runSettingsFile $runSettingsFileWithParallel

} catch {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
"loc.input.help.testFiltercriteria": "Additional criteria to filter tests from Test assemblies. For example: Priority=1|Name=MyTestMethod",
"loc.input.label.runSettingsFile": "Run Settings File",
"loc.input.help.runSettingsFile": "Path to runsettings file to use with the tests. Use `$(Build.SourcesDirectory)` to access the Project folder.",
"loc.input.label.runInParallel": "Run In Parallel (Experimental)",
"loc.input.help.runInParallel": "Enable parallel execution of your tests (Experimental).",
"loc.input.label.openCoverFilters": "OpenCover Filter criteria",
"loc.input.help.openCoverFilters": "Additional criteria to filter what OpenCover considers.",
"loc.input.label.disableCodeCoverage": "Disable OpenCover",
Expand Down
11 changes: 10 additions & 1 deletion Tasks/RunOpenCover/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"version": {
"Major": 1,
"Minor": 0,
"Patch": 11
"Patch": 15
},
"minimumAgentVersion": "1.95.0",
"instanceNameFormat": "Tests/Coverage - $(testAssembly)",
Expand Down Expand Up @@ -75,6 +75,15 @@
"helpMarkDown": "Path to runsettings file to use with the tests. Use `$(Build.SourcesDirectory)` to access the Project folder.",
"groupName": "executionOptions"
},
{
"name": "runInParallel",
"type": "boolean",
"label": "Run In Parallel",
"defaultValue": "false",
"required": false,
"helpMarkDown": "Enable parallel execution of your tests.",
"groupName": "executionOptions"
},
{
"name": "openCoverFilters",
"type": "string",
Expand Down
11 changes: 10 additions & 1 deletion Tasks/RunOpenCover/task.loc.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"version": {
"Major": 1,
"Minor": 0,
"Patch": 11
"Patch": 15
},
"minimumAgentVersion": "1.95.0",
"instanceNameFormat": "ms-resource:loc.instanceNameFormat",
Expand Down Expand Up @@ -75,6 +75,15 @@
"helpMarkDown": "ms-resource:loc.input.help.runSettingsFile",
"groupName": "executionOptions"
},
{
"name": "runInParallel",
"type": "boolean",
"label": "ms-resource:loc.input.label.runInParallel",
"defaultValue": "false",
"required": false,
"helpMarkDown": "ms-resource:loc.input.help.runInParallel",
"groupName": "executionOptions"
},
{
"name": "openCoverFilters",
"type": "string",
Expand Down

0 comments on commit e64e7ce

Please sign in to comment.