Wednesday, 1 August 2012

TDD,BDD!Agile Development Methods!

All would have been familiar with writing the code first and then testing the required module using the test case required.
TDD is an evolutionary approach which is being followed now a days which involves test first approach along with the code refactoring,as and when the code is developed.

Why Test Driven Design when you have a testing team??
-->Lets you be more clear about the functionality when you do the testing module .
-->Improves your code,single responsibility principle is maintained.

How is it Done??
-->First you write a piece of code which is the testing code used for testing.It will be put in a separate class .This testing code calls the actually "To be tested Module".
-->You hard-code a sample input and output.And you send input if required to the "To be tested Module" and execute the code.Once the "To be tested Module" returns a value,Compare it with the expected hard-coded output.Then based on the equality of the hard-coded and the returned output you can conclude the test.
-->Now you have to develop the "To be Tested module".Initially give a simple return statement returning the expected value,then slowly implement the actual functionality till you reach a clean code.
-->Execute the testing code and check for a positive result.
-->Repeat this for all the small modules in that particular functionality.Once its all done integrate it and put it all into a single module (has the complete flow)which ensures Success of the complete functionality.

Consider a  functionality in which you post a comment to a post and the following things are supposed to happen.
1)Must be posted in the corresponding post.
2)Must be updated in the database.

Consider the first functionality,Il give you a sample c#code which works on visual studio 2005 and NUnit.

1)To display the corresponding post:

[Test]
public void post_page(String post1)
{
String p1=post_fun(4,post1);
Assert.AreEqual("1",p1,"It is posted");
}


public void post_page(String post1)
{
String p1=post_fun(4,post1);
Assert.AreEqual("0",p1,"It is not posted");
}




[Actual code]
public int post_fun(String sample)
{
int n=0;
 n=Comment_To_Post(pid,);  //returns 1 if it successfully posts the comment
returns n;
}

[Test]
public void db_update(String s)
{

String s1=db_fun(s);
Assert.AreEqual(s,s1,"It updates in the db");

}


public void db_update(String s)
{

String s1=db_fun(s);
Assert.AreNotEqual(s,s1,"It is not updated in the db");

}



[Actual Code]
public String db_fun()
{
//update into the db and return the string if successful or null string otherwise.
}

The above 2 test codes should be put together into a complete module which maintains a flow that includes the checking both the tests.If this test passes completely then the functionality works fine.


Consider another example where the an unique no is to be given which should be 

1)Should have only numbers.
2)No alphabets or special characters.
3)between 8-16 characters .


Testcase id                      Input given                   Test Case Status                    
TC1                                 123                                   Fail
TC2                                 123456789                      Pass
TC3                                 45735464...(19digits)      Fail
TC4                                 gdhfg3654                       Fail
TC5                                 fh5*                                Fail
TC6                                  *%$&                             Fail (Edge case Scenario)

Actually the edge case scenario is the one which can very rarely occur.It is a subset of negative case.

The above cases can be put in the testing script as shown in the above ex and can be executed,to check for any errors.
While integrating the various modules if you don't have certain modules ready use stubs and drivers.

Behaviour Driven Development:
-->Very useful in agile development.
-->All developers,testers,and the design specification uses the same language.
-->minimizes the stress in relating design,implementation and schema.

- Sourcebits University
Cloud Computing
www.sourcebits.com


No comments:

Post a Comment