QA‎ > ‎

JBehave

JBehave is implemented based on Behavior Driven Development concept. BDD is quite advancement of TDD. TDD (Test Driven Development) focus more on code coverage and does not focus on actual behavior of the application. That's BDD came into picture.

There are many tools or frameworks implemented using BDD. Following are the few tools, but not limited to.

JBehave - Java
NBehave - .NET
JSSpec and Jsmine using Java Script
CPPSpec using C++
Cucumber using Ruby
CSpec using C

JBehave is doing good in the software market to test applications. User can write test scenarios in story format. For example to test addition functionality in Calculator application, here are the few test scenarios.
  1. Add 2 and 3 result should be 5
  2. Add 0 and 2 result should be 2
  3. Add 0 and 0 result should be 0
For the above test cases a typical test automation looks like this

import calc;

addTest(2,3)
Assert.AssertEqual(5,calc.add(a,b)

addTest(0,2)
Assert.AssertEqual(2,calc.add(0,2))

addTest(0,0)
Assert.AssertEqual(0,calc.add(0,0)


Even-though there is fun in writing lines of code for test automation there are some disadvantages of this. we need to expect automation skill from each tester. Report generation after execution. etc.

BDD would be the better solution for this kind of problems. Test scenarios will be written in EBNF format with simple English.

For the same test cases, BDD test scenarios would be like this.

Scenario: adding 2 and 3
Given user has two inputs 2 and 2
When he adds
Then result should be 5

Scenario: adding 0 and 2
Given user has two inputs 0 and 2
When he adds
Then result should be 2

Scenario: adding 0 and 0
Given user has two inputs 0 and 0
When he adds
Then result should be 0


It is evident from the above scenarios that writing test scenarios is very easy and it does not expect much experience and skillet from the testers. Tester can achieve this with basic training on BDD.

JBehave example:


For JBehave implementation following are steps:

  1. Write story
  2. Maps JBehave steps of story to Java
  3. Configure stories
  4. Run stories
  5. View reports

1. Writing story:


Scenario: adding 2 and 3
Given user has two inputs 2 and 2
When he wants to add
Then result should be 5

Scenario: adding 0 and 2
Given user has two inputs 0 and 2
When he wants to add
Then result should be 2

Scenario: adding 0 and 0
Given user has two inputs 0 and 0
When he wants to add
Then result should be 0

2. Maps JBehave steps of story to Java:

package com.pavan.jbehave.calc.steps;

import junit.framework.Assert;
import org.jbehave.core.annotations.AfterStories;
import org.jbehave.core.annotations.BeforeStories;
import org.jbehave.core.annotations.Given;
import org.jbehave.core.annotations.Named;
import org.jbehave.core.annotations.Then;
import org.jbehave.core.annotations.When;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.WebDriverWait;


public class CalcSteps {
public int a,b;
public int result;
@BeforeStories
public void initialization(){
                //TODO: some initilization
}

@Given("user has inputs $x and $y")
public void userHasInputs(int x, int y){
a=x;
b=y;
}

@When("he wants to add")
public void heWantstoAdd(){
result = a+b;
}
@Then("result should be $rvalue")
public void resultIsDisplayed(){

Assert.assertEquals(value, result);
}
public void add(int a, int b){
result = a+b;
}

@AfterStories
public void afterStories(){
            //TODO: some steps to perform after execution of stories
}
}

3. Configure stories:

package com.pavan.jbehave.calc;

import static org.jbehave.core.io.CodeLocations.codeLocationFromClass;
import static org.jbehave.core.reporters.Format.HTML;
import java.util.List;
import org.jbehave.core.Embeddable;
import org.jbehave.core.configuration.Configuration;
import org.jbehave.core.configuration.Keywords;
import org.jbehave.core.configuration.MostUsefulConfiguration;
import org.jbehave.core.i18n.LocalizedKeywords;
import org.jbehave.core.io.CodeLocations;
import org.jbehave.core.io.LoadFromClasspath;
import org.jbehave.core.io.StoryFinder;
import org.jbehave.core.junit.JUnitStories;
import org.jbehave.core.reporters.Format;
import org.jbehave.core.reporters.StoryReporterBuilder;
import org.jbehave.core.steps.InjectableStepsFactory;
import org.jbehave.core.steps.InstanceStepsFactory;
import org.jbehave.core.steps.ParameterConverters;
import com.pavan.jbehave.calc.steps.CalcSteps;

public class CalcStories extends JUnitStories{

@Override
protected List<String> storyPaths() {
Class<? extends Embeddable> embeddableClass = this.getClass();
List<String> storyPaths = new StoryFinder().findPaths(codeLocationFromClass(this.getClass()), "**/*.story", "");
return storyPaths;
}
public CalcStories(){
configuredEmbedder().embedderControls().doGenerateViewAfterStories(true).doIgnoreFailureInStories(false)
        .doIgnoreFailureInView(true).useThreads(2).useStoryTimeoutInSecs(10);
}
@Override
public Configuration configuration(){
Class<? extends Embeddable> embeddableClass = this.getClass();
Keywords keywords = new LocalizedKeywords();
return new MostUsefulConfiguration()
.useParameterConverters(new ParameterConverters().addConverters())
        .useStoryLoader(new LoadFromClasspath(embeddableClass))
        .useStoryReporterBuilder(new StoryReporterBuilder()
            .withCodeLocation(CodeLocations.codeLocationFromClass(embeddableClass))
            .withDefaultFormats()
            .withFormats(Format.CONSOLE, HTML)
            .withFailureTrace(true)
            .withFailureTraceCompression(true));
}

@Override
    public InjectableStepsFactory stepsFactory() {
return new InstanceStepsFactory(configuration(), new CalcSteps());
}
}


4. Run Stories:

Stories can be run from eclipse and using Apache Maven also. For running from Apache Maven, here is pom.xml file 

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

<url>http://maven.apache.org</url>
<modelVersion>4.0.0</modelVersion>
<name>JBehaveDemo</name>
<groupId>com.pavan.jbehave.demo</groupId>
<artifactId>jbehave-demo</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>

<dependencies>
<dependency>
<groupId>org.jbehave</groupId>
<artifactId>jbehave-core</artifactId>
<version>3.7</version>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>${basedir}/src/main/java</directory>
<filtering>false</filtering>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
<resource>
<directory>${basedir}/src/main/java</directory>
<filtering>false</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<encoding>UTF-8</encoding>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.jbehave</groupId>
<artifactId>jbehave-maven-plugin</artifactId>
<executions>
<execution>
<id>run-stories-as-embeddables</id>
<phase>integration-test</phase>
<configuration>
<includes>
<include>**/CalcStories.java</include>
</includes>
<ignoreFailureInStories>true</ignoreFailureInStories>
<ignoreFailureInView>false</ignoreFailureInView>
</configuration>
<goals>
<goal>run-stories-as-embeddables</goal>
</goals>
</execution>
</executions>
</plugin>

</plugins>
</build>
</project>


Execute following command at command prompt mvn clean install

NOTE: Apache Maven should be installed prior to the execution of the above command.

5. View Reports

After execution of the stories reports can be located at target/jbehave/view/reports.html file.



Comments