Create runner file in Cucumber BDD

Tutorial 4​​ ​​ ​​ Create​​ runner​​ file​​ in Cucumber BDD

What you will Learn :

  • Create​​ runner​​ file

  • Execute runner file​​ as junit test

  • Print the pretty output

  • Monochrome option

  • Execute​​ runner file as​​ maven test

Create​​ runner​​ file

So​​ far, we have been executing​​ our feature​​ file​​ by right clicking the feature file >Run As > Cucumber Feature​​ ​​ 

Now,​​ if​​ we have multiple​​ feature files,​​ we cannot execute our feature files​​ using​​ the above method.​​ It will be quite tedious.​​ A better aproach would be to create a runner file.

Also, recall​​ cucumber design​​ flow steps. We create​​ TestRunner.java​​ file​​ to execute our step def file

See below.​​ Create​​ a new package ‘TestRunner’ under src/test/java. Under this package, create a java class ‘TestRunner’​​ 

Now we will add 2 annotations to this class: @RunWith and @CucumberOptions

Let us first add @RunWith(Cucumber.class)

To resolve​​ error, import the classes [Ctrl + Shift +​​ O]

Let us now add the second annotation

To resolve error, import the classes [Ctrl + Shift + O]

We will now add some options.​​ Now, how will the TestRunner file know the location of feature file?​​ The first​​ option​​ does that. We use the ‘features’ keyword

To find the location of feature file, right click the feature file > properties

The below window comes up.Just copy the path starting from src/test/java/Features

Paste it

Note that, we copied the path only upto ‘Features’ package. We could have also​​ copied the path upto the exact feature filename viz​​ src/test/java/Features/Search.feature
However, tomorrow if there are multiple feature files inside ‘Features’ package​​ and we want to execute all of them, than​​ it would be​​ better​​ to mention the path ony uptill ‘Features’ package.

Next, using second option, we will tell the TestRunner where exactly the step definition file is available. This is done using ‘glue’ keyword

Just​​ mention the name of the ‘StepDefinitions’​​ package folder. The TestRunner file will come to know that all the step definition files are present inside this package

Save the TestRunner file.

Execute runner file​​ as junit test​​ 

Right click TestRunner.java > Run As > Junit Test

See the console o/p below. All 3 steps are executed fine

You can also see the Junit plugin o/p. It tells us that the ‘Search course’ scenario got executed fine

Recall that the name of our scenario is ‘Search course’

Print the pretty output

Notice that in the​​ console output that we got above,​​ we do not see the respective Give/When/Then steps.​​ 

We can see the respective Give/When/Then steps in the console o/p by​​ printing​​ the pretty output. This is done​​ by adding one more cucumber option. There is a ‘plugin’ keyword that we can use, see below

Save the file.

Execute the TestRunner

Notice​​ that the o/p is still not pretty. We do see the Given/When/Then steps but not easily readbale, see below

Monochrome option

To make the console o/p further readbale, add another option, see below

Save the file

Execute the TestRunner

Now, the console o/p shows the readable Give/When/Then steps ​​ 

Execute​​ runner file as​​ maven test

Let us​​ now see how to run the same runner file as a maven test.​​ 

Before we run​​ the maven test, we have to first rename our runner file​​ with​​ ‘Test’ is prefix​​ (otherwise you will not see the Given/When/Then steps in output)

Let us fix the error by renaming the class file

So we have

Right click project > Run As > Maven test

Below is console o/p. Notice that it the​​ ‘Build’ got​​ successfull​​ and it mentions the Given/When/Then steps



This is how we design and runner the runner file.

So so far, we have seen how to​​ design a simple cucumber feature file, step definition file and a runner file.

Thank you​​ for reading!

Share On

Share on facebook
Share on twitter
Share on linkedin
Share on whatsapp
Share on tumblr
Share on email

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top

Lifetime Membership Club

LIFETIME MEMBERSHIP BIG SALE - ALL LIVE COURES JUST - 7000 RS/ 99 USD
Attend All Live courses in just 7000 rs / $99 - offer ends 10th Nov 2024