diff --git a/csharp/unit-testing-code-coverage/.gitignore b/csharp/unit-testing-code-coverage/.gitignore new file mode 100644 index 00000000000..d6bc47910f7 --- /dev/null +++ b/csharp/unit-testing-code-coverage/.gitignore @@ -0,0 +1,3 @@ +/TestResults/* +/Reports/* +coverage.json diff --git a/csharp/unit-testing-code-coverage/Numbers/Numbers.csproj b/csharp/unit-testing-code-coverage/Numbers/Numbers.csproj new file mode 100644 index 00000000000..9f5c4f4abb6 --- /dev/null +++ b/csharp/unit-testing-code-coverage/Numbers/Numbers.csproj @@ -0,0 +1,7 @@ + + + + netstandard2.0 + + + diff --git a/csharp/unit-testing-code-coverage/Numbers/PrimeService.cs b/csharp/unit-testing-code-coverage/Numbers/PrimeService.cs new file mode 100644 index 00000000000..672b713fc02 --- /dev/null +++ b/csharp/unit-testing-code-coverage/Numbers/PrimeService.cs @@ -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; + } + } +} diff --git a/csharp/unit-testing-code-coverage/README.md b/csharp/unit-testing-code-coverage/README.md new file mode 100644 index 00000000000..5c2f6bb02a2 --- /dev/null +++ b/csharp/unit-testing-code-coverage/README.md @@ -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. diff --git a/csharp/unit-testing-code-coverage/XUnit.Coverage.sln b/csharp/unit-testing-code-coverage/XUnit.Coverage.sln new file mode 100644 index 00000000000..4ecf03e3a2f --- /dev/null +++ b/csharp/unit-testing-code-coverage/XUnit.Coverage.sln @@ -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 diff --git a/csharp/unit-testing-code-coverage/XUnit.Coverlet.Collector/PrimeServiceTests.cs b/csharp/unit-testing-code-coverage/XUnit.Coverlet.Collector/PrimeServiceTests.cs new file mode 100644 index 00000000000..fa0bffd5f9e --- /dev/null +++ b/csharp/unit-testing-code-coverage/XUnit.Coverlet.Collector/PrimeServiceTests.cs @@ -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"); + } +} diff --git a/csharp/unit-testing-code-coverage/XUnit.Coverlet.Collector/XUnit.Coverlet.Collector.csproj b/csharp/unit-testing-code-coverage/XUnit.Coverlet.Collector/XUnit.Coverlet.Collector.csproj new file mode 100644 index 00000000000..450328e5cd0 --- /dev/null +++ b/csharp/unit-testing-code-coverage/XUnit.Coverlet.Collector/XUnit.Coverlet.Collector.csproj @@ -0,0 +1,31 @@ + + + + netcoreapp3.1 + + false + + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + + + + diff --git a/csharp/unit-testing-code-coverage/XUnit.Coverlet.MSBuild/PrimeServiceTests.cs b/csharp/unit-testing-code-coverage/XUnit.Coverlet.MSBuild/PrimeServiceTests.cs new file mode 100644 index 00000000000..fa0bffd5f9e --- /dev/null +++ b/csharp/unit-testing-code-coverage/XUnit.Coverlet.MSBuild/PrimeServiceTests.cs @@ -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"); + } +} diff --git a/csharp/unit-testing-code-coverage/XUnit.Coverlet.MSBuild/XUnit.Coverlet.MSBuild.csproj b/csharp/unit-testing-code-coverage/XUnit.Coverlet.MSBuild/XUnit.Coverlet.MSBuild.csproj new file mode 100644 index 00000000000..7359b90af35 --- /dev/null +++ b/csharp/unit-testing-code-coverage/XUnit.Coverlet.MSBuild/XUnit.Coverlet.MSBuild.csproj @@ -0,0 +1,30 @@ + + + + netcoreapp3.1 + + false + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + diff --git a/csharp/unit-testing/.gitignore b/csharp/unit-testing/.gitignore new file mode 100644 index 00000000000..d6bc47910f7 --- /dev/null +++ b/csharp/unit-testing/.gitignore @@ -0,0 +1,3 @@ +/TestResults/* +/Reports/* +coverage.json diff --git a/csharp/unit-testing/MSTest.Project/MSTest.Project.csproj b/csharp/unit-testing/MSTest.Project/MSTest.Project.csproj index a45be4b8a75..8cc1f6a3fce 100644 --- a/csharp/unit-testing/MSTest.Project/MSTest.Project.csproj +++ b/csharp/unit-testing/MSTest.Project/MSTest.Project.csproj @@ -7,10 +7,13 @@ - - - - + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/csharp/unit-testing/NUnit.TestProject/NUnit.Project.csproj b/csharp/unit-testing/NUnit.TestProject/NUnit.Project.csproj index 3737e9ea44f..c17585ba1c2 100644 --- a/csharp/unit-testing/NUnit.TestProject/NUnit.Project.csproj +++ b/csharp/unit-testing/NUnit.TestProject/NUnit.Project.csproj @@ -9,7 +9,7 @@ - + diff --git a/csharp/unit-testing/UnitTesting.sln b/csharp/unit-testing/UnitTesting.sln index 2a346661ec9..bc97bf3b600 100644 --- a/csharp/unit-testing/UnitTesting.sln +++ b/csharp/unit-testing/UnitTesting.sln @@ -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 diff --git a/csharp/unit-testing/XUnit.TestProject/XUnit.Project.csproj b/csharp/unit-testing/XUnit.TestProject/XUnit.Project.csproj index b84acb879c1..c4da77dfc15 100644 --- a/csharp/unit-testing/XUnit.TestProject/XUnit.Project.csproj +++ b/csharp/unit-testing/XUnit.TestProject/XUnit.Project.csproj @@ -7,10 +7,16 @@ - - - - + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive +