The Self-Contained Test

Updated . Posted . Visible to the public. Repeats.

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.

Also see A Quest for Better Specs: Prefer Self-Containment Show snapshot .

Henning Koch
Last edit
Henning Koch
Source code in this card is licensed under the MIT License.
Posted by Henning Koch to makandra dev (2020-06-02 07:16)