TDD – Become a “better” programmer

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:

public class Calculator
{
    public int Divided(int a,int b)
    {
        throw new NotImplementedException();
    }
}

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

 

public class CalculatorTest
{
    [Fact]
    public void Divided_DividedFourAndTwo_ResultShouldBeTwo()
    {
        // Arrange:
        Calculator calc = new Calculator();

        // Act:
        var result = calc.Divided(4, 2);

        // Assert:
        result.Should().Be(2);
    }
    [Fact]
    public void Divided_DividedByZero_ResultShouldThrowDivideByZeroException()
    {
        // Arrange:
        Calculator calc = new Calculator();

        // Act:
        var result = new Action(() => { calc.Division(4, 0); });

        // Assert:
        result.ShouldThrow<DivideByZeroException>();
    }
}

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).

public class Calculator
{
    public int Divided(int a,int b)
    {
            if (b == 0) { throw new DivideByZeroException(); }
            return a / b;
    }
}

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.

One Comment on “TDD – Become a “better” programmer”

Leave a Reply

Your email address will not be published. Required fields are marked *