Available here.
There’s a couple of examples included :
1. The ListDeleteTests.
This one’s carried over from quicknet.
Here’s the requirement :
Write a function which takes a list of values and a value to remove as arguments, and return a copy of the inputlist that does not contain the value to remove.
Here’s the unit-test :
public class UnitTesting
{
[Fact]
public void Result_Does_Not_Contain_Removed_Value()
{
var inputList = new List<int> {1, 2, 3, 4, 5};
var resultList = new ListDeleter().DoingMyThing(inputList, 4);
Assert.DoesNotContain(4, resultList);
}
}
Here’s a first implementation :
public class ListDeleter
{
public IList<int> DoingMyThing(IList<int> theList, int iNeedToBeRemoved)
{
var result = theList.ToList();
result.Remove(iNeedToBeRemoved);
return result;
}
}
Unit test output :
1 passed, 0 failed, 0 skipped, took 1,21 seconds (xunit).
Here’s the (still quite verbose) QuickDotNetCheck test :
public class PropertyBasedTesting : Fixture
{
private readonly ListDeleter listDeleter = new ListDeleter();
private readonly IntGenerator intGen = new IntGenerator(1, 50);
private IList<int> inputList { get; set; }
private IList<int> resultList;
private int toRemove;
public override void Arrange()
{
toRemove = intGen.GetRandomValue();
inputList = new DomainGenerator().With(() => intGen).Many<int>(1, 10).ToList();
}
protected override void Act()
{
resultList = listDeleter.DoingMyThing(inputList, toRemove);
}
[Spec]
public void Result_Does_Not_Contain_Removed_Value()
{
Ensure.False(resultList.Contains(toRemove));
}
[Fact]
public void VerifyAll()
{
new Suite(100, 20)
.Register(() => new PropertyBasedTesting())
.Run();
}
public override void Shrink(Func<bool> runFunc)
{
new ListShrinkingStrategy<PropertyBasedTesting, int>(
this, e => e.inputList, new[] { -1, 0, 1 })
.Shrink(runFunc);
}
public override string ToString()
{
var sb = new StringBuilder();
sb.AppendLine(GetType().Name);
sb.Append("inputList : ");
inputList.ForEach(i => sb.AppendFormat("{0}, ", i));
sb.Remove(sb.Length - 2, 2);
sb.Append(".");
sb.AppendLine();
sb.AppendFormat("toRemove : {0}.", toRemove);
return sb.ToString();
}
}
Which outputs (f.i.) :
Test 'QuickDotNetCheck.Examples.PropertyBasedTesting.VerifyAll' failed: QuickDotNetCheck.RunReport : --------------------Simplest Fail Case-------------------- 1 : PropertyBasedTesting inputList : 45, 45. toRemove : 45. ---------------------------------------------------------- ---- QuickDotNetCheck.Exceptions.FalsifiableException : Expected : False. Actual : True. Suite.cs(151,0): at QuickDotNetCheck.Suite.Run() ListDeleteTests.cs(63,0): at QuickDotNetCheck.Examples.PropertyBasedTesting.VerifyAll() ----- Inner Stack Trace ----- Ensure.cs(34,0): at QuickDotNetCheck.Ensure.False(Boolean flag) ListDeleteTests.cs(57,0): at QuickDotNetCheck.Examples.PropertyBasedTesting.Result_Does_Not_Contain_Removed_Value() Fixture.cs(58,0): at QuickDotNetCheck.Fixture.AssertSpec(MethodInfo info) Implementation\EnumerableForEachExtension.cs(12,0): at QuickDotNetCheck.Implementation.EnumerableForEachExtension.ForEach[T](IEnumerable`1 enumerable, Action`1 action) Fixture.cs(63,0): at QuickDotNetCheck.Fixture.Assert() Suite.cs(138,0): at QuickDotNetCheck.Suite.Run() 0 passed, 1 failed, 0 skipped, took 1,78 seconds (xunit).
Or :
Test 'QuickDotNetCheck.Examples.PropertyBasedTesting.VerifyAll' failed: QuickDotNetCheck.RunReport : --------------------Simplest Fail Case-------------------- 1 : PropertyBasedTesting inputList : 18, 18. toRemove : 18. ---------------------------------------------------------- ---- QuickDotNetCheck.Exceptions.FalsifiableException : Expected : False. Actual : True. Suite.cs(151,0): at QuickDotNetCheck.Suite.Run() ListDeleteTests.cs(63,0): at QuickDotNetCheck.Examples.PropertyBasedTesting.VerifyAll() ----- Inner Stack Trace ----- Ensure.cs(34,0): at QuickDotNetCheck.Ensure.False(Boolean flag) ListDeleteTests.cs(57,0): at QuickDotNetCheck.Examples.PropertyBasedTesting.Result_Does_Not_Contain_Removed_Value() Fixture.cs(58,0): at QuickDotNetCheck.Fixture.AssertSpec(MethodInfo info) Implementation\EnumerableForEachExtension.cs(12,0): at QuickDotNetCheck.Implementation.EnumerableForEachExtension.ForEach[T](IEnumerable`1 enumerable, Action`1 action) Fixture.cs(63,0): at QuickDotNetCheck.Fixture.Assert() Suite.cs(138,0): at QuickDotNetCheck.Suite.Run() 0 passed, 1 failed, 0 skipped, took 1,76 seconds (xunit).
1. The BugHouseTest.
Also carried over from quicknet.
The BugHouse :
public class BugHouse
{
private int count;
public bool Run(int a)
{
if (count++ >= 3 && a == 6)
throw new Exception();
return true;
}
}
Here’s the test code :
public class BugHouseTest
{
[Fact]
public void VerifyAll()
{
var suite = new Suite(50, 20);
suite
.Using(() => new BugHouseFixtureState())
.Register(() => new BugHouseFixture(suite))
.Run();
}
}
public class BugHouseFixtureState
{
public BugHouse BugHouse { get; private set; }
public BugHouseFixtureState()
{
BugHouse = new BugHouse();
}
}
public class BugHouseFixture : Fixture
{
private readonly Suite suite;
private int input { get; set; }
private bool output;
public BugHouseFixture(Suite suite)
{
this.suite = suite;
}
public override void Arrange()
{
input = new IntGenerator(0, 20).GetRandomValue();
}
protected override void Act()
{
output = suite.Get<BugHouseFixtureState>().BugHouse.Run(input);
}
private SimpleValuesShrinkingStrategy<BugHouseFixture> shrunk;
public override void Shrink(Func<bool> runFunc)
{
shrunk =
new SimpleValuesShrinkingStrategy<BugHouseFixture>(this, e => e.input);
shrunk.AddValues(new object[] { -1, 0, 1 });
shrunk.Shrink(runFunc);
}
public override string ToString()
{
var sb = new StringBuilder();
sb.AppendLine(GetType().Name);
if(!shrunk.Shrunk())
sb.AppendFormat("input : {0}.", input);
return sb.ToString();
}
[Spec]
public void Always_Returns_True()
{
Ensure.True(output);
}
}
Resulting in something like :
Test 'QuickDotNetCheck.Examples.BugHouseTest.VerifyAll' failed: QuickDotNetCheck.RunReport : --------------------Simplest Fail Case-------------------- 1 : BugHouseFixture 2 : BugHouseFixture 3 : BugHouseFixture 4 : BugHouseFixture input : 6. ---------------------------------------------------------- ---- QuickDotNetCheck.Exceptions.FalsifiableException : Expected : True. Actual : False. Suite.cs(151,0): at QuickDotNetCheck.Suite.Run() BugHouseTest.cs(26,0): at QuickDotNetCheck.Examples.BugHouseTest.VerifyAll() ----- Inner Stack Trace ----- Ensure.cs(27,0): at QuickDotNetCheck.Ensure.True(Boolean flag) BugHouseTest.cs(85,0): at QuickDotNetCheck.Examples.BugHouseFixture.Always_Returns_True() Fixture.cs(58,0): at QuickDotNetCheck.Fixture.AssertSpec(MethodInfo info) Implementation\EnumerableForEachExtension.cs(12,0): at QuickDotNetCheck.Implementation.EnumerableForEachExtension.ForEach[T](IEnumerable`1 enumerable, Action`1 action) Fixture.cs(63,0): at QuickDotNetCheck.Fixture.Assert() Suite.cs(138,0): at QuickDotNetCheck.Suite.Run() 0 passed, 1 failed, 0 skipped, took 1,30 seconds (xunit).
3. The ElaborateExample.
Experiments.
Not quite working at the moment, but interesting nonetheless.
Maintaining consistency :
As promised, the project on google code is up for deletion.

