Avoiding duplication
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:Retrieving test data
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:Generating test data
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.Takeaways
- Keep test data separate from your scripts.
- REST APIs can be useful for retrieving test data.
- Libraries such as Faker can help generate test data.