Tuesday, March 2, 2010

Easyb Revisited - A Groovy based BDD Testing Framework

easyb is a behavior driven development framework for testing. easyb accepts the Groovy syntax for better explanation of the test scenarios. The first time i encountered easyb, i could not understand the syntactical and semantical sugar but after learning Groovy now i am finding the framework pretty good and useful.

easyb provides two types of templates: one is Ruby RSpac based "it 'should...'" style and another is "given".."when".."then" style. I use the "given.." style because its very explanatory and yet simple make someone understand.

Let's take an example to understand the benefits of the easyb from a programmer and non-programmer's point.

// A utility class to be tested
public class StringList{
      public void add(String element){
           if(element == null){
                  throw new RuntimeException("Null values not allowed");
           }
           // add value
      }
}

//easyb story - given..when..then style
scenario "check StringList with null value addition", {   // scenario with a reason
     given "an empty StringList", {  // what all things available in the scenario
          strList = new StringList()
     }


     when "insert null value", {   // what all operations has to be carried out in the scenario
          insertNull = {
                strList.add(null)
          }
     }


     then "a runtime exception should be thrown", {  // what should be the result
           ensureThrows(RuntimeException){
                  insertNull()
           }
     }
}

//easyb story - it 'should..' style
description 'check StringList with null value addition'


before "initialize the StringList before each spec", {
    strList = new StringList()
}


it "should throw an exception when null value inserted", {
    ensureThrows(RuntimeException){
         strList.add(null)
    }
}

The above stories can be easily divided into two parts one who writes plain test scenarios and another who fills the valid code for testing. No additional knowledge or logic is required to write the test case. An online system can very well generate this kind of templates based on the user input.

// plain test scenario
scenario "check StringList with null value addition", {   // scenario with a reason
     given "an empty StringList", {  // what all things available in the scenario
     }

     when "insert null value", {   // what all operations has to be carried out in the scenario
     }

     then "a runtime exception should be thrown", {  // what should be the result
     }
}

The development or testing team can fill the scenario blocks later with actual code. easyb generates report with pending scenarios and executable scenarios. Pending scenarios are those in which no test code has been added so user can easily identify pending test cases from the reports.

The textual description is right available with the code and is also executable and comes in the result report. The approach helps team members to understand the test cases without much efforts or external documentation. 

The easyb comes in a .tar.gz distribution having easyb jar along with Groovy and Apache Commons CLI. So no additional Groovy dependencies are required.

...dev\tdd\easyb>java -cp .;$easyb_home\easyb-0.9.6.jar;$easyb_home\commons-cli-1.2.jar;$easyb_home\groovy-1.6.4.jar org.easyb.BehaviorRunner *.groovy

The above statement will execute all the .groovy files through easyb interpreter BehaviorRunner. There are various switches for detail result of the execution.

easyb generates the result reports in various formats like PlainText, HTML and XML, which can be integrated with any test-case reporting system.

The flexible syntax helps the business analyst and testing team to create test scenarios independent of the development team. A web-based test scenario identification system would be very helpful in generating various kinds of test cases from different users.

I use Netbeans IDE for all my development purpose. There is no plugin available for NetBeans but Eclipse and IntelliJ provides plugin for easyb. For NetBeans one can use the Ant scriptlets provided at Running easyb.

Additional refrences:

No comments:

Post a Comment