Skip to content

Commit

Permalink
Introduce mutation testing (#1031)
Browse files Browse the repository at this point in the history
  • Loading branch information
martintmk authored Mar 15, 2023
1 parent 32cf92f commit 8affebc
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 6 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ jobs:
files: ./src/Polly.Specs/coverage-reports/Cobertura.xml,./src/Polly.Core.Tests/coverage-reports/Cobertura.xml
flags: ${{ matrix.os_name }}

- name: Upload Mutation Report
uses: actions/upload-artifact@v3
with:
name: mutation-report-${{ matrix.os_name }}
path: StrykerOutput

- name: Publish NuGet packages
uses: actions/upload-artifact@v3
with:
Expand Down
30 changes: 29 additions & 1 deletion build.cake
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ var configuration = Argument<string>("configuration", "Release");
//////////////////////////////////////////////////////////////////////

#Tool "xunit.runner.console&version=2.4.2"
#Tool "dotnet-stryker&version=3.6.1"

//////////////////////////////////////////////////////////////////////
// EXTERNAL NUGET LIBRARIES
Expand Down Expand Up @@ -46,6 +47,10 @@ string nugetVersion;
string assemblyVersion;
string assemblySemver;

// Stryker / Mutation Testing
var strykerConfig = File("./eng/stryker-config.json");
var strykerOutput = Directory("StrykerOutput");

///////////////////////////////////////////////////////////////////////////////
// INNER CLASSES
///////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -84,7 +89,8 @@ Task("__Clean")
{
testResultsDir,
nupkgDestDir,
artifactsDir
artifactsDir,
strykerOutput
};

CleanDirectories(cleanDirectories);
Expand Down Expand Up @@ -206,6 +212,27 @@ Task("__RunTests")
}
});

Task("__RunMutationTests")
.Does(() =>
{
TestProject(File("./src/Polly/Polly.csproj"), File("./src/Polly.Specs/Polly.Specs.csproj"), "Polly");
TestProject(File("./src/Polly.Core/Polly.Core.csproj"), File("./src/Polly.Core.Tests/Polly.Core.Tests.csproj"), "Polly.Core");

void TestProject(FilePath proj, FilePath testProj, string project)
{
var strykerPath = Context.Tools.Resolve("Stryker.CLI.dll");
var mutationScore = XmlPeek(proj, "/Project/PropertyGroup/MutationScore/text()", new XmlPeekSettings { SuppressWarning = true });
var score = int.Parse(mutationScore);

Information($"Running mutation tests for '{proj}'. Test Project: '{testProj}'");
var result = StartProcess("dotnet", $"{strykerPath} --project {project} --test-project {testProj} --break-at {score} --config-file {strykerConfig} --output {strykerOutput}/{project}");
if (result != 0)
{
throw new InvalidOperationException($"The mutation testing of '{project}' project failed.");
}
}
});

Task("__CreateSignedNuGetPackages")
.Does(() =>
{
Expand Down Expand Up @@ -240,6 +267,7 @@ Task("Build")
.IsDependentOn("__UpdateAssemblyVersionInformation")
.IsDependentOn("__BuildSolutions")
.IsDependentOn("__RunTests")
.IsDependentOn("__RunMutationTests")
.IsDependentOn("__CreateSignedNuGetPackages");

///////////////////////////////////////////////////////////////////////////////
Expand Down
1 change: 0 additions & 1 deletion eng/Common.targets
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
<SignAssembly>true</SignAssembly>
<LangVersion>latest</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
<RootDir>$(MSBuildThisFileDirectory)..\</RootDir>
<ContinuousIntegrationBuild Condition=" '$(CI)' != '' ">true</ContinuousIntegrationBuild>
</PropertyGroup>

Expand Down
26 changes: 26 additions & 0 deletions eng/stryker-config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"stryker-config": {
"reporters": [
"json",
"html",
"markdown"
],
"ignore-methods": [
"*Exception.ctor",
"AddError",
"ConfigureAwait",
"Dispose",
"LogError",
"LogInformation"
],
"ignore-mutations": [
"block",
"statement"
],
"target-framework": "net7.0",
"thresholds": {
"high": 100,
"low": 100
}
}
}
2 changes: 0 additions & 2 deletions src/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
<Project>
<Import Project="../eng/Common.targets" />

<ItemGroup>
<Using Include="System.Collections" />
<Using Include="System.Collections.Concurrent" />
Expand Down
5 changes: 3 additions & 2 deletions src/Directory.Build.targets
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<Project>
<Import Project="$(RootDir)eng/Test.targets" Condition="$(ProjectType) == 'Test'" />
<Import Project="$(RootDir)eng/Library.targets" Condition="$(ProjectType) == 'Library'" />
<Import Project="$(MsBuildThisFileDirectory)../eng/Common.targets" />
<Import Project="$(MsBuildThisFileDirectory)../eng/Test.targets" Condition="$(ProjectType) == 'Test'" />
<Import Project="$(MsBuildThisFileDirectory)../eng/Library.targets" Condition="$(ProjectType) == 'Library'" />
</Project>
12 changes: 12 additions & 0 deletions src/Polly.Core.Tests/DummyTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Xunit;

namespace Polly.Core.Tests;

public class DummyTest
{
[Fact]
public void Success()
{
Assert.True(true);
}
}
1 change: 1 addition & 0 deletions src/Polly.Core/Polly.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<ProjectType>Library</ProjectType>
<UseDefaultAnalyzers>true</UseDefaultAnalyzers>
<SkipPollyUsings>true</SkipPollyUsings>
<MutationScore>100</MutationScore>
</PropertyGroup>

<ItemGroup>
Expand Down
1 change: 1 addition & 0 deletions src/Polly/Polly.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<TargetFrameworks>netstandard1.1;netstandard2.0;net461;net472</TargetFrameworks>
<AssemblyTitle>Polly</AssemblyTitle>
<ProjectType>Library</ProjectType>
<MutationScore>70</MutationScore>
</PropertyGroup>

<PropertyGroup Condition=" '$(TargetFramework)' == 'netstandard1.1' ">
Expand Down

0 comments on commit 8affebc

Please sign in to comment.