How to TDD When You Don’t Know Anything

In this article, I’m sharing with you my experience with Test-Driven Development on a new environment. But first, we must understand Test-Driven Development.

What Is Test-Driven Development?

Test-Driven Development (TDD) is a technique to create tests before the implementation code. Writing the test first reduces errors that can occur during development. Test cases make sure that the code remains functional even when other people try to modify your code.

Steps on how to code with TDD:

  1. Write a Failing Test [RED]: Create failing test. Understand the user story enough so that you know what to expect in your implementation. Create a commit with [RED] in the commit message.
  2. Implement to Pass the Test [GREEN]: Implement with a code that just barely make the test no longer error and successful. You don’t want overdo your implementation. When you want to implement more, you need to update the test or create new tests. After you finish implementing, create a commit with [GREEN] in the commit message.
  3. Clean Up Your Code [REFACTOR]: Clean up your implementation and make it more efficient without breaking the test. It means that even after you modify your code, the tests remains successful. After you’re done cleaning up, create a commit with [REFACTOR] in the commit message. Refactor only if you need to.
  4. Repeat Step 1

Benefits of Test-Driven Development

Writing the test first can be difficult, but it comes with a lot of benefits.

  • Easier refactoring
    After the tests is successful, you can safely refactor any part of the code and only make sure that all tests remain successful in the end.
  • Less time debugging
    With TDD you will create more tests, which results in less bugs. You’ll save a lot of time down the line trying to fix bugs that pops up during the development.
  • Living Documentation
    If you’re unsure about how a component or a library works, read the tests. In most cases, the tests contain one of the use case that you are looking.
  • and more …

My Experience With TDD

What if you’re still learning the framework or language and you aren’t sure how to write the test? When I was writing my flutter app for the first time, I’m also new to flutter. I didn’t even know how to write flutter code, let alone creating the test. So here’s what I did.

I created the implementation first, and then I wrote the test after that.

Wait, that’s not TDD at all!

It’s true, It’s not actually like a real Test-Driven Development. But I can make it look like TDD, because I didn’t create a commit yet.

For example: this is my implementation and test for my flutter application:

After writing the test that I know will pass, I commit only the test file.

git add path/to/buat_laporan_test.dart
git commit -m "[RED] my test"

After that, commit the implementation file.

git add path/to/buat_laporan.dart
git commit -m "[GREEN] my implementation"

After making those two commits, your git log should look like you create the test first, and then the implementation after the test.

* 9c54aa (HEAD) [GREEN] my implementation
* fb6bdf [RED] my test
* d357a4 older commits ...
* ...

Once you got used to writing test, you should start writing test before your implementation like how TDD should be, because above method is not good for the long run. So use this knowledge wisely. Right now, I’m familiar with writing tests in flutter and I write the test before the implementation.

Thank you for reading and I hope you find it interesting :)

--

--

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