tasks/test.tasks.ps1
|
# <copyright file="test.tasks.ps1" company="Endjin Limited"> # Copyright (c) Endjin Limited. All rights reserved. # </copyright> . $PSScriptRoot/test.properties.ps1 # Synopsis: Run .NET solution tests with 'dotnet-coverage' to collect code coverage task RunTestsWithDotNetCoverage -If {$SolutionToBuild} { # Setup tasks that should always run even if the build fails after this point (e.g. because of test failures). # - Upload test logs to ADO $_uploadTestLog = { if ((Test-Path $DotNetTestLogFile) -and $IsAzureDevOps) { Write-Build White "Publishing test log file as Azure Pipeline artifact" Write-Host "##vso[artifact.upload artifactname=logs]$((Resolve-Path $DotNetTestLogFile).Path)" } } Register-OnExitAction -Action $_uploadTestLog # - Generate test reports $_generateTestReports = { Invoke-Build -File "$PSScriptRoot/report.tasks.ps1" -Task TestReport } Register-OnExitAction -Action $_generateTestReports # Detect Microsoft Testing Platform (MTP) usage $isMtp = $false try { # Rather than re-implement all the detection logic ourselves with respect to # locating a valid global.json etc., we simply rely on 'dotnet test' to do # this for us. When detecting a solution or project using the VSTest # platform it outputs a different message to reflect how it will interpret # any command-line parameters passed to it. $helpOutput = & dotnet test $SolutionToBuild --help 2>&1 | Out-String if ($helpOutput -match "\.NET Test Command for Microsoft\.Testing\.Platform") { $isMtp = $true Write-Build White "Microsoft Testing Platform detected." } else { Write-Build White "VSTest detected." } } catch { Write-Build Yellow "Failed to detect testing platform via help command: $($_.Exception.Message)`nAssuming VSTest." } # Setup the appropriate CI/CD platform test logger, unless explicitly disabled (or using MTP) if (!$isMtp -and !$DisableCicdServerLogger) { if ($script:IsAzureDevOps) { Write-Build Green "Configuring Azure Pipelines test logger" $script:DotNetTestLoggers += "AzurePipelines" } elseif ($script:IsGitHubActions) { Write-Build Green "Configuring GitHub Actions test logger" $script:DotNetTestLoggers += "GitHubActions" } } # Evaluate the file logger properties so we can pass them to 'dotnet test' $script:_fileLoggerProps = Resolve-Value $DotNetTestFileLoggerProps # Setup the arguments we need to pass to 'dotnet test' $dotnetTestArgs = @( "--configuration", $Configuration "--no-build" "--no-restore" "--verbosity", $LogLevel ) if ($isMtp) { $dotnetTestArgs += _GetDotNetTestParamsForMtp } else { $dotnetTestArgs += _GetDotNetTestParamsForVsTest } $coverageOutput = "coverage{0}.cobertura.xml" -f ($TargetFrameworkMoniker ? ".$TargetFrameworkMoniker" : "") if ($TargetFrameworkMoniker) { $dotnetTestArgs += @("--framework", $TargetFrameworkMoniker) } Remove-Item $coverageOutput -ErrorAction Ignore -Force $dotnetCoverageArgs = @( "collect" "-o", $coverageOutput "-f", "cobertura" ) if ($DotNetCoverageSettingsFile -and (Test-Path $DotNetCoverageSettingsFile)) { Write-Build Green "Using dotnet-coverage settings file: $DotNetCoverageSettingsFile" $dotnetCoverageArgs += @("-s", $DotNetCoverageSettingsFile) } # Ensure the dotnet-coverage global tool is installed, as we need it to collect the code coverage data Install-DotNetTool -Name "dotnet-coverage" -Global # Add any custom test arguments that have been specified if ($AdditionalTestArgs) { $dotnetTestArgs += $AdditionalTestArgs } $dotnetCoverageArgs += @( "dotnet" "test" ) Write-Verbose "CmdLine: $dotnetCoverageArgs $dotnetTestArgs" -Verbose exec { & dotnet-coverage @dotnetCoverageArgs @dotnetTestArgs } } task RunDotNetTests ` -If {!$SkipTest -and !$SkipDotNetTests} ` -After TestCore ` RunTestsWithDotNetCoverage |