Why day is day, night night, and time is time,
Were nothing but to waste night, day, and time.
Therefore, since brevity is the soul of wit,"
-- Shakespeare, Hamlet
Act II, Scene II, Lines 87-90
I'll admit it, sometimes I have no freaking clue what I am doing. Much like the internet dog meme, I feel completely out of my league. You have to start somewhere and no clue is often the first stop on the journey to mastery.
"A journey of a thousand leagues started with what was under one footstep."
-- Tao Te Ching verse 64, translated by Jan J. L. Duyvendak
Property Testing allows one to run their code under test through it's paces. Let us take a look at using Property Testing against FizzBuzz and see what we can learn.
We see that we have a fairly simple implementation of FizzBuzz.
For our first test case we use the Test Case property of NUnit to show what the results of the Translate on the FizzBuzzer would be for different inputs. Note, I am showing the result here after many rounds of Red, Green, Refactor. I cannot predict the future and as such I had two different test methods for 2 and 3 and ended up refactoring them to the "one" you see below, but I digress.
This is great, but what happens for other values? How do we know if this really works?
We use the Property Test above using fscheck to show that any number not divisible by 3 or 5 will return the ToString value of the number. We do not have to check 15 since 3 * 5 =15 and therefore it is covered by the Fundamental Theory of Arithmetic.
Next we test that every number divisible by 3 contains the string "Fizz", likewise we do the same with 5 and "Buzz" and 15 with "FizzBuzz". We check that they contain the string, so that if we get a value like 45, which is 3 * 15, for our divisible by 3 value we do not have a failing test because we got "FizzBuzz" back instead of just "Fizz", this is a very important thing to think about with Property Testing. Note also, this is another case were refactoring played a big part in the final result. I did not start off with a DivisibleBy function; no I found that the generate code for the 3 and 5 looked a lot a liked, so I combined them into the function you now see.
To show that the generator was in fact covering all four different possibilities I set up a "test" which was using fscheck's classification to show the break down of the different values which were coming out of Translate.
Last I set up a test case to show that if a value was divisible by both 3 and 5 it will contain both "Fizz" and "Buzz". Now this test case was not needed since it was covered above, but I wanted to show how the And works.
There you have it FizzBuzz using Property Testing. I found the examples in fscheck's GitHub repo very helpful.
Note, I used QuickCheckThrownOnFailure to cause the unit test to actually fail the test runner when the property is not true for some value. This is very important to do if you are using something like NCrunch to run your tests. If you have a property which fails for some value and do not use QuickCheckThrownOnFailure on your tests it will "pass" from the runner's point of view, but the result will give the value which falsifies it, this was not what I wanted so I had fscheck throw an exception when the property was falsified.