If a car has Test-Drive, how about your code?

Ngeeeeng…. This is a person doing Test Drive in a Car (source: https://www.blackxperience.com/blackauto/autotips/pentingnya-melakukan-test-drive)

Assume you are working on a big project that has thousands of customers. You wake up, thinking about your plan for today. Then, the boss tells you that the project has a bug after someone made a very little change to fix an issue, that he/she doesn’t know will create a bigger issue.

‘GO FIX THAT BUG BEFORE THE USERS UNINSTALL OUR APP’ — said your angry boss. So you and your teammates fix that annoying bug. You fix this bug in 15 minutes, and you’ve already pushed that fix. Then, another bug comes out and your boss is crying. Aww :(

TDD Benefits

The story above is one of the reasons why TDD is important in developing an application, especially the big ones. The Software Engineering course that I take is my first real project that needs TDD in the development cycle. The best thing about TDD in my experience is the ability to change. What this mean is that TDD allows us to change some of the code while maintaining that the functionality is still working shown by the unit and functional test is passed.

So if you want to merge your new features into the project, you must make sure that your change to the project won’t affect the tests that passed before. This is also one of the benefits of TDD, that is test automation. Without TDD, you will test every features of your project manually, like clicking this button, filling this form, etc. TDD will help you to automate this steps using functional tests.

Wow, TDD seems interesting. What is that?

TDD, abbreviation for Test-Driver Development is an approach in software development which uses test cases to specify and validate what the code will do (guru99.com).

Process of TDD

The first phase of TDD is RED. This is when you create the tests to check whether your implementation works or not. You may create your tests and then when you run the test code, the test should fail.

Then, the GREEN phase will come. In this phase, you will create the implementation so your tests created in the RED phase will be passed by your implementation.

When you feel the need to change your code, you are on the REFACTOR phase. After you modify your code, make sure that your change does not make the test fail.

The code above is the example of my test for a Forgot Password screen. If you enter the right email format, the page should not give you any warning. So, I created the implementation (I cannot give the code because it’s sooooo long). Then I see that I have an unused import in my code. So, I refactored my code to remove that import.

There you have it! You have done TDD if you follow my steps. Here is the sample of the TDD process in Git:

Process of a TDD

How to create good tests?

When you create tests, you should follow the FIRST principle which consists of:

  • Fast. The test you create should be able to run quickly, so the developer can fix the bug as soon as the test is finished.
  • Isolated. No test case should depend on other test case.
  • Repeatable. Your test should be repeatable in any environment, like development, staging, and other environment.
  • Self-validating. The tests should be able to verify if the program pass the tests or failed.
  • Timely. Don’t waste your time in creating tests. You should write test with timely manner.

Ending

Thank you for reading this article, I hope you can implement Test-Driven Development in your future projects.

References

Buku Panduan Git Flow PPL 2021

Buku Panduan Git Deployment PPL 2021

Buku Panduan Definition of Done PPL 2021

https://www.guru99.com/test-driven-development.html

A Computer Science Student at Universitas Indonesia

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store