Test-Driven Development

Out of the Agile movement emerged a number of core practices which were felt to be important to guarantee not only quality software but also an enjoyable working experience for developers. Ron Jeffries summarises these excellently in his article introducing Extreme Programming,[11] one of a family of Agile approaches that emerged in the early 2000s. Some of these practices can be introduced as good habits, and don’t require much technology to support their implementation. Of this family, the practice most crucial for creating a supportable structure for imminent change, providing insurance and warning against unwanted side effects, is that of test-driven development (TDD). For infrastructure developers, the practice is both the most difficult to introduce and implement, and also the one which promises the biggest return on investment.

TDD is a widely-adopted way of working that facilitates the creation of highly reliable and maintainable code. The philosophy of TDD is encapsulated in the phrase Red, Green, Refactor. This is an iterative approach that follows these six steps:

  1. Write a test based on requirements.

  2. Run the test, and watch it fail.

  3. Write the simplest code you can to make the test pass.

  4. Run the test and watch it pass.

  5. Improve the code as required to make it perform well, be readable and reusable, but without changing its behavior.

  6. Repeat the cycle.

Kent Beck[12] suggests this way of working brings benefits in four clear areas:

  1. It helps prevent scope from growing—we write code only to make a failing test pass.

  2. It reveals design problems—if the process of writing the test is laborious, there’s a sign of a design issue; loosely coupled, highly cohesive code is easy to test.

  3. The ongoing, iterative process of demonstrating clean, well-written code, with intent indicated by a suite of targeting, automated tests, builds trust with team members, managers, and stakeholders.

  4. Finally, it helps programmers get into a rhythm of test, code, refactor—a rhythm that is at once productive, sustainable, and enjoyable.



[12] Extreme Programming Explained, by Kent Beck and Cynthia Andres (Addison-Wesley)

Get Test-Driven Infrastructure with Chef now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.