We define test data as any data we consistently use to verify properties such as a system’s functionality or performance. Another popular term for the same concept is fixture. We will use these interchangeably.
It is often desirable to DRY up our scripts by factoring out test data. In the case of simple tests, it’s usually not an issue to embed test data directly inside our script, but complex end-to-end scenarios might require moving this information elsewhere, like a dedicated file.
Looking at our test webshop, we might want to verify that a specific item list is loaded on the store’s front page. As this list contains several tens of elements, each with different attributes, keeping our fixtures inside our script would be impractical. Let’s add this data to a JSON file instead:
We are then able to feed this file into our test…
…and have each comparison executed to ensure the right elements are being shown.
If the platform you are testing exposes an API endpoint to pull up-to-date test data, you could fetch the file as part of the setup phase of your test and then utilize it:
This approach enables testing with production data, so you don’t have to maintain the test data yourself.
If you test form submissions, relying on generated data might also make sense to avoid only testing the happy path. Faker is a library to create fake but realistic data sets.
Faker covers a wide range of data that includes names, addresses, products, images and much more.