Clojure's library for Property-Based Testing is called test.check. It's a powerful, full-featured library for getting your computer to write tests for you and finding hard-to-reproduce bugs.
Here's how Property-Based Testing works:
- Define the random values the computer will generate—these are called your generators.
- Define a boolean expression where true indicates passing, and false indicates failing—this is your property.
- Tell the computer how many tests to generate.
test.check will generate random inputs, run them through your code, and see if they pass. It will generate way crazier inputs than you'd ever think to generate, and as many as you ask for. This is why test.check can find bugs that you would have missed.
However, it's not done there. Seeing a crazy, random value that happens to fail is not helpful. Why did it fail? What, in particular, is wrong with it? That's where the magic happens. test.check will figure out a minimum failing value—through a process called shrinkage—that is so small and clear that you can know very easily why it failed. Shrinkage is one of the keys to making Property-Based Testing work for you.
What you will learn in this course
Property-Based Testing is a powerful technique and there's a lot to learn. This beginner's introduction to the topic will give you everything you need to get started.
- How to use built-in generators and how to build your own
- 3 strategies for defining properties so you'll never get stuck
- Testing before you implement, like in TDD
- Testing after you implement, which is only possible with property-based testing
And, of course, plenty of examples for each of them. You'll also get an in-depth understanding of how the whole process works.
In this course, you will be watching me test code. You'll see how I read error messages and get out of trouble. You'll see how I develop the tests interactively using the REPL. You'll see how to use the REPL to better understand your generators and properties.