TDD – Become a „better” programmer

testingoautomation

TDD

Test-Driven Development is not just for testing. TDD is a technique for programming.

TDD philosophy is based on the writing of code in the first place before production code. What it gives us?

First we need to rethink the design of our code. Advantage of this approach is that, it gives you the ability to detect potential errors like problems with dependencies. Next, any expansion/changes in our program will be easily implemented because in test code, we can see if our change doesn’t cause an error somewhere else. Thanks to this we don’t lose time for debugging. And of course,  tests can serve as valid documentation.

The advantages we can find probably more but what about the negatives?

Of course, we must take time to learn this approach.We have to also take care of test code even more than production code.Prolongs time of the development(but I think it is the same if you add time spent on debugging in the no-TDD approach).

OK. So, now we know more or less TDD advantages and disadvantages. Let’s look closer this issue.

The Cycles of TDD

Key aspect of TDD approach is the cycles Red-Green-Refactor.

 

06_red_green_refactor

  1. Red – Create a unit tests that fails
  2. Green – Write production code that makes that test pass.
  3. Refactor – Refactor your code (like removing duplication etc.)

Let’s see how it’s working on the simple example.

Our solution and test project references:

solution

In this project i use xUnit and FluentAssertions.

Our class Calculator in main project:

At the beginning we write tests(red phase). Test method should be divided into three parts.

Arrange –  all necessary preconditions and inputs

Act – on the object or method under test

Assert – that the expected results have occurred

 

We have two test methods. In first we divide four and two and we expect a result – two. In the second we expect that our program throw DivideByZeroExepction when we divide by zero.

red phase

Our red phase is completed. We received error becouse we didn’t implemented our Divided method in Calulator class.

So implement it(green phase).

green phase

Now our test are green. We can now move to phase refactoring. We should do something with DividedByZeroExeption.
For example, to display the information to the user that he divides by zero, show him message in Message.Box , web page, or something like this etc. ;).

For example write new static class with a method of validating data(that checks if our „b” isn’t equal to 0) and informs us that something is wrong if „b == 0”.

Summary

Tests are divided into unit tests, integration tests, regression tests, acceptance tests. All types of this tests require separate posts. But if someone interested in it, on the bottom I put useful links.

I hope my article thought little interest you approach TDD.

TDD is very broad and interesing topic. For more information about TDD i refer to the blog DariuszWoźniak.NET  and an „expert” in this field to the blog DevStyle.PL by Maciej Aniserowicz. Recommendable is also a podcast that is on the blog Forever F[r]ame. But for surely everyone knows these blogs 😉

Link to the project.

2 Comments on “TDD – Become a „better” programmer”

  1. „We should do something with DividedByZeroExeption.
    For example, to display the information to the user that he divides by zero, show him message in Message.Box , web page, or something like this etc” – klasa „Calculator” na pewno nie powinna zajmować się takimi rzeczami 😀

Dodaj komentarz

Twój adres email nie zostanie opublikowany. Pola, których wypełnienie jest wymagane, są oznaczone symbolem *