Back to the glossary

Process and automation

Test-driven development (TDD)

In test-driven development, a test describing the desired behaviour is written first and initially fails. Only then is the code written that makes it pass.

Also known as: test-first development, behaviour-driven development, BDD

The cycle: red, green, refactor

  1. Red: write a small test describing the next desired behaviour. It fails.
  2. Green: write just enough code for the test to pass.
  3. Refactor: clean up the code while all tests stay green.

One cycle often takes only a few minutes.

What TDD brings

  • Every function has tests from the start, coverage comes as a by-product
  • Code is designed to be testable, with clear interfaces and few hidden dependencies
  • Refactoring becomes safe, because tests report any unintended change

TDD on embedded systems

Firmware can be developed test-first too. The logic is separated from the hardware and tested on the development machine with test doubles for drivers and registers. This fits well with software-in-the-loop.

Behaviour-driven development carries the idea over to business requirements. Examples are written in structured language (“Given … When … Then …”) and executed directly as automated tests.