In different post in the past, we have explorer why is testing so important to us. Also, we took a look at ScalaCheck, a Property Based Testing Framework for the Scala programming language. Because we are explorers, we have started a new adventure, creating a small, yet functional and easy to use Property Based Library to be used in .NET based languages. Motivation We want a library that works with our current testing framework, in our case, xUnit. By doing a quick look at NuGet, we can find some frameworks that target the same space that ours, but they required learning the entire framework. Also, you might need to give up your favorite testing framework in order to use it. Many other problems rise from this such as having a test runner for your IDE, etc…. Our library is not intended to change anything that we do today, but instead, allows us to use property based testing along our regular tests. Core Concepts The most import piece is what we call and it represents a way to generate data of type T. Gen<T> bounded The most simple generator can be created in the following way: gen = Gen< >.Empty(); var int Assert.Empty(gen.Generate()); Notice that we are using so we are not introducing any assertion API at all. xUnit.Assert Now, let’s look at some other ways to create generators. A single item generator once = Gen< >.Once(5) var int Assert.Single(Gen< >.Once(5).Generate()); int Repeat generator repeat = Gen< >.Repeat(5, 20); var int Assert.True(repeat.Generate().Count() == 20);Assert.True(repeat.Generate().ToList().TrueForAll(x => x == 5)); In here, we are saying that we want to generate the value as many as times. 5 20 Important to notice is that calling will always return the same dataset. .Generate() Enumerable generator A more interesting generator is the one created from . IEnumerable<T> enumerableGen = Gen< >.FromEnumerable(Enumerable.Range(1, 100)); var int Assert.True(enumerableGen.Generate().Count() == 100); A simple version can be generated by doing: enumerableGen = Enumerable.Range(1, 100).ToGen(); var Assert.True(enumerableGen.Generate().Count() == 100); Generators by itself cannot do much. However, the idea is to use them as value generators in order to test some properties on them. Producers Many times we need a more robust way to generate data. A ’s intend is exactly that. Producer IProducer< T> : IEnumerable<T>{ public interface out } As you can see, is only a way to generate a sequence of values of the same type . IProducer T A simple for generating ** **s could be: Producer int IntProducer : IProducer< >{ IEnumerator< > GetEnumerator(){ ( ){ Random().Next();}} public class int public int while true yield return new IEnumerator IEnumerable.GetEnumerator() { **return** GetEnumerator(); } } It is important to notice that generates a collection of ) from the so we can force bounds on it. IntProducer unbounded int s. We need to obtain a generator ( Gen<T> Producer We can use the following to do so: intGenerator = ( IntProducer()).ChooseFrom() var new By applying to any we create a which forces bounds on the . .ChooseFrom(howMany) Producer Gen<T> Producer Another example could be : StringProducer StringProducer : IProducer< >{ IEnumerator< > GetEnumerator(){ ( ){ Random().Next().ToString();}} public class string public string while true yield return new IEnumerator IEnumerable.GetEnumerator() { **return** GetEnumerator(); } } Again, we could get a from it by doing: Gen<T> stringGenerator = ( StringProducer()).ChooseFrom(20) var new We also included a way to generate tuples of the form by using . (T, K) TupleProducer<T, K> TupleProducer<T, K> : IProducer<(T, K)>{ IProducer<T> ; IProducer<K> ; public class private readonly _tProducer private readonly _kproducer **public** TupleProducer(IProducer<T> tProducer, IProducer<K> kproducer) { **\_tProducer** \= tProducer; **\_kproducer** \= kproducer; } **public** IEnumerator<(T, K)> GetEnumerator() { **var** tenum = **\_tProducer**.GetEnumerator(); **var** kenum = **\_kproducer**.GetEnumerator(); **while** (tenum.MoveNext() && kenum.MoveNext()) { **yield return** (tenum.**Current**, kenum.**Current**); } } IEnumerator IEnumerable.GetEnumerator() { **return** GetEnumerator(); } } Once more, we can get a generator from it using . .ChooseFrom ForAll & Any Once we have created a generator we can use and as existential quantification to proof that a property holds for the values being generated. .ForAll .Any Let’s look at some examples by testing the following functions: id< >( x) => x; T T T sqrt( x) => x * x; int int concat( a, b) => a + b; string string string Notice this is perfectly valid code, here we are only using method body syntax, the equivalent in a more traditional will be: C# C# id< >( x) { T T T x;}... return Let’s move forward to test these functions. [Fact] Identity(){Assert.True(( IntProducer()).ChooseFrom().ForAll(x => id(x) == x));} public void new receives the statement we want to state about the values. .ForAll In the case of , we want to proof that . Identity for all x, id(x) == x The property will be evaluated for every single value on the generator. Now, let’s test sqrt: [Fact] SomeSquares(){Assert.True(Gen< >.FromEnumerable(Enumerable.Range(1, 100)).Any(x => sqrt(x) == x));} public void int In this case, the property is only valid for the value . Since we are using the test is also a passing test. It is enough that the property holds for at least one value when we are using . sqrt(x) == x 1 .Any .Any Finally, we can test . concat [Fact] Concat(){ stringProducer = StringProducer(); producer = TupleProducer< , >(stringProducer, stringProducer); public void var new var new string string producer.ChooseFrom().ForAll(t =>concat(t. , t. ).StartsWith(t. )&&concat(t. , t. ).EndsWith(t. )).Should().BeTrue();} Item1 Item2 Item1 Item1 Item2 Item2 This one is very interesting since we are using the and together. StringProducer TupleProducer In here, we generate random strings, concat them together and proof the property: . Our library will choose 100 strings (bounded collections or ) produced by the and test that the property holds for all of them. for all a, b that are string, a + b == a* && a + b == *b Gen<T> StringProducer Notice we are doing the assertions through the FluentAssertion API instead of simply Assert . However, it is just to show that our library is not limited to one testing framework or assertion style. Endings Even though this is a very small library, it does its job well and allows us to bring a simplified property based testing to . .net you can start using it today by getting it from the NuGet Gallery. PM> Install-Package NetChecker -Version 1.0.0 Contributions are always welcome on the GitHub NetChecker Project . If you want to learn more about Property Based Testing you can read Exploring Property-Based Testing with ScalaCheck (simple examples)