Project | Build | Mutation Score |
---|---|---|
Mutation.Foo | ||
Mutation.Store.Example |
It is a simple example to demonstrate you how to implement mutation testing into your .NET Core/C# application
Mutation testing is an approach to help you to check if your unit tests are effective enough.
The main idea is to create a mutant code which is a modified version of your code and run the existing unit tests against this mutant code, instead of original code. If the mutation code break tests, it mean that the mutant has killed, which is good. If the mutation code doesn't break tests then the mutant survived, which is not so good. In other words... we espect that the unit tests fails:
mutated code + failed tests = killed mutant
✔️
mutated code + passed tests = survived mutant
❌
Original | Mutated |
---|---|
var value = a + b; | var value = a – b; |
var value = a * b; | var value = a / b; |
Original | Mutated |
---|---|
if (a == b) { … } | if (a != b) { … } |
if (a > b) { … } | if (a < b) { … } |
Original | Mutated |
---|---|
if (a && b) { … } | if (a || b) { … } |
if (a || b) { … } | if (a && b) { … } |
Original | Mutated |
---|---|
var x = y; | var x = !y; |
if (!x) { … } | if (x) { … } |
In our example we have used Stryker.NET. The Stryker Mutator implementation for .NET Core/Framework
First of all we need to install Stryker.NET tool. For that we have two installation options. Locally, just for a specific project. Or globally, that can be used by any other project.
To install locally run the follow commands on root folder of your test project:
dotnet new tool-manifest
dotnet tool install dotnet-stryker
To install globally run the follow command:
dotnet tool install -g dotnet-stryker
Bellow is the command to run Stryker.NET. Just execute it in your test project's root folder:
dotnet stryker
And then, you may to see something like the screen bellow:
After each execution Stryker generate a report.
The default type is a HTML page containing a dashboard, where you can see all the mutations generated and wich mutation has killed or survived on your project.
And is that! You can find more examples with Stryker and CI pipelines (GitHub Actions) in this repository.
Explore and enjoy 😃