So Predictable

Good code should have test coverage.

However, writing tests is not a box-ticking exercise.

If tests are not written well, they are not helpful. One characteristic of a good test is that its results are consistently reproducible. This helps us see what the problem is every time we run the test.

There is a common mistake I see developers make. Trying to cover a wide range of scenarios, they use a random value in their tests.

To be clear, I am referring to run-time generated random values, not random values generated in advance and hardcoded into the test. The latter can be fine.

When you use random values in your tests, you may end up with flaky tests. Your tests may pass some of the time, and fail at other times.

Both scenarios are bad. If the tests pass, you may have missed out on a bug. Your tests no longer give you confidence. If the tests fail, you don’t know what values made them fail. Will they fail when you run them? Maybe. Maybe not. Maybe only after you ran them ten times. This is time-consuming.

You get the idea. Tests should be boringly predictable. If you want to test multiple values, test them all. Use a parameterized test. Use a loop. Don’t use random values.

Testing should give you confidence. Keep them predictable. If you want the adrenaline rush of gambling, go to Vegas. I hear it’s lovely this time of year.

Published by eranboudjnah

A software consultant and tech lead. Passionate about optimizing as many aspects of my life as possible, to free time for what really matters.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: