"Woman Holding a Balance" by Johannes Vermeer, photo from wikipedia.org
👋...
“...77% of the failures can be reproduced by a unit test”
https://www.usenix.org/system/files/conference/osdi14/osdi14-paper-yuan.pdf
...we can prevent many serious bugs by just writing unit tests
...making our unit tests more comprehensive has a big benefit
public static bool IsItTeaTimeYet(
int hour)
{
return hour > 5 && hour < 22;
}
[Test]
public void Perfect_tea_time()
{
Assert.That(
Tea.IsItTeaTimeYet(3),
Is.False);
Assert.That(
Tea.IsItTeaTimeYet(12),
Is.True);
}
public static bool IsItTeaTimeYet(
int hour)
{
return hour >= 5 && hour < 22; // '> 5' mutated to '>= 5'
}
[Test]
public void Perfect_tea_time()
{
Assert.That(
Tea.IsItTeaTimeYet(3),
Is.False);
Assert.That(
Tea.IsItTeaTimeYet(12),
Is.True);
}
MutationTestingExample\TeaTime.cs:8
original: return hour > 5 && hour < 22;
mutated: return hour >= 5 && hour < 22;
From google dictionary
public static int Sum(int a, int b)
{
return a + b;
}
IL_0000: nop
IL_0001: ldarg.0
IL_0002: ldarg.1
IL_0003: add # the interesting bit
IL_0004: stloc.0
IL_0005: br.s IL_0007
IL_0007: ldloc.0
IL_0008: ret
string result = "";
switch (input)
{
case "a": result = "F"; break;
case "b": result = "G"; break;
case "c": result = "H"; break;
...
case "j": result = "O"; break;
}
string result = "";
uint num = .ComputeStringHash(input);
if (num <= 3826002220u)
{
if (num <= 3775669363u)
{
if (num != 3758891744u)
{
...
public static int Sum(int a, int b)
{
return a + b;
}
MethodDeclaration
PublicKeyword
StaticKeyword
...
Block
OpenBraceToken
ReturnStatment
ReturnKeyword
AddExpression
IdentifierName // 'a'
PlusToken // '+'
IdentifierName // 'b'
SemicolonToken
CloseBraceToken
for every class
for every method
for every instruction
run all the tests
public static bool IsItTeaTimeYet(
int hour)
{
return hour > 5 && hour < 22;
}
public static bool IsItTeaTimeYet(
int hour)
{
Console.WriteLine(
"Fettle: d3044986-2942-4d5b-b157-99c0d9488f54");
return hour > 5 && hour < 22;
}
TestA
MethodA
MethodC
TestB
MethodC
MethodD
MethodA | TestA |
|
MethodB | (never called) |
|
MethodC | TestA, TestB | |
MethodD | TestB |
Get in touch @owennell
Slides: oliwennell.github.io
Fettle: github.com/ComparetheMarket/fettle
Stryker.NET: github.com/stryker-mutator/stryker-net