The Self-Contained Test

One of the earliest pieces of wisdom we are given as programmers is to not write duplicate code: Don’t Repeat Yourself (or DRY if you prefer). Identical blocks of code to set up a test sure does look like repetition, so we extract it into a before block.

This is a mistake for tests.

The article explains about how sharing setup between examples make test files harder to read and evolve.

A related frustration I have is working on ultra-DRY & betterspecs-like test files where shared setup has worked for all existing tests, but won't work for the test I need to add. I now need to refactor a huge file to add a small test.

Henning Koch Almost 4 years ago