In this episode, we'll take a look at how to simplify repeated tests in PEST using datasets. Last time, we had three nearly identical tests for each book status in our app. While that's totally okay, there's actually a much cleaner way!
We'll start by removing the extra tests and keeping just one. With PEST's with
method, we can pass in an array of different data sets to run the same test multiple times with different values. We'll try it out first with some empty arrays just to see how it works—each test run is listed in your console output.
Next, we modify the test so it actually takes in the book status and heading for each row of test data. We define three sets: 'want to read', 'reading', and 'read'—each with its own heading. Inside the test, we grab the status
and heading
parameters directly from the dataset, and swap them into our logic.
Now, instead of having three tests, we have one test that runs three times, each time with different data! It's tidier and easier to update, especially if you add more statuses in the future. So whenever you have similar tests that just swap out parameters, using datasets like this in PEST is definitely the way to go.