Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Sample code for continuous integration testing and code coverage in Jenkins for .Net (Core) with xUnit and OpenCover
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)'=='Debug'">
<DebugType>full</DebugType>
<DebugSymbols>True</DebugSymbols>
</PropertyGroup>
</Project>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netcoreapp2.0;net47;net461</TargetFrameworks>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0-preview-20170106-08" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.0" />
<PackageReference Include="xunit" Version="2.3.0" />
<PackageReference Include="OpenCover" Version="4.6.519">
<PrivateAssets>All</PrivateAssets>
</PackageReference>
<PackageReference Include="OpenCoverToCoberturaConverter" Version="0.2.4">
<PrivateAssets>All</PrivateAssets>
</PackageReference>
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.0" />
</ItemGroup>
</Project>
$testProjects = "Sample.Tests", "Sample.Tests.Integration"
$testRuns = 1;
& dotnet restore
$oldResults = Get-ChildItem -Path "$PSScriptRoot\results_$testRuns-*.testresults"
if ($oldResults) {
Remove-Item $oldResults
}
foreach ($testProject in $testProjects){
& cd "$PSScriptRoot\test\$testProject"
& dotnet.exe xunit `
-parallel all `
-xml $PSScriptRoot\results_$testRuns.testresults
$testRuns++
}
& cd "$PSScriptRoot"
$testProjects = "Sample.Tests", "Sample.Tests.Integration"
# Get the most recent OpenCover NuGet package from the dotnet nuget packages
$nugetOpenCoverPackage = Join-Path -Path $env:USERPROFILE -ChildPath "\.nuget\packages\OpenCover"
$latestOpenCover = Join-Path -Path ((Get-ChildItem -Path $nugetOpenCoverPackage | Sort-Object Fullname -Descending)[0].FullName) -ChildPath "tools\OpenCover.Console.exe"
# Get the most recent OpenCoverToCoberturaConverter from the dotnet nuget packages
$nugetCoberturaConverterPackage = Join-Path -Path $env:USERPROFILE -ChildPath "\.nuget\packages\OpenCoverToCoberturaConverter"
$latestCoberturaConverter = Join-Path -Path (Get-ChildItem -Path $nugetCoberturaConverterPackage | Sort-Object Fullname -Descending)[0].FullName -ChildPath "tools\OpenCoverToCoberturaConverter.exe"
If (Test-Path "$PSScriptRoot\OpenCover.coverageresults"){
Remove-Item "$PSScriptRoot\OpenCover.coverageresults"
}
If (Test-Path "$PSScriptRoot\Cobertura.coverageresults"){
Remove-Item "$PSScriptRoot\Cobertura.coverageresults"
}
& dotnet restore
$testRuns = 1;
foreach ($testProject in $testProjects){
# Arguments for running dotnet
$dotnetArguments = "xunit", "-xml `"`"$PSScriptRoot\testRuns_$testRuns.testresults`"`""
"Running tests with OpenCover"
& $latestOpenCover `
-register:user `
-target:dotnet.exe `
-targetdir:$PSScriptRoot\test\$testProject `
"-targetargs:$dotnetArguments" `
-returntargetcode `
-output:"$PSScriptRoot\OpenCover.coverageresults" `
-mergeoutput `
-oldStyle `
-excludebyattribute:System.CodeDom.Compiler.GeneratedCodeAttribute `
"-filter:+[Sample*]* -[*.Tests]* -[*.Tests.*]*"
$testRuns++
}
"Converting coverage reports to Cobertura format"
& $latestCoberturaConverter `
-input:"$PSScriptRoot\OpenCover.coverageresults" `
-output:"$PSScriptRoot\Cobertura.coverageresults" `
"-sources:$PSScriptRoot"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.