-
Notifications
You must be signed in to change notification settings - Fork 0
User Guide
Inspired by ScalaCheck this can be thought of as a C# version of that. Whereas Scala has numerous language features which makes the ScalaCheck fit in organically, with the advent of latest c# with .net 4.5 we can also have that flavor too for proposition based automated testing. For a description and concept see here.
- No shrinking strategy for making most simple failed case.
- No mechanism for proving the property spec based of number of tests passed.
The API is similar that of ScalaCheck but of-course based on C# language features and style.
The idea is we create a property based on our specification like:
var prop = Prop.ForAll((string s, string t) => (s + t).StartsWith(s));
And then we call
prop.Check();
It reports like:
+ OK, passed after 10000 tests. in the console.
Now if we make the property like
var prop = Prop.ForAll((string s, string t) => (s + t).StartsWith(t));
Now it throws a TestFailedException and prints in console:
CSharpCheck.TestFailedException : ! Failed after 0 tests, for value: { Item1 = cSp deXgI d, Item2 = 6 tM60d J M5 vdd 52 6 }
Generators are basically IEnumerable implementation which can be iterated and combined to make new generators.
Arbitrary generators are implemented for int, string, char and List, List, List.
When we create property with Prop.ForAll(int x=>???) the arbitrary generator is used. In previous section two arbitrary string generator was used.
We can use the Gen class to give us some convenient generators:
OneOf Gives one of the arguments values
var vowel=Gen.OneOf('a', 'e', 'i', 'o', 'u') var colors=Gen.OneOf("red", "green", "blue")
Choose Gives an integer from a range
var gen=Gen.Choose(1, 10)// generates integer ranging from 1 to 10
Now we can combine generators to make our own like:
//generate tuple<int, int> where second int is at least twice as the first one var myGen = (from m in Gen.Choose(1, 10) from n in Gen.Choose(2*m, 25) select new Tuple<int, int>(m, n));
Now we can check this property as:
var prop = Prop.ForAll(myGen).SuchThat(t => t.Item2 >= t.Item1*2); prop.Check();