in Programming, Software Engineering, Software Testing

Avoid Fakers in Unit Tests

In your unit and integration tests, avoid using tools that automatically generate test input data. Unless you are fuzz testing or your goal is to create property-based tests, these test data generators (a.k.a. fakers) are likely to do more harm than good in the long run because:

  1. Your tests will become much slower than they should be. The computational cost of randomization and data generation adds up much quicker than you might think. Slow tests are productivity poison.
  2. By using fakers in your tests, you increase the likelihood of flakiness due to randomized data. Your future self will be more likely to have a tough time figuring out why a specific test failed. Tests should be repeatable and deterministic, with input values that stay the same each time you execute them.

It is often better to explicitly specify your test inputs. If you have to use a faker in your tests, make sure it always generates the same test inputs each time your tests are executed. You can do this by configuring your faker with a random seed.

Write a Comment