-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3679 from IEvangelist/unit-testing-coverage
Unit testing code coverage
- Loading branch information
Showing
14 changed files
with
260 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
/TestResults/* | ||
/Reports/* | ||
coverage.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
33 changes: 33 additions & 0 deletions
33
csharp/unit-testing-code-coverage/XUnit.Coverlet.Collector/PrimeServiceTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
csharp/unit-testing-code-coverage/XUnit.Coverlet.Collector/XUnit.Coverlet.Collector.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
33 changes: 33 additions & 0 deletions
33
csharp/unit-testing-code-coverage/XUnit.Coverlet.MSBuild/PrimeServiceTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
csharp/unit-testing-code-coverage/XUnit.Coverlet.MSBuild/XUnit.Coverlet.MSBuild.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
/TestResults/* | ||
/Reports/* | ||
coverage.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters