Automated Tests Should Act as Accessible Documentation

I write Elixir/Ash apps at Alembic. Here is my approach to automated testing, very much influenced by BDD (behavior driven development) and focused on heuristics like “does this make me feel more confident implementing this?” rather than metrics like “have we achieved X% test coverage?”. Also we aim to have improved documentation and accessibility as by-products, using these as lenses to guide writing our tests.

Ask: what am I wanting to gain by writing this test?

Before writing the test decide what you and your team gains from having the test. Having more tests isn’t necessarily better. The “wrong” test can be expensive if it has to be rewritten all the time, often fails in CI, or verifies things of little value.

Ask: what is the interface to what I’m implementing?

Imagine how what you are implementing is then used. Who or what uses it? This then tells you how to test it. Your test puts your implementation through its paces through its interface and checks it behaves how you expect.

Types help check the interface has a certain shape, automated tests check the interface behaves a certain way.

Tests should educate a reader on the system and the world around the system

Tests should be without branching or clever abstraction and be simpler than the implementation under test

Tests are like a scientist running an experiment and then making a bunch of observations and verifying those match expectations

Look for ways to avoid tests