Saturday, July 20, 2013

Test Driven Development example (Getting started)

There was a workshop in our university to give us an introduction about TDD by OrangeHRM. The followings are based on those experience. 

You can read about TDD here .
Simply the TDD coding process has 3 rules.(for more details refer this)

 1) You are not allowed to write any production code unless it is to make a failing unit test pass.  
 2) You are not allowed to write any more of a unit test than is sufficient to fail; and compilation failures are failures. 
 3) You are not allowed to write any more production code than is sufficient to pass the one failing unit test.
 Here i used java and the net beans IDE to do the example.

We have to develop a program which give us the n-th Fibonacci number.

Let's create a new project called TDD first. Then create a new JUnit test. (right click the project and select JUnit test- If it is not there use other option to find it)

First create a class in Source Packages.(name it  Fibonacci )Then create a test class in the Test Packages.(name it FibonacciTest)(Don't worry if you haven't the Test Packages yet, just right click the project and create a new test, it will create a new Test Packages section ). (you can do it in 3 ways)

In the Test Packages ,
1) Create a normal java class and we can import the relevant Unit Test packages later.
2) Create a JUnit test and give the name "FibonacciTest" manually.(This will automatically call the relevant imports)

3) Create a JUnit test class by using Test for Existing class. (It will automatically set the name "FibonacciTest" and will  call  the imports as well)

Then add the JUnit library (here i used JUnit 4.10) to the Test Libraries.

The Project folder structure will  looks like this.



 

0 comments:

Post a Comment