Following the Red-Green-Refactor Cycle

Let’s understand different rhythms of TDD with Red-Green-Refactor (RGR) phases.

We know how a single unit test is split into three parts, known as the Arrange, Act, and Assert sections. This forms a simple rhythm of work that guides us through writing every test. It forces us to design how our code is going to be used – the outside of our code. If we think of an object as being an encapsulation boundary, it makes sense to talk about what is inside and outside that boundary. The public methods form the outside of our object. The Arrange, Act and Assert rhythm helps us design those.

RGR rhythm

We’re using the word rhythm here in an almost musical sense. It’s a constant, repeating theme that holds our work together. There is a regular flow of work in writing tests, writing code, improving that code, and then deciding which test to write next. Every test and piece of code will be different, but the rhythm of work is the same, as though it were a steady beat in an ever-changing song. Once we have written our test, we turn to creating the code that is inside our object – the private fields and methods. For this, we make use of another rhythm called RGR.

Get hands-on with 1200+ tech skills courses.