IntroToTestDrivenDevelopment



IntroToTestDrivenDevelopment

0 0


IntroToTestDrivenDevelopment


On Github EricCharnesky / IntroToTestDrivenDevelopment

A Brief Introduction to Test Driven Development

In 15 minutes or less

It's a process

Relax, you'll learn to love it or maybe not...

Begin with the end in mind

What does your code need to accomplish?

Design your classes

Design is not wasted time, but writing poorly designed code is.

Write some tests

    @Test
    public void testAdd() {
        assertEquals(5, SimpleMath.add(2,  3));
    }

    @Test
    public void testSubtract() {
        assertEquals( 4, SimpleMath.subtract(42, 38));
    }

I prefer this way

Let the tool do the busy work for you

Write stub classes

public class SimpleMath {
    public static int add( int a, int b )
    {
        return 0;
    }

    public static int subtract( int a, int b )
    {
        return 0;
    }
}

Right click magic

Accept the defaults

Check the box

Test code stubs appear

public class SimpleMathTest {

    @Test
    public void testAdd() {
        fail("Not yet implemented");
    }

    @Test
    public void testSubtract() {
        fail("Not yet implemented");
    }
}

Make sure they fail

Write code to make the tests pass

public class SimpleMath {
    public static int add( int a, int b )
    {
        return a + b;
    }

    public static int subtract( int a, int b )
    {
        return a - b;
    }
}

Run the tests

Refactor

Cleanup the code

Sources

http://www.agiledata.org/essays/tdd.html http://en.wikipedia.org/wiki/Test-driven_development