Test Driven Development

8 07 2009

TDD (Test Driven Development). What is it? According to Extreme Programming, TDD is an incremental software development approach that relies on automated regression tests to steer development activities .

By using TDD, the steps for developin software are Test – Coding – Design. This is the simple life cycle for develop a system using TDD.

Life Cycle - Test Driven DevelopmetLife Cycle – Test Driven Developmet

Let me explain the life cycle for you:

  1. Provide some test lists (test cases).
  2. Create a simple code for test lists that you have been provided and compile the code.
  3. You will get some errors because the code is not complete.
  4. Implement your code and compile again.
  5. By using some testing framework run all the tests.
  6. If fail, observe and revice your code and compile again.
  7. If still fail, repeat the point number four until you get pass.
  8. If pass, improve again your code wihout changing the fungtionality of your code.
  9. If all the tests have been passed and fix, you can go to next step to begin a new cycle.

If you just read the theory without an example, it just like a folktale. Right? Now I will give an example to use the TDD approach.

Assume that we want to make a service to add and multiply.  Let start from provide some test lists. So what are the test lists. Add and multiply, these are the test lists.
using NUnit.Framework;
public class MyServiceFixture
{
Assert.AreEqual(4, myService.Add(2, 2));
}

This is test case for add. You want to ensure that the result of 2 + 2 is 4. You can use method AreEqual to check the result. Compile your code. Some erros will occur, because the object myService is not identified. So make instatiate of an object myService.
using NUnit.Framework;
public class MyServiceFixture
{
IMyService myService = new MyService();
Assert.AreEqual(4, myService.Add(2, 2));
}

Compile again your code. Some errors have been reduced. Wow interface IMyService and class MyService are not found. Create the interface and class.
public interface IMyService
{
int Add();
}

public class MyService : IMyService
{
public MyService() {}
public int Add(_firstNumber, _secondNumber)
{
return _firstNumber + _secondNumber;
}
}

Now lets compile again your class MyServiceFixture. What is the result. No errors will be occured. You can try for the multiply test case.


Tindakan

Information

Tinggalkan komentar