Properties
Learn about properties and how property-based tests compare to example-based tests.
Properties
As suggested by its name, property-based testing is about properties. Let’s learn about what thinking in properties looks like.
The difference
Let’s start by taking a look at the differences between traditional and property-based testing.
Traditional tests
Traditional tests are often example-based. We make a list of a bunch of inputs for a given program along with the expected output. We may put in a couple of comments about what the code should do, but that’s about it. Our test will be good if we have examples that can exercise all the possible program states that we have.
Property-based tests
Property-based tests, on the other hand, have nothing to do with listing examples by hand. Instead, we’ll want to write some meta tests. This means that we find a rule that dictates the behavior that should always be the same no matter what sample input we give to our program and encode that into some executable code known as property. A special test framework will generate examples and run them against the properties to check whether the program follows the rule.
Get hands-on with 1400+ tech skills courses.