Test Driven Development in PsychoTip

Alya Putri
PsychoTeam
Published in
3 min readMar 20, 2019

Test driven development has become popular over the last few years. This particular technique is something that is, in my opinion, easy in theory, but challenging to implement. I came to know this practice from the Web Programming course I took 2 years ago, give or take. Since then, I have used it in multiple occasions, albeit with a little bit of struggle. But even so, I have to admit that TDD does make an impact during software development.

What is Test Driven Development?

Test driven development, or TDD for short, is essentially a programming practice in which programmers develop and run automated test before actual development of the application.

Uncle Bob describes TDD with three rules:

- You are not allowed to write any production code unless it is to make a failing unit test pass.

- You are not allowed to write any more of a unit test than is sufficient to fail; and compilation failures are failures.

- You are not allowed to write any more production code than is sufficient to pass the one failing unit test.

Those rules define the mechanics of TDD, but they are definitely not everything you need to know. In fact, the process of using TDD is often described as a Red/Green/Refactor cycle.

Red/Green/Refactor?

In the red phase, tests are written according to the behavior that will be implemented. The green phase is usually the easiest phase, because in this phase you write (production) code. If you are a programmer, you do that all the time. Lastly, refactor. In the refactor phase, you are allowed to change the code, while keeping all tests green, so that it becomes better. In optimizing the code, keep in mind to remove all sorts of code duplication.

Now that I’ve elaborated the theory, let’s look at an example from PsychoTip shall we?

TDD Example

The code above is an example of how a test might look in Java using the JUnit and Espresso test framework. In the test, a couple of things are checked,which will be elaborated below:

  1. testRegisterButton(): The component with the ID daftar_button is on the page and has the correct label.
  2. testRegisterIntent(): The component with the ID daftar_button will load a new page called RegistrationList when clicked.
  3. testLoginButton(): The component with the ID login_button is on the page and has the correct label.
  4. testLoginIntent: The component with the ID login_button will load a new page called LoginPage when clicked.

When run at first, it will fail as it doesn’t have any implementation yet. This is part of the red phase.

Next step is to implement the functions needed in order to pass all of the tests. The implementation can be seen below.

Now, when the test is run, each tests will pass. This is part of the green phase.

So, What is All This For Actually?

The short answer is “because it is the simplest way to achieve both good quality code and good test coverage”.

Yes, it requires a lot of time learning/mastering TDD as well as understanding how to set up and use a testing environment. But, when you are finally familiar with the testing tools and the TDD technique, it actually doesn’t require more time. On the contrary, it helps keep a project as simple as possible and thus saves time.

Also, in my experience, I would already know the rough draft of the function that I want to implement after writing the tests. This makes it far easier to code the implementation, and saves time again.

--

--