jBehave -- Behavioural Driven Development (BDD)
Q. What is BDD?
A. BDD is principally an idea about how software development should be managed by both business interests and technical insight. Test-driven development focuses on the developer’s opinion on how parts of the software should work. Behavior-driven development focuses on the users’ opinion on how they want your application to behave. So, when you start writing a test, you need to think about the stories, and each story should cover three things:
- Given : an input value of 2
- When : you multiply the input with 3
- Then : result should be 6
Even you write unit tests as part of TDD (Test Driven Development) or without TDD , you need to think about Given ... When ... Then ...
Here is a simple example using the jBehave framework in Java. jBehave tutorial.
Step 1: Maven pom.xml file on jBehave dependency
<dependency> <groupId>org.jbehave</groupId> <artifactId>jbehave-core</artifactId> <version>3.8</version> </dependency>
Step 2: Define the story in plain English that business users and testers can understand using Given... When Then... style. The math.story file under src/main/resources/jbehave folder
Scenario: 2 squared Given a variable input with value 2 When I multiply input by 2 Then result should equal 4 Scenario: 3 squared Given a variable input with value 3 When I multiply input by 3 Then result should equal 9
Step 3: Map the above scenarios based stories to Java equivalent.
package com.mycompany.jbehave; 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.jbehave.core.steps.Steps; public class MathSteps extends Steps { private int input; private int result; @Given("a variable input with value $value") public void givenInputValue(@Named("value") int value) { input = value; } @When("I multiply input by $value") public void whenImultiplyInputBy(@Named("value") int value) { result = input * value; } @Then("result should equal $value") public void thenInputshouldBe(@Named("value") int value) { if (value != result) throw new RuntimeException("result is " + result + ", but should be " + value); } }
Step 4: Write a main class to excute the scenarios.
package com.mycompany.jbehave; import java.util.Arrays; import java.util.List; import org.jbehave.core.embedder.Embedder; public class JBehaveTest { private static Embedder embedder = new Embedder(); private static List<String> storyPaths = Arrays .asList("jbehave/math.story"); public static void main(String[] args) { embedder.candidateSteps().add(new MathSteps()); try { embedder.runStoriesAsPaths(storyPaths); } catch (Exception e) { e.printStackTrace(); } } }
Step 5: Run it to get output:
Processing system properties {} Using controls EmbedderControls[batch=false,skip=false,generateViewAfterStories=true,ignoreFailureInStories=false,ignoreFailureInView=false,verboseFailures=false,verboseFiltering=false,storyTimeoutInSecs=300,threads=1] Running story jbehave/math.story Generating reports view to 'C:\projects\my-app-parent\my-app\target\jbehave' using formats '[]' and view properties '{defaultFormats=stats, decorateNonHtml=true, viewDirectory=view, decorated=ftl/jbehave-report-decorated.ftl, reports=ftl/jbehave-reports-with-totals.ftl, maps=ftl/jbehave-maps.ftl, navigator=ftl/jbehave-navigator.ftl, views=ftl/jbehave-views.ftl, nonDecorated=ftl/jbehave-report-non-decorated.ftl}' Reports view generated with 0 stories (of which 0 pending) containing 0 scenarios (of which 0 pending)
Now, modify the math.story file to have an error for the second scenario as shown below. 3 * 3 is not 10.
Scenario: 2 squared Given a variable input with value 2 When I multiply input by 2 Then result should equal 4 Scenario: 3 squared Given a variable input with value 3 When I multiply input by 3 Then result should equal 10
Run it again with the change, and you will get an error as shown below for the second scenario.
Processing system properties {} Using controls EmbedderControls[batch=false,skip=false,generateViewAfterStories=true,ignoreFailureInStories=false,ignoreFailureInView=false,verboseFailures=false,verboseFiltering=false,storyTimeoutInSecs=300,threads=1] Running story jbehave/math.story Generating reports view to 'C:\projects\my-app-parent\my-app\target\jbehave' using formats '[]' and view properties '{defaultFormats=stats, decorateNonHtml=true, viewDirectory=view, decorated=ftl/jbehave-report-decorated.ftl, reports=ftl/jbehave-reports-with-totals.ftl, maps=ftl/jbehave-maps.ftl, navigator=ftl/jbehave-navigator.ftl, views=ftl/jbehave-views.ftl, nonDecorated=ftl/jbehave-report-non-decorated.ftl}' Reports view generated with 0 stories (of which 0 pending) containing 0 scenarios (of which 0 pending) org.jbehave.core.embedder.Embedder$RunningStoriesFailed: Failures in running stories: jbehave/math.story: org.jbehave.core.embedder.StoryManager$StoryExecutionFailed: jbehave/math.story at org.jbehave.core.embedder.Embedder$ThrowingRunningStoriesFailed.handleFailures(Embedder.java:495) at org.jbehave.core.embedder.Embedder.handleFailures(Embedder.java:224) at org.jbehave.core.embedder.Embedder.runStoriesAsPaths(Embedder.java:205) at com.mycompany.jbehave.JBehaveTest.main(JBehaveTest.java:19)
Next post will use junit for testing the scenarios.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home