Category: Property-Based Testing
-
Creativity is key
Writing property-based tests requires a lot of creativity from the developer. Finding ways to express the property, generating random data, and being able to assert the expected behavior without knowing the concrete input is not easy. Property-based testing requires more practice than traditional example-based testing: get your hands dirty as soon as possible. I hope…
-
Common issues in property-based tests
I see three common issues in the property-based tests my students write when they learn this technique. The first is requiring jqwik to generate data that is very expensive or even impossible. If you ask jqwik to, say, generate an array of 100 elements in which the numbers have to be unique and multiples of…
-
Example-based testing vs. property-based testing
Property-based testing seems much fancier than example-based testing. It also explores the input domain much better. Should we only use property-based testing from now on? In practice, I mix example-based testing and property-based testing. In the testing workflow I propose, I use example-based testing when doing specification-based and structural testing. Example-based tests are naturally simpler…
-
Creating complex domain objects
Building more complex objects may come in handy when testing business systems. This can be done using jqwik’s Combinators feature, which we’ll use in the following listing. Imagine that we have the following Book class, and we need to generate different books for a property-based test. Listing 5.21 A simple Book class One way to do this would be to have a…
-
Testing the Basket Class
Let’s explore one last example that revisits the Basket class. The class offers two methods: an add() method that receives a product and adds it a quantity of times to the basket, and a remove() method that removes a product completely from the cart. Let’s start with the add method. Listing 5.12 Implementation of Baskets add method ❶ Checks all the pre-conditions ❷ Stores the old value so we can check the post-condition later ❸ If the product…
-
Testing the indexOf method
The Apache Commons Lang has an interesting method called indexOf() with the following documentation, adapted from its Javadoc: Finds the index of the given value in the array starting at the given index. This method returns –1 for a null input array. A negative startIndex is treated as zero. A startIndex larger than the array length will return –1. Input…
-
Testing the unique method
The Apache Commons Lang offers the unique method. Following is its adapted Javadoc: Returns an array consisting of the unique values in data. The return array is sorted in descending order. Empty arrays are allowed, but null arrays result in a NullPointerException. Infinities are allowed. Parameters: The method returns a descending list of values included in the input array.…
-
The passing grade program
Consider the following requirement, inspired by a similar problem in Kaner et al.’s book (2013): A student passes an exam if they get a grade >= 5.0. Grades below that are a fail. Grades fall in the range [1.0, 10.0]. A simple implementation for this program is shown in the following listing. Listing 5.1 Implementation…
-
Introduction
So far, we have been doing example-based testing. We judiciously divide the input space of a program (into partitions), pick one concrete example from all the possible ones, and write the test case. What if we did not have to pick one concrete example out of many? What if we could express the property we are trying to exercise…