Test Driven Development

Do you still hate testing?

If you’re here, that mean you don’t hate it any more, or you’re still not convinced, because you feel like you’re wasting a lot of time writing useless tests. Today I’ll show you great way to write meaningful tests and clean, testable application code. Unfortunately I can’t say it will be faster. It surly will be better for your clients and for your career.

Haaaave.. you met TDD?

Test Driven Development is way to develop application, where you start from tests. It’s something completely different than normal workflow. I mean, how can we test our code before it even exist? It’s easy, you just need to write tests and application code at the same time. TDD basis is to write test code, before you start developing application.

Red, Green, Refactor, Commit

TDD is based on these words. First you need to create test for small part of functionality. For example simplest happy path, remember that. You don’t want to test everything from beginning, in TDD we create code iteratively, step by step. This will give you time to think about algorithm.

Red                                       

When test is finished you need to run it, and make sure it failed. Test is red. Why? If test passes from beginning it may mean that test will pass every time, even if there’s bug. You can’t skip it, there is nothing worse than a deceitful test. Everyone will think that application works, because test passes, but in truth test passes always, even with commented code.

Green

After that you can start writing application code. You just need to make it green, for now you can ignore how your code looks. There are allowed big methods with copied over code. In green phase important is only to write code that will pass your tests. It is very important, your previous tests have to pass as well before you can go to next phase.

Refactor

When tests are green you can start refactoring. Why in TDD you have to write ugly code first and then refactor it? Because only with tests you can refactor code safely. Tests are your contract, application should work like tests expect it to work. Additionally it’s simpler to write clean code by refactoring. Have you ever thought about creating something perfect on the first run? It is the same, you can’t write better code from the scratch. Only by improving it (refactor) you can get closer to perfection.

Commit

Last step isn’t used by every developer. It’s more like optional step. Did you heard about “commit early, commit often”? If not you’re hearing it now. Using git, you should commit your changes whenever you’ve finished something that didn’t break anything else. There isn’t better occasion to commit than last step of TDD cycle. You implemented some of functionality and added test for it. Code is clean, because you refactored it. Additionally you’ll make easy to read history in git, you’ll aggregate functionality and test within one commit.

What’s in it for you?

First of all, you’ll force yourself to write and use tests. I’m not surprised that developers don’t like tests if they have to write them at the end. Think about it, you’ve finished your task. You’re tired, you’d like to take a break, but you can’t. There are tests to write, sad duty. It is even possible that these tests will not be useful, you will run them once and forget.

With TDD you’ll run your tests multiple times, you’ll have feeling that writing tests wasn’t waste of time. Additionally when you finish implementation, your tests will be there, there’s nothing more to do. It will help to keep up morale high.

Second, you’ll implement functionality step by step. One test for one working functionality. You won’t be sidetracked, because you need to make test green and nothing more. It’s easier to focus when task is small.

Additionally you won’t need to run application during development. You don’t need to test it manually. You can run tests for it. Even better, if you want to find out what’s happening in code, you can run test and debug it. You can use every possibility that unit tests give you while implementing solution.

Finally, TDD helps you write clean and testable code.

How does TDD help writing testable code?

Well, you can’t write untestable code with TDD, because you’ve already written tests. Believe me or not, it is very easy to write code that cannot be unit tested. I worked in project like that.

No one needed tests. When I wanted to test my changes it often took me 5 to 10 min, because I had to upload file on blob storage, then application had to load it into memory and transform data. Files were pretty big so it took time to just download it, let alone transform. For my client it was at least 1 hour wasted time daily.

How does TDD help you write clean code?

Answer is simple, Red, Green, Refactor, Commit. Yep, you’ll have to refactor your code every time tests turn green.

Outro

Please don’t judge TDD from this post. Try it yourself with open mind. Keep in mind that TDD won’t make you work faster. At the beginning you’ll work even slower, but when you get used to it, you’ll not be able to write code without writing test first. Just give yourself time. If you want to learn more about commit part of TDD cycle read this post.

Leave a Reply