Site icon TestingDocs.com

Examples Table: Repeating a Scenario

Introduction

In this post, we will discuss the Examples table in JBehave story. We can use it to run repeating alike scenarios. Also, we can data drive the scenario and repeat it as many times as the number of data rows in the examples table.

Repeating a Scenario

We will see a simple scenario of adding 2 numbers and repeat the scenario multiple times. Also, this can be achieved by injecting parameters to the java method with @Named annotation. The java method will execute the number of times as the number of rows in the Examples table. Furthermore, the example is a simplification of stated:  Annotated @Named Parameters Example

Examples tables require parameters for the textual step to be matched to step methods.

Story gherkin

Example Tables Story 

Narrative:
Checking simple ADD functions in this story

Scenario: ADD numbers 
 
Given numbers to add i <i> and j <j>
When we add them
Then verify the sum


Examples:     
|i|j|
|7|9|
|10|15|
|100|250|


We can see that i and j are the parameters specified in the table.
The number of rows are three. So , the scenario run for three times with different parameter values specified in the table rows.

Tip: to navigate to the corresponding matching java method ( control + click )  on the textual step in the story file.

Method

@Given("numbers to add i $i and j $j")
public void givenNumbers(@Named("i")int i,@Named("j")int  j )
{
  // step logic

 .......
    
}

 

Run output

We run the story we may see that the scenario executes three times each time with the row data specified in the table.

 

Example: {i=7, j=9}
Given numbers to add i 7 and j 9
When we add them
Then verify the sum
Using timeout for story ExamplesTable.story of 300 secs.

Example: {i=10, j=15}
Given numbers to add i 10 and j 15
When we add them
Then verify the sum

Example: {i=100, j=250}
Given numbers to add i 100 and j 250
When we add them
Then verify the sum

 

This feature can be used for data-driven testing a story or scenario multiple times.

 

JBehave Tutorials on this website can be found at:
https://www.testingdocs.com/jbehave-framework-tutorial/

For more details on the JBehave framework, visit the official JBehave website at:
http://jbehave.org

Exit mobile version