{"id":31759,"date":"2026-12-01T22:04:13","date_gmt":"2026-12-01T22:04:13","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=31759"},"modified":"2026-12-01T22:04:13","modified_gmt":"2026-12-01T22:04:13","slug":"dotcover-powershell-script-code-coverage-report-with-dotcover","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/dotcover-powershell-script-code-coverage-report-with-dotcover\/","title":{"rendered":"dotCover: Powershell Script code coverage report with dotCover"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">This page shows a PowerShell script that runs .NET unit tests under JetBrains dotCover, writes DetailedXML coverage files, and uses ReportGenerator to convert them to Cobertura and HTML for the Azure DevOps Publish Code Coverage Results task. Below the script you&#039;ll find an explanation of how it works, fixes for several bugs that stop it from running as a file-based pipeline script, a corrected version, and a complete pipeline example.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p class=\"wp-block-paragraph\">Script generating a code coverage report with dotCover and later converting it with Report Generator so that Azure DevOps task Publish Code Coverage Results can use it.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<script src=\"https:\/\/gist.github.com\/rszuban\/b6853b61e8b319e95577258e846aebce.js\"><\/script>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">How the script works<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Azure DevOps cannot read dotCover&#039;s native report formats directly. The Publish Code Coverage Results task expects Cobertura (or JaCoCo) XML. The script above therefore runs in two stages. First, dotCover wraps the test runner and records coverage. Then ReportGenerator converts the output into a format the pipeline understands.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Stage<\/th><th>Tool<\/th><th>Output<\/th><\/tr><\/thead><tbody><tr><td>Find test projects<\/td><td><code>Get-ChildItem<\/code> with <code>*Tests.csproj<\/code><\/td><td>List of test project files<\/td><\/tr><tr><td>Run tests with coverage<\/td><td><code>dotCover.exe cover<\/code> wrapping <code>dotnet test<\/code> or <code>vstest.console.exe<\/code><\/td><td><code>&lt;Project&gt;_coverage.xml<\/code> in DetailedXML format<\/td><\/tr><tr><td>Convert to Cobertura<\/td><td>ReportGenerator <code>-reporttypes:Cobertura<\/code><\/td><td><code>coverage-report\/cobertura\/Cobertura.xml<\/code><\/td><\/tr><tr><td>Build HTML report<\/td><td>ReportGenerator <code>-reporttypes:HtmlInline_AzurePipelines<\/code><\/td><td><code>coverage-report\/html\/index.html<\/code><\/td><\/tr><tr><td>Publish<\/td><td><code>PublishCodeCoverageResults<\/code> task<\/td><td>Code Coverage tab on the pipeline run<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Prerequisites on the build agent<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>A <strong>Windows agent<\/strong>. The script uses Windows paths and <code>dotCover.exe<\/code>.<\/li>\n\n\n<li><strong>dotCover command-line tools<\/strong>. You can install them from the JetBrains download page or restore the <code>JetBrains.dotCover.CommandLineTools<\/code> NuGet package, which puts <code>dotCover.exe<\/code> in its <code>tools<\/code> folder.<\/li>\n\n\n<li><strong>.NET SDK<\/strong> if you use <code>dotnet test<\/code>. If you use <code>vstest.console.exe<\/code> instead, you need Visual Studio or Build Tools with the test platform installed.<\/li>\n\n\n<li><strong>ReportGenerator<\/strong>, installed as a .NET tool. Install it under the account the agent runs as, because global tools go into that user&#039;s profile. The original path <code>C:\\Windows\\ServiceProfiles\\NetworkService\\.dotnet\\tools<\/code> only applies when the agent service runs as Network Service.<\/li>\n\n\n<li>Test projects that build successfully in the configuration you pass (for example <code>Release<\/code>).<\/li>\n<\/ul>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-comment\"># Install ReportGenerator for the current user (run as the agent account)<\/span>\ndotnet tool install --<span class=\"hljs-keyword\">global<\/span> dotnet-reportgenerator-globaltool\n\n<span class=\"hljs-comment\"># Or install it into a folder you control, which is better for build agents<\/span>\ndotnet tool install dotnet-reportgenerator-globaltool --tool-path C:\\tools\\reportgenerator\n\n<span class=\"hljs-comment\"># Locate vstest.console.exe instead of hardcoding a Visual Studio 2017 path<\/span>\n&amp; <span class=\"hljs-string\">\"${env:ProgramFiles(x86)}\\Microsoft Visual Studio\\Installer\\vswhere.exe\"<\/span> -latest -products * -find <span class=\"hljs-string\">\"**\\TestPlatform\\vstest.console.exe\"<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">Problems in the original script<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Azure DevOps macro syntax inside PowerShell.<\/strong> <code>$(System.DefaultWorkingDirectory)<\/code> and <code>$(BuildConfiguration)<\/code> are only replaced when the script is pasted into an *inline* task. In a <code>.ps1<\/code> file, PowerShell reads <code>$( ... )<\/code> as a subexpression and tries to run a command called <code>System.DefaultWorkingDirectory<\/code>, which fails. Use the environment variables <code>$env:SYSTEM_DEFAULTWORKINGDIRECTORY<\/code> and <code>$env:BUILDCONFIGURATION<\/code> instead.<\/li>\n\n\n<li><strong>Both runners execute by default.<\/strong> If you don&#039;t comment one region out, every test project runs twice, and the vstest run overwrites the <code>dotnet test<\/code> coverage file.<\/li>\n\n\n<li><strong>Failed tests are not detected.<\/strong> <code>$?<\/code> only reflects the last command. Without <code>\/ReturnTargetExitCode<\/code>, dotCover also returns its own exit code, not the test runner&#039;s. A failing test can therefore leave the pipeline green.<\/li>\n\n\n<li><strong>Stray space in <code>-reports: $reportsFilePaths<\/code>.<\/strong> In the Cobertura call, the value is passed with a leading space. Write it as <code>-reports:&lt;paths&gt;<\/code>, as the HTML call already does.<\/li>\n\n\n<li><strong>Missing comma<\/strong> after <code>&quot;\/Filters=$dotCoverAssemblyFilter&quot;<\/code> in the dotnet argument array. It happens to work, because a newline also separates elements inside <code>@( )<\/code>, but it&#039;s inconsistent and easy to break when editing.<\/li>\n\n\n<li><strong>Hardcoded Visual Studio 2017 Build Tools path.<\/strong> That path is outdated on most current agents. Locate the runner with <code>vswhere<\/code> instead.<\/li>\n\n\n<li><strong>Comment typos.<\/strong> The comments say &quot;apropriate&quot;, &quot;scrip&quot; and &quot;haven&#039;t passed&quot;.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Corrected script<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">This version keeps the <code>dotnet test<\/code> path only, since that&#039;s the common case for SDK-style projects. It reads pipeline values from environment variables and runs every test project. It still generates the reports when tests fail, then fails the step at the end. Save it in the repository, for example as <code>build\/coverage.ps1<\/code>.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">param(\n    &#91;string]$DotCoverExecutable        = <span class=\"hljs-string\">'C:\\Program Files\\DotCover\\dotCover.exe'<\/span>,\n    &#91;string]$ReportGeneratorExecutable = <span class=\"hljs-string\">\"$env:USERPROFILE\\.dotnet\\tools\\reportgenerator.exe\"<\/span>,\n    &#91;string]$TestProjectPattern        = <span class=\"hljs-string\">'*Tests.csproj'<\/span>,\n    &#91;string]$AssemblyFilter            = <span class=\"hljs-string\">'+:module=MyApplication*;-:module=*Tests'<\/span>,\n    &#91;string]$WorkingDirectory          = $env:SYSTEM_DEFAULTWORKINGDIRECTORY,\n    &#91;string]$Configuration             = $env:BUILDCONFIGURATION\n)\n\n$ErrorActionPreference = <span class=\"hljs-string\">'Stop'<\/span>\n<span class=\"hljs-keyword\">if<\/span> (-not $WorkingDirectory) { $WorkingDirectory = (Get-Location).Path }\n<span class=\"hljs-keyword\">if<\/span> (-not $Configuration)    { $Configuration = <span class=\"hljs-string\">'Release'<\/span> }\n\n<span class=\"hljs-keyword\">foreach<\/span> ($exe in $DotCoverExecutable, $ReportGeneratorExecutable) {\n    <span class=\"hljs-keyword\">if<\/span> (-not (Test-Path -Path $exe)) { <span class=\"hljs-keyword\">throw<\/span> <span class=\"hljs-string\">\"Executable not found: $exe\"<\/span> }\n}\n$dotnetExecutable = (Get-Command dotnet).Source\n\n<span class=\"hljs-comment\"># Clean output structure: coverage-report\\cobertura and coverage-report\\html<\/span>\n$reportDirectory = Join-Path $WorkingDirectory <span class=\"hljs-string\">'coverage-report'<\/span>\n$coberturaDir    = Join-Path $reportDirectory <span class=\"hljs-string\">'cobertura'<\/span>\n$htmlDir         = Join-Path $reportDirectory <span class=\"hljs-string\">'html'<\/span>\n<span class=\"hljs-keyword\">if<\/span> (Test-Path $reportDirectory) { Remove-Item $reportDirectory -Recurse -Force }\n<span class=\"hljs-keyword\">New<\/span>-Item -ItemType Directory -Path $coberturaDir, $htmlDir -Force | Out-<span class=\"hljs-keyword\">Null<\/span>\n\n$testProjects = Get-ChildItem -Path $WorkingDirectory -Filter $TestProjectPattern -Recurse -File\n<span class=\"hljs-keyword\">if<\/span> (-not $testProjects) { <span class=\"hljs-keyword\">throw<\/span> <span class=\"hljs-string\">\"No files matched $TestProjectPattern.\"<\/span> }\n\n$failedProjects = @()\n<span class=\"hljs-keyword\">foreach<\/span> ($project in $testProjects) {\n    $output = Join-Path $reportDirectory <span class=\"hljs-string\">\"$($project.BaseName)_coverage.xml\"<\/span>\n\n    $dotCoverArgs = @(\n        <span class=\"hljs-string\">'cover'<\/span>,\n        <span class=\"hljs-string\">\"\/TargetExecutable=$dotnetExecutable\"<\/span>,\n        <span class=\"hljs-string\">\"\/TargetWorkingDir=$($project.DirectoryName)\"<\/span>,\n        <span class=\"hljs-string\">\"\/TargetArguments=test --configuration $Configuration\"<\/span>,\n        <span class=\"hljs-string\">\"\/Output=$output\"<\/span>,\n        <span class=\"hljs-string\">'\/ReportType=DetailedXML'<\/span>,\n        <span class=\"hljs-string\">\"\/Filters=$AssemblyFilter\"<\/span>,\n        <span class=\"hljs-string\">'\/ReturnTargetExitCode'<\/span>\n    )\n\n    Write-Output <span class=\"hljs-string\">\"Running dotCover for $($project.Name)\"<\/span>\n    &amp; $DotCoverExecutable @dotCoverArgs\n    <span class=\"hljs-keyword\">if<\/span> ($LASTEXITCODE -ne <span class=\"hljs-number\">0<\/span>) { $failedProjects += $project.Name }\n}\n\n$reports = (Get-ChildItem -Path $reportDirectory -Filter <span class=\"hljs-string\">'*_coverage.xml'<\/span> -File).FullName\n<span class=\"hljs-keyword\">if<\/span> (-not $reports) { <span class=\"hljs-keyword\">throw<\/span> <span class=\"hljs-string\">'No coverage reports were generated.'<\/span> }\n$reportList = $reports -join <span class=\"hljs-string\">';'<\/span>\n\n&amp; $ReportGeneratorExecutable <span class=\"hljs-string\">\"-reports:$reportList\"<\/span> <span class=\"hljs-string\">\"-targetdir:$coberturaDir\"<\/span> <span class=\"hljs-string\">'-reporttypes:Cobertura'<\/span>\n<span class=\"hljs-keyword\">if<\/span> ($LASTEXITCODE -ne <span class=\"hljs-number\">0<\/span>) { <span class=\"hljs-keyword\">throw<\/span> <span class=\"hljs-string\">'ReportGenerator (Cobertura) failed.'<\/span> }\n\n&amp; $ReportGeneratorExecutable <span class=\"hljs-string\">\"-reports:$reportList\"<\/span> <span class=\"hljs-string\">\"-targetdir:$htmlDir\"<\/span> <span class=\"hljs-string\">'-reporttypes:HtmlInline_AzurePipelines'<\/span>\n<span class=\"hljs-keyword\">if<\/span> ($LASTEXITCODE -ne <span class=\"hljs-number\">0<\/span>) { <span class=\"hljs-keyword\">throw<\/span> <span class=\"hljs-string\">'ReportGenerator (HTML) failed.'<\/span> }\n\n<span class=\"hljs-keyword\">if<\/span> ($failedProjects) { <span class=\"hljs-keyword\">throw<\/span> <span class=\"hljs-string\">\"Tests failed in: $($failedProjects -join ', ')\"<\/span> }<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<ul class=\"wp-block-list\">\n<li><code>\/TargetWorkingDir<\/code> runs <code>dotnet test<\/code> from the project folder. You don&#039;t need to pass a quoted project path, which avoids argument-quoting problems with paths that contain spaces.<\/li>\n\n\n<li><code>\/ReturnTargetExitCode<\/code> makes dotCover return the exit code of <code>dotnet test<\/code>, so <code>$LASTEXITCODE<\/code> tells you whether the tests failed.<\/li>\n\n\n<li>The filter includes your application modules and excludes the test assemblies, so test code doesn&#039;t inflate the coverage percentage.<\/li>\n\n\n<li>Every parameter can be overridden, which lets you run the script on a developer machine: <code>.\/build\/coverage.ps1 -WorkingDirectory C:\\src\\MyApp -Configuration Debug<\/code>.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Note:<\/strong> Recent dotCover releases have reworked parts of the console runner&#039;s syntax and options. If an option is rejected, run <code>dotCover.exe help cover<\/code> on the agent and check which options your installed version supports.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Using vstest.console instead of dotnet test<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Older .NET Framework test projects sometimes have to run through <code>vstest.console.exe<\/code>. In that case, build the solution first, find the compiled test DLL, and replace the argument array in the loop with the one below. As in the <code>dotnet<\/code> version, setting the working directory avoids quoting the DLL path.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">$vsTest = &amp; <span class=\"hljs-string\">\"${env:ProgramFiles(x86)}\\Microsoft Visual Studio\\Installer\\vswhere.exe\"<\/span> -latest -products * -find <span class=\"hljs-string\">\"**\\TestPlatform\\vstest.console.exe\"<\/span> | Select-Object -First <span class=\"hljs-number\">1<\/span>\n\n$testDll = Get-ChildItem -Path (Join-Path $project.DirectoryName <span class=\"hljs-string\">\"bin\\$Configuration\"<\/span>) -Filter <span class=\"hljs-string\">\"$($project.BaseName).dll\"<\/span> -Recurse -File | Select-Object -First <span class=\"hljs-number\">1<\/span>\n<span class=\"hljs-keyword\">if<\/span> (-not $testDll) { <span class=\"hljs-keyword\">throw<\/span> <span class=\"hljs-string\">\"Build $($project.Name) before running coverage.\"<\/span> }\n\n$dotCoverArgs = @(\n    <span class=\"hljs-string\">'cover'<\/span>,\n    <span class=\"hljs-string\">\"\/TargetExecutable=$vsTest\"<\/span>,\n    <span class=\"hljs-string\">\"\/TargetWorkingDir=$($testDll.DirectoryName)\"<\/span>,\n    <span class=\"hljs-string\">\"\/TargetArguments=$($testDll.Name)\"<\/span>,\n    <span class=\"hljs-string\">\"\/Output=$output\"<\/span>,\n    <span class=\"hljs-string\">'\/ReportType=DetailedXML'<\/span>,\n    <span class=\"hljs-string\">\"\/Filters=$AssemblyFilter\"<\/span>,\n    <span class=\"hljs-string\">'\/ReturnTargetExitCode'<\/span>\n)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">Wiring it into Azure Pipelines<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Azure Pipelines exposes non-secret pipeline variables as environment variables. The name is uppercased and dots become underscores, so <code>BuildConfiguration<\/code> arrives as <code>BUILDCONFIGURATION<\/code> and <code>System.DefaultWorkingDirectory<\/code> as <code>SYSTEM_DEFAULTWORKINGDIRECTORY<\/code>. That&#039;s why the corrected script needs no macro syntax.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\"><span class=\"hljs-selector-tag\">pool<\/span>:\n  <span class=\"hljs-selector-tag\">vmImage<\/span>: <span class=\"hljs-selector-tag\">windows-latest<\/span>   # <span class=\"hljs-selector-tag\">or<\/span> <span class=\"hljs-selector-tag\">a<\/span> <span class=\"hljs-selector-tag\">self-hosted<\/span> <span class=\"hljs-selector-tag\">Windows<\/span> <span class=\"hljs-selector-tag\">pool<\/span> <span class=\"hljs-selector-tag\">with<\/span> <span class=\"hljs-selector-tag\">dotCover<\/span> <span class=\"hljs-selector-tag\">installed<\/span>\n\n<span class=\"hljs-selector-tag\">variables<\/span>:\n  <span class=\"hljs-selector-tag\">BuildConfiguration<\/span>: <span class=\"hljs-selector-tag\">Release<\/span>\n\n<span class=\"hljs-selector-tag\">steps<\/span>:\n<span class=\"hljs-selector-tag\">-<\/span> <span class=\"hljs-selector-tag\">task<\/span>: <span class=\"hljs-selector-tag\">PowerShell<\/span><span class=\"hljs-keyword\">@2<\/span>\n  <span class=\"hljs-attribute\">displayName:<\/span> Run tests with dotCover\n  <span class=\"hljs-attribute\">inputs:<\/span>\n    <span class=\"hljs-attribute\">filePath:<\/span> build\/coverage.ps1\n    <span class=\"hljs-attribute\">arguments:<\/span> -DotCoverExecutable <span class=\"hljs-string\">\"C:\\tools\\dotcover\\dotCover.exe\"<\/span> -ReportGeneratorExecutable <span class=\"hljs-string\">\"C:\\tools\\reportgenerator\\reportgenerator.exe\"<\/span>\n\n- <span class=\"hljs-attribute\">task:<\/span> PublishCodeCoverageResults@<span class=\"hljs-number\">2<\/span>\n  <span class=\"hljs-attribute\">displayName:<\/span> Publish coverage\n  <span class=\"hljs-attribute\">condition:<\/span> succeededOrFailed()\n  <span class=\"hljs-attribute\">inputs:<\/span>\n    <span class=\"hljs-attribute\">summaryFileLocation:<\/span> $(System.DefaultWorkingDirectory)\/coverage-report\/cobertura\/Cobertura.xml\n\n- <span class=\"hljs-attribute\">task:<\/span> PublishPipelineArtifact@<span class=\"hljs-number\">1<\/span>\n  <span class=\"hljs-attribute\">displayName:<\/span> Keep HTML report\n  <span class=\"hljs-attribute\">condition:<\/span> succeededOrFailed()\n  <span class=\"hljs-attribute\">inputs:<\/span>\n    <span class=\"hljs-attribute\">targetPath:<\/span> $(System.DefaultWorkingDirectory)\/coverage-report\/html\n    <span class=\"hljs-attribute\">artifact:<\/span> coverage-html<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">Version 2 of the Publish Code Coverage Results task builds its own HTML view from the summary file. That makes the <code>HtmlInline_AzurePipelines<\/code> output optional. Keep it as an artifact, or drop that ReportGenerator call. Version 1 of the task is deprecated. It used <code>codeCoverageTool: Cobertura<\/code> and <code>reportDirectory<\/code> inputs, and the original script&#039;s HTML folder was designed for it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Note:<\/strong> On Microsoft-hosted agents, dotCover is not preinstalled. Restore it in an earlier step, for example from the NuGet package, and pass the resulting path through <code>-DotCoverExecutable<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Common errors and fixes<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Symptom<\/th><th>Cause<\/th><th>Fix<\/th><\/tr><\/thead><tbody><tr><td><code>The term &#039;System.DefaultWorkingDirectory&#039; is not recognized<\/code><\/td><td>Macro syntax used in a <code>.ps1<\/code> file<\/td><td>Use <code>$env:SYSTEM_DEFAULTWORKINGDIRECTORY<\/code><\/td><\/tr><tr><td>Pipeline passes although tests fail<\/td><td>dotCover exit code returned instead of the test runner&#039;s<\/td><td>Add <code>\/ReturnTargetExitCode<\/code> and check <code>$LASTEXITCODE<\/code><\/td><\/tr><tr><td>Coverage is 0% or the report is empty<\/td><td>Filter matches no modules, or only test assemblies<\/td><td>Check module names against the <code>bin<\/code> output and adjust <code>+:module=<\/code> masks<\/td><\/tr><tr><td>ReportGenerator cannot find report files<\/td><td>Space after <code>-reports:<\/code> or a wrong path<\/td><td>Pass <code>-reports:path1;path2<\/code> with no space<\/td><\/tr><tr><td>Tests run twice and coverage looks wrong<\/td><td>Both the dotnet and vstest regions are enabled<\/td><td>Keep only one runner block<\/td><\/tr><tr><td>Profiler errors when the agent runs as a service account<\/td><td>Bitness mismatch between dotCover and the test process<\/td><td>Set the core instruction set option (<code>x64<\/code> or <code>x86<\/code>) to match the test process, as the original comment suggests<\/td><\/tr><tr><td>Publish task reports no coverage data<\/td><td><code>summaryFileLocation<\/code> points to the wrong file<\/td><td>Point it at <code>coverage-report\/cobertura\/Cobertura.xml<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Best practices<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Pass tool paths as parameters or pipeline variables instead of editing the script on each agent.<\/li>\n\n\n<li>Exclude test assemblies, generated code and migrations with filters, so the percentage reflects production code.<\/li>\n\n\n<li>Publish coverage with <code>condition: succeededOrFailed()<\/code>, so you still see coverage when a test breaks.<\/li>\n\n\n<li>Keep coverage generation separate from quality gates. Feed the Cobertura file into tools such as SonarQube when you want thresholds enforced.<\/li>\n\n\n<li>Pin tool versions on self-hosted agents and update them deliberately, because console options can change between releases.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Frequently asked questions<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Can Azure DevOps read dotCover reports directly?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">No. The Publish Code Coverage Results task reads Cobertura or JaCoCo files, so dotCover&#039;s DetailedXML output has to be converted first, which is what ReportGenerator does in this script.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Why does my script fail with System.DefaultWorkingDirectory is not recognized?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>$(Variable)<\/code> syntax is only replaced by Azure DevOps in inline scripts. In a script file, PowerShell treats it as a subexpression, so read the value from <code>$env:SYSTEM_DEFAULTWORKINGDIRECTORY<\/code> instead.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How do I make the build fail when unit tests fail under dotCover?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Add <code>\/ReturnTargetExitCode<\/code> to the dotCover arguments so it returns the test runner&#039;s exit code. Then check <code>$LASTEXITCODE<\/code> after each run and throw if it isn&#039;t zero.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Do I still need the HtmlInline_AzurePipelines report?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Not with PublishCodeCoverageResults@2, which builds its own HTML view from the Cobertura summary. You can keep the ReportGenerator HTML as a pipeline artifact if you prefer its layout.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Can I use Coverlet instead of dotCover?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Yes. For SDK-style projects, <code>dotnet test --collect &quot;XPlat Code Coverage&quot;<\/code> produces Cobertura directly, with no extra conversion. dotCover is mainly useful when your team already relies on JetBrains tooling or its filtering options.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Related tutorials<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.devopsschool.com\/blog\/category\/devops\/code-analysis\/\">More Code Quality &amp; Analysis tutorials<\/a><\/li>\n\n\n<li><a href=\"https:\/\/www.devopsschool.com\/blog\/sonarqube-masterclass-2-day-hands-on-training-for-developers-devops-administrators\/\">SonarQube Masterclass: 2-Day Hands-on Training for Developers, DevOps &amp; Administrators<\/a><\/li>\n\n\n<li><a href=\"https:\/\/www.devopsschool.com\/blog\/top-10-static-code-analysis-tools-in-2025-features-pros-cons-comparison\/\">Top 10 Static Code Analysis Tools in 2026: Features, Pros, Cons &amp; Comparison<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>This page shows a PowerShell script that runs .NET unit tests under JetBrains dotCover, writes DetailedXML coverage files, and uses ReportGenerator to convert them to Cobertura and&#8230; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[62],"tags":[6126,610,1840],"class_list":["post-31759","post","type-post","status-publish","format-standard","hentry","category-code-analysis","tag-azure-devops","tag-code-coverage","tag-powershell"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/31759","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=31759"}],"version-history":[{"count":2,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/31759\/revisions"}],"predecessor-version":[{"id":80480,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/31759\/revisions\/80480"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=31759"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=31759"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=31759"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}