Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit testing code coverage #3679

Merged
merged 3 commits into from
Jun 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions csharp/unit-testing-code-coverage/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/TestResults/*
/Reports/*
coverage.json
7 changes: 7 additions & 0 deletions csharp/unit-testing-code-coverage/Numbers/Numbers.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>

</Project>
22 changes: 22 additions & 0 deletions csharp/unit-testing-code-coverage/Numbers/PrimeService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace System.Numbers
{
public class PrimeService
{
public bool IsPrime(int candidate)
{
if (candidate < 2)
{
return false;
}

for (int divisor = 2; divisor <= Math.Sqrt(candidate); ++divisor)
{
if (candidate % divisor == 0)
{
return false;
}
}
return true;
}
}
}
33 changes: 33 additions & 0 deletions csharp/unit-testing-code-coverage/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
languages:
- csharp
products:
- dotnet
- dotnet-core
page_type: sample
name: ".NET Core unit testing code coverage"
urlFragment: "unit-testing-code-coverage-cs"
description: ".NET Core unit testing code coverage and reporting with coverlet, and ReportGenerator."
---

# .NET Core unit testing code coverage

This sample solution includes a class library that is unit tested by two xUnit test projects. The corresponding article, [use code coverage for unit testing](https://docs.microsoft.com/dotnet/core/testing/unit-testing-code-coverage) details the usage of C#, xUnit, coverlet, and ReportGenerator.

## Sample prerequisites

This sample is written in C# and targets .NET Core 3.1. It requires the [.NET Core 3.1 SDK](https://dotnet.microsoft.com/download/dotnet-core/3.1).

## Building the sample

The source code includes an MSBuild project file for C# (a *.csproj* file) that targets .NET Core 3.1. After you download the *.zip* file containing the example code, create a directory and select **Download ZIP** to download the sample code files to your computer. To build the example:

1. Download the *.zip* file containing.
1. Create the directory to which you want to copy the files.
1. Copy the files from the *.zip* file to the directory you just created.
1. If you are using Visual Studio 2019:
1. In Visual Studio, select **Open a project or solution** (or **File** > **Open** > **Project/Solution** from the Visual Studio menu.
1. Select **Debug** > **Start Debugging** from the Visual Studio menu to build and launch the application.
1. If you are working from the command line:
1. Navigate to the directory that contains the sample.
1. Type in the command `dotnet run` to build and launch the application.
43 changes: 43 additions & 0 deletions csharp/unit-testing-code-coverage/XUnit.Coverage.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.30204.135
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "XUnit.Coverlet.MSBuild", "XUnit.Coverlet.MSBuild\XUnit.Coverlet.MSBuild.csproj", "{D175215F-6236-4099-AF52-1A590D469C77}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "XUnit.Coverlet.Collector", "XUnit.Coverlet.Collector\XUnit.Coverlet.Collector.csproj", "{2658198A-3AF7-41BC-9A4B-FC527B966ADE}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Numbers", "Numbers\Numbers.csproj", "{EE84B727-F32F-4AC0-AF3E-35ED1C2ABF54}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{4B1428C5-6394-41F0-BD14-E5D600734E9D}"
ProjectSection(SolutionItems) = preProject
.gitignore = .gitignore
README.md = README.md
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{D175215F-6236-4099-AF52-1A590D469C77}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D175215F-6236-4099-AF52-1A590D469C77}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D175215F-6236-4099-AF52-1A590D469C77}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D175215F-6236-4099-AF52-1A590D469C77}.Release|Any CPU.Build.0 = Release|Any CPU
{2658198A-3AF7-41BC-9A4B-FC527B966ADE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2658198A-3AF7-41BC-9A4B-FC527B966ADE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2658198A-3AF7-41BC-9A4B-FC527B966ADE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2658198A-3AF7-41BC-9A4B-FC527B966ADE}.Release|Any CPU.Build.0 = Release|Any CPU
{EE84B727-F32F-4AC0-AF3E-35ED1C2ABF54}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EE84B727-F32F-4AC0-AF3E-35ED1C2ABF54}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EE84B727-F32F-4AC0-AF3E-35ED1C2ABF54}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EE84B727-F32F-4AC0-AF3E-35ED1C2ABF54}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {6FC1D618-6596-4B95-AE0F-AE47427A6748}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System.Numbers;
using Xunit;

namespace XUnit.Coverlet
{
public class PrimeServiceTests
{
readonly PrimeService _primeService;

public PrimeServiceTests() => _primeService = new PrimeService();

[
Theory,
InlineData(-1), InlineData(0), InlineData(1)
]
public void IsPrime_ValuesLessThan2_ReturnFalse(int value) =>
Assert.False(_primeService.IsPrime(value), $"{value} should not be prime");

[
Theory,
InlineData(2), InlineData(3), InlineData(5), InlineData(7)
]
public void IsPrime_PrimesLessThan10_ReturnTrue(int value) =>
Assert.True(_primeService.IsPrime(value), $"{value} should be prime");

[
Theory,
InlineData(4), InlineData(6), InlineData(8), InlineData(9)
]
public void IsPrime_NonPrimesLessThan10_ReturnFalse(int value) =>
Assert.False(_primeService.IsPrime(value), $"{value} should not be prime");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.1" />
<PackageReference Include="ReportGenerator" Version="4.6.1" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="1.3.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Numbers\Numbers.csproj" />
</ItemGroup>

<ItemGroup>
<Folder Include="TestResults\" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System.Numbers;
using Xunit;

namespace XUnit.Coverlet
{
public class PrimeServiceTests
{
readonly PrimeService _primeService;

public PrimeServiceTests() => _primeService = new PrimeService();

[
Theory,
InlineData(-1), InlineData(0), InlineData(1)
]
public void IsPrime_ValuesLessThan2_ReturnFalse(int value) =>
Assert.False(_primeService.IsPrime(value), $"{value} should not be prime");

[
Theory,
InlineData(2), InlineData(3), InlineData(5), InlineData(7)
]
public void IsPrime_PrimesLessThan10_ReturnTrue(int value) =>
Assert.True(_primeService.IsPrime(value), $"{value} should be prime");

[
Theory,
InlineData(4), InlineData(6), InlineData(8), InlineData(9)
]
public void IsPrime_NonPrimesLessThan10_ReturnFalse(int value) =>
Assert.False(_primeService.IsPrime(value), $"{value} should not be prime");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.msbuild" Version="2.9.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.1" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="1.3.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Numbers\Numbers.csproj" />
</ItemGroup>

</Project>
3 changes: 3 additions & 0 deletions csharp/unit-testing/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/TestResults/*
/Reports/*
coverage.json
11 changes: 7 additions & 4 deletions csharp/unit-testing/MSTest.Project/MSTest.Project.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
<PackageReference Include="MSTest.TestAdapter" Version="2.1.0" />
<PackageReference Include="MSTest.TestFramework" Version="2.1.0" />
<PackageReference Include="coverlet.collector" Version="1.2.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.1" />
<PackageReference Include="MSTest.TestAdapter" Version="2.1.2" />
<PackageReference Include="MSTest.TestFramework" Version="2.1.2" />
<PackageReference Include="coverlet.collector" Version="1.3.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion csharp/unit-testing/NUnit.TestProject/NUnit.Project.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<ItemGroup>
<PackageReference Include="NUnit" Version="3.12.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.16.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0"/>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.1" />
</ItemGroup>

</Project>
7 changes: 4 additions & 3 deletions csharp/unit-testing/UnitTesting.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.30104.148
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "XUnit.Project", "XUnit.TestProject\XUnit.Project.csproj", "{04082197-A92E-49DC-8349-F11CB93D9E8E}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "XUnit.Project", "XUnit.TestProject\XUnit.Project.csproj", "{04082197-A92E-49DC-8349-F11CB93D9E8E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NUnit.Project", "NUnit.TestProject\NUnit.Project.csproj", "{20D0BC71-F331-4D52-B281-7E743386C536}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NUnit.Project", "NUnit.TestProject\NUnit.Project.csproj", "{20D0BC71-F331-4D52-B281-7E743386C536}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MSTest.Project", "MSTest.Project\MSTest.Project.csproj", "{2BFAD3EE-A562-4825-995D-462484112A59}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MSTest.Project", "MSTest.Project\MSTest.Project.csproj", "{2BFAD3EE-A562-4825-995D-462484112A59}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{11197C29-FF8A-40DA-8E27-9BD52125EAF8}"
ProjectSection(SolutionItems) = preProject
.gitignore = .gitignore
README.md = README.md
EndProjectSection
EndProject
Expand Down
14 changes: 10 additions & 4 deletions csharp/unit-testing/XUnit.TestProject/XUnit.Project.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,16 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
<PackageReference Include="xunit" Version="2.4.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
<PackageReference Include="coverlet.collector" Version="1.2.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.1" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="1.3.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

</Project>