Skip to content

Commit 8c7d4b2

Browse files
authored
Merge pull request #3679 from IEvangelist/unit-testing-coverage
Unit testing code coverage
2 parents 0c061c7 + 7a87131 commit 8c7d4b2

File tree

14 files changed

+260
-12
lines changed

14 files changed

+260
-12
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/TestResults/*
2+
/Reports/*
3+
coverage.json
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netstandard2.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
</Project>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
namespace System.Numbers
2+
{
3+
public class PrimeService
4+
{
5+
public bool IsPrime(int candidate)
6+
{
7+
if (candidate < 2)
8+
{
9+
return false;
10+
}
11+
12+
for (int divisor = 2; divisor <= Math.Sqrt(candidate); ++divisor)
13+
{
14+
if (candidate % divisor == 0)
15+
{
16+
return false;
17+
}
18+
}
19+
return true;
20+
}
21+
}
22+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
---
2+
languages:
3+
- csharp
4+
products:
5+
- dotnet
6+
- dotnet-core
7+
page_type: sample
8+
name: ".NET Core unit testing code coverage"
9+
urlFragment: "unit-testing-code-coverage-cs"
10+
description: ".NET Core unit testing code coverage and reporting with coverlet, and ReportGenerator."
11+
---
12+
13+
# .NET Core unit testing code coverage
14+
15+
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.
16+
17+
## Sample prerequisites
18+
19+
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).
20+
21+
## Building the sample
22+
23+
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:
24+
25+
1. Download the *.zip* file containing.
26+
1. Create the directory to which you want to copy the files.
27+
1. Copy the files from the *.zip* file to the directory you just created.
28+
1. If you are using Visual Studio 2019:
29+
1. In Visual Studio, select **Open a project or solution** (or **File** > **Open** > **Project/Solution** from the Visual Studio menu.
30+
1. Select **Debug** > **Start Debugging** from the Visual Studio menu to build and launch the application.
31+
1. If you are working from the command line:
32+
1. Navigate to the directory that contains the sample.
33+
1. Type in the command `dotnet run` to build and launch the application.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.30204.135
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "XUnit.Coverlet.MSBuild", "XUnit.Coverlet.MSBuild\XUnit.Coverlet.MSBuild.csproj", "{D175215F-6236-4099-AF52-1A590D469C77}"
7+
EndProject
8+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "XUnit.Coverlet.Collector", "XUnit.Coverlet.Collector\XUnit.Coverlet.Collector.csproj", "{2658198A-3AF7-41BC-9A4B-FC527B966ADE}"
9+
EndProject
10+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Numbers", "Numbers\Numbers.csproj", "{EE84B727-F32F-4AC0-AF3E-35ED1C2ABF54}"
11+
EndProject
12+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{4B1428C5-6394-41F0-BD14-E5D600734E9D}"
13+
ProjectSection(SolutionItems) = preProject
14+
.gitignore = .gitignore
15+
README.md = README.md
16+
EndProjectSection
17+
EndProject
18+
Global
19+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
20+
Debug|Any CPU = Debug|Any CPU
21+
Release|Any CPU = Release|Any CPU
22+
EndGlobalSection
23+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
24+
{D175215F-6236-4099-AF52-1A590D469C77}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
25+
{D175215F-6236-4099-AF52-1A590D469C77}.Debug|Any CPU.Build.0 = Debug|Any CPU
26+
{D175215F-6236-4099-AF52-1A590D469C77}.Release|Any CPU.ActiveCfg = Release|Any CPU
27+
{D175215F-6236-4099-AF52-1A590D469C77}.Release|Any CPU.Build.0 = Release|Any CPU
28+
{2658198A-3AF7-41BC-9A4B-FC527B966ADE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
29+
{2658198A-3AF7-41BC-9A4B-FC527B966ADE}.Debug|Any CPU.Build.0 = Debug|Any CPU
30+
{2658198A-3AF7-41BC-9A4B-FC527B966ADE}.Release|Any CPU.ActiveCfg = Release|Any CPU
31+
{2658198A-3AF7-41BC-9A4B-FC527B966ADE}.Release|Any CPU.Build.0 = Release|Any CPU
32+
{EE84B727-F32F-4AC0-AF3E-35ED1C2ABF54}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
33+
{EE84B727-F32F-4AC0-AF3E-35ED1C2ABF54}.Debug|Any CPU.Build.0 = Debug|Any CPU
34+
{EE84B727-F32F-4AC0-AF3E-35ED1C2ABF54}.Release|Any CPU.ActiveCfg = Release|Any CPU
35+
{EE84B727-F32F-4AC0-AF3E-35ED1C2ABF54}.Release|Any CPU.Build.0 = Release|Any CPU
36+
EndGlobalSection
37+
GlobalSection(SolutionProperties) = preSolution
38+
HideSolutionNode = FALSE
39+
EndGlobalSection
40+
GlobalSection(ExtensibilityGlobals) = postSolution
41+
SolutionGuid = {6FC1D618-6596-4B95-AE0F-AE47427A6748}
42+
EndGlobalSection
43+
EndGlobal
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System.Numbers;
2+
using Xunit;
3+
4+
namespace XUnit.Coverlet
5+
{
6+
public class PrimeServiceTests
7+
{
8+
readonly PrimeService _primeService;
9+
10+
public PrimeServiceTests() => _primeService = new PrimeService();
11+
12+
[
13+
Theory,
14+
InlineData(-1), InlineData(0), InlineData(1)
15+
]
16+
public void IsPrime_ValuesLessThan2_ReturnFalse(int value) =>
17+
Assert.False(_primeService.IsPrime(value), $"{value} should not be prime");
18+
19+
[
20+
Theory,
21+
InlineData(2), InlineData(3), InlineData(5), InlineData(7)
22+
]
23+
public void IsPrime_PrimesLessThan10_ReturnTrue(int value) =>
24+
Assert.True(_primeService.IsPrime(value), $"{value} should be prime");
25+
26+
[
27+
Theory,
28+
InlineData(4), InlineData(6), InlineData(8), InlineData(9)
29+
]
30+
public void IsPrime_NonPrimesLessThan10_ReturnFalse(int value) =>
31+
Assert.False(_primeService.IsPrime(value), $"{value} should not be prime");
32+
}
33+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp3.1</TargetFramework>
5+
6+
<IsPackable>false</IsPackable>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.1" />
11+
<PackageReference Include="ReportGenerator" Version="4.6.1" />
12+
<PackageReference Include="xunit" Version="2.4.1" />
13+
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.2">
14+
<PrivateAssets>all</PrivateAssets>
15+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
16+
</PackageReference>
17+
<PackageReference Include="coverlet.collector" Version="1.3.0">
18+
<PrivateAssets>all</PrivateAssets>
19+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
20+
</PackageReference>
21+
</ItemGroup>
22+
23+
<ItemGroup>
24+
<ProjectReference Include="..\Numbers\Numbers.csproj" />
25+
</ItemGroup>
26+
27+
<ItemGroup>
28+
<Folder Include="TestResults\" />
29+
</ItemGroup>
30+
31+
</Project>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System.Numbers;
2+
using Xunit;
3+
4+
namespace XUnit.Coverlet
5+
{
6+
public class PrimeServiceTests
7+
{
8+
readonly PrimeService _primeService;
9+
10+
public PrimeServiceTests() => _primeService = new PrimeService();
11+
12+
[
13+
Theory,
14+
InlineData(-1), InlineData(0), InlineData(1)
15+
]
16+
public void IsPrime_ValuesLessThan2_ReturnFalse(int value) =>
17+
Assert.False(_primeService.IsPrime(value), $"{value} should not be prime");
18+
19+
[
20+
Theory,
21+
InlineData(2), InlineData(3), InlineData(5), InlineData(7)
22+
]
23+
public void IsPrime_PrimesLessThan10_ReturnTrue(int value) =>
24+
Assert.True(_primeService.IsPrime(value), $"{value} should be prime");
25+
26+
[
27+
Theory,
28+
InlineData(4), InlineData(6), InlineData(8), InlineData(9)
29+
]
30+
public void IsPrime_NonPrimesLessThan10_ReturnFalse(int value) =>
31+
Assert.False(_primeService.IsPrime(value), $"{value} should not be prime");
32+
}
33+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp3.1</TargetFramework>
5+
6+
<IsPackable>false</IsPackable>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="coverlet.msbuild" Version="2.9.0">
11+
<PrivateAssets>all</PrivateAssets>
12+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
13+
</PackageReference>
14+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.1" />
15+
<PackageReference Include="xunit" Version="2.4.1" />
16+
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.2">
17+
<PrivateAssets>all</PrivateAssets>
18+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
19+
</PackageReference>
20+
<PackageReference Include="coverlet.collector" Version="1.3.0">
21+
<PrivateAssets>all</PrivateAssets>
22+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
23+
</PackageReference>
24+
</ItemGroup>
25+
26+
<ItemGroup>
27+
<ProjectReference Include="..\Numbers\Numbers.csproj" />
28+
</ItemGroup>
29+
30+
</Project>

csharp/unit-testing/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/TestResults/*
2+
/Reports/*
3+
coverage.json

0 commit comments

Comments
 (0)