TDD is one type of Unit testing, just like Scrum is one type of the Agile methodology. In this section, we will discuss why we believe TDD is not only in line with the teachings of DevOps, but is a critical piece of accomplishing some of the key components of DevOps. Below we will highlight how TDD actually works hand in hand with DevOps.
While Unit testing does create a feedback loop for the developer and team who creates the Unit tests, TDD actually creates a feedback loop earlier in the cycle, which helps to prevent defects and improves quality. How does TDD create an earlier feedback loop than traditional Unit Testing? In traditional Unit testing the developer would write the code and only after the code was complete would the developer write the Unit tests and run them to confirm the code works, thus only getting feedback after the fact. In TDD, the developer would write a test that meets the requirements (preferably using Specification by Example) and then would write the code to pass the test. This creates a feedback loop immediately as to whether or not the developer is writing code that meets the requirements.
Higher Acceptance Rate
Because Specification by Example (also known as BDD and ATDD) has become almost a subset of TDD, when paired with a DevOps environment, organizations will find that the implementation of the code is more likely to match the product owners vision for the requirements. Writing TDD test cases that are based off of Specification by Example allows the test cases to be easily generated from the acceptance criteria without interference from the constraints of the architecture design or programming constructs. By pairing TDD with Specification by example we guarantee to some degree that the final version will fulfill the needs of the customer as communicated by the product owner.
The combination of Specification by Example and TDD helps to prevents unneeded design ideas or components to sneak into the product. Specification by Example guides the test cases, to help define the exact set of required features. By using TDD along with tools such as SonarQube, we make it easy to identify redundant code.
TDD along with Specification by Example work well with a customer-centric agile process that is at the heart of DevOps. Through TDD, our iterations can now be defined as the implementation of functionality to execute against a pre-defined set of specification test cases instead of the more traditional set of specifications.
One of the requirements for DevOps is that developers write code in small snippets and constantly check this code in. In other words, we are attempting to create code that is encapsulated and independent. Through the use of TDD, we can create encapsulated, flexible, and extensible code. The TDD technique requires that developers think of software as small units to be written and tested independently and then integrated together later.