.

post

TDD - what is it, exactly?

Kasia
Kasia, Flutter Developer
07.02.2022

TDD stands for Test Driven Development. To put it simply - it is a kind of development process that focuses on testing. Someone who follows TDD principles will write or update test case scenarios for every change they make during development.

TDD is iterative, which means that is repeats following sequence:

  1. Write a test: if you want to add new functionality, first write a test for it.
  2. Run your test: the test should fail, which is expected, as there is no implementation yet.
  3. Write code: now it is time to write your functionality, just so it will pass your test.
  4. Run your test, again: now the test should pass.
  5. Refactor your code: the fact that your code works is, of course, not enough. It also has to be scalable, follow SOLID principles and clean code guidelines. While refactoring your code you should repeat step 4 often to make sure you didn't break anything during the process.

Now... repeat those steps every time you want to add or change something in your application. Simple as that!

But do I REALLY need it?

Well... yes and no.

It may sound like a mundane, tedious task that only wastes your precious developer time. And sometimes you will be right! But other times it will serve as a powerful, time-saving technique.

So, when to avoid it?

  • When your project is really small, simple and will not grow much overtime.  Writing all those tests will not bring any satisfying results as you will be able to see all those possible scenarios in your head.
  • If you don't exactly know what is the solution to your problem. In that case it is better to sketch out your idea and code it first. That way we will understand our problem and make sure you know how to fix it. Only then you will be able to stress test it and check borderline cases. TDD would only slow this process. However, even without TDD, refactoring and unit tests are still covetable.

TDD does not truly mean testing every single thing in your application, no matter how small. Testing trivial code consumes a lot of time and brings little to no value. It is good to be flexible while using any development process, so if you find yourself writing tests that you see no purpose in - stop testing and start coding.

And what does it give me?

Now that you understand when TDD is not an ideal solution - let us see what is so great about it!

TDD minimalizes bugs in the app. By writing tests ahead, you will not waste time on searching for bugs later. This is a time (and sanity) saver - especially when developing more complex apps! TDD ensures a developer that every bit of the code is necessary. This means that there is no borderplate code and that two important rules, KISS (Keep It Stupid Simple ) and DRY (Don't Repeat Yourself), are obeyed.

Refactoring is cleaner, easier and faster. This one makes a huge impact during a long-time development. You know exactly where to start, what to do and if your newly written code worked as it should.

Good tests are your documentation. That means you don't have to spend your time writing dry documentation and updating it constantly. You can just show the test folder and your new team member should quickly grasp what everything does.

Practice, practice, practice...

Writing in TDD is an easy habit, once you know how to do it. To speed up learning process there are some important things you should take into account:

  • keep it small: keeping tests small will make it more readable and easier to debug. It is a common practice to test only one feature per test.
  • don't be scared of the test volume: during TDD, you will find yourself writing a lot of tests. Do not get scared of it! However, try to keep them clean - maybe by separating them into different folders depending on their type?
  • isolate your tests: it is always good to isolate your code and that includes tests. If your tests are interdependent, they are complex and can change behaviour unexpectedly. This means that you will find yourself spending a lot of time debugging tests, while they should prevent you from debugging your code.

Summary

It is important to understand when and why you need a certain development style. TDD is one worth remembering when you are planning to develop a project, especially if they arelikely to last for a long time.

newsletter