Thursday, August 30, 2007

mari belajar TDD

today i started coding and practicing TDD. TDD?? just in case u guys haven't heard about it, its kind of practices taken from XP programming emphasis on test should come first before coding. At first it sound very weird to me. Its weird because it add extra work for the developer(actually thats not the only reason). Shouldn't developers concentrate on 1 thing they do the best, that is coding?? and Tester should be the one that write code for automated test aite?? I think in the next 10 years, most jobs for tester will be replaced by automated test created by developer. Bubai tester.

Let me show u simple TDD....


just a simple class student with its test class named studentTest.

1. create the StudentTest class.

public class StudentTest extends TestCase
{
public void testCreate()
{
Student student = new Student("peysal");
}
}


this test will sure fail becoz we haven't create the student class. So we create the student class

2. create the Student class

public class Student
{
public Student(String name)
{

}
}


then try running the automated test, guarantee ok

3. assert what u expected, this 1st assert must fail

public class StudentTest extends TestCase
{
public void testCreate()
{
Student student = new Student("peysal");
assert("peysal", student.getName());
}
}


as u can see, we haven't create the function of student.getName. So u can expect failure when u run the automated test

4. implement the missing function in Student class

public class Student
{
private String name;

public Student(String name)
{
this.name = name;
}

public String getName()
{
return name;
}
}


thats it...u should be getting the green colur. Ok and well done.


Even though, writing automated test is fun. I just don feel comfortable writing code 1st then getting all the syntax error for missing function or what so ever. So maybe i'll adopt TDD style to some extend only, for now. Getting failure in automated test is ok, but getting the error for me....alaaa lecehnya. I want to skip that part.

No comments: