DEV Community

David Adamo Jr.
David Adamo Jr.

Posted on • Originally published at davidadamojr.com

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 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.

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more