TDD can be described as a way of building software incrementally, expressing the desired behaviour of the code in tests: For every new production code, a test is written first. Then just enough production code is written to make the test pass. Any modifications to the code must be validated by running the tests.
The “Red->Green->Refactor” micro-cycle summarizes the approach:
Red: Write a failing test.
Green: Make the test pass.
Refactor: Improve your code, by keeping the test passing.
The tests grow organically alongside the production code. To keep the micro-cycle efficient, fast and automated tests are key to the success.
Robert C. Martin a.k.a. “Uncle Bob”, provides a concise set of rules for practicing TDD.
1. Write production code only to pass a failing unit test.
2. Write no more of a unit test than sufficient to fail (compilation failures are failures).
3. Write no more production code than necessary to pass the one failing unit test.
More than just good test coverage
TDD should not be considered as a testing technique because the benefits are more than just a good test coverage:
The design of the production code is driven by the tests. The focus is first on the interface rather than on the implementation. This results to a more modular design.
The tests provide a “safety harness” for code modifications. The developers can (almost) fearlessly refactor the production code.
The tests provide a documentation that does not lie.
The need of debugging the code can be reduced significantly.
The resulting production code can be released with a greater confidence.
The TDD approach is straight forward to apply to projects running on the development PC written with languages like Python or Java. However, it becomes more difficult to apply it to embedded system firmware written in C.
Keep exploring and read on
This is where the Test Driven Development for Embedded C book comes handy, providing concrete solutions for the embedded space.
Our journey on exploring Test Driven Development for embedded software for wearable devices will continue. Stay tuned!
How are you testing your embedded software?
We are always curious to learn how others apply software development and testinc concepts.
Our team is happy to exchange on best practicesand share our experience in embedded software development and testing. Pick up the conversation with the click of one button… the one below.