Installing Cucumber Tutorial

Your a nobody if you don’t know how to run Cucumber tests and in this blog we will look at installing Cucumber. We will install Cucumber on a Windows operating system however the installation should be very similar across all operating system. To simplify this installation tutorial as much as possible we will use nothing but the command line and download a couple of Jars. This will also help us understand the process a little better whilst also keeping us away from any IDE noise.

01 – Installing Cucumber: Directory Setup

To keep this tutorial as simple as possible we will run our tests from the command line. Before doing any of this and to confirm our ‘installing Cucumber’ setup lets go ahead and setup our project directory first.

First lets create the actual directory in which all our code, jars, code etc will be saved:

mkdir cucumber_project

Now lets go into the folder and create another folder which will contain all the jars:

cd cucumber_project
mkdir jars

We now need to download all the relevant jars and place them into the jars folder. Go to http://search.maven.org/ and download the latest versions of the relevant jars (note that the URL’s below for each jar is the latest version of that jar at the point of writing this blog):

cucumber-core - http://search.maven.org/#artifactdetails|info.cukes|cucumber-core|1.2.4|jar
cucumber-java - http://search.maven.org/#artifactdetails|info.cukes|cucumber-java|1.2.4|jar
cucumber-jvm-deps - http://search.maven.org/#artifactdetails|info.cukes|cucumber-jvm-deps|1.0.5|jar
gherkin - http://search.maven.org/#artifactdetails|info.cukes|gherkin|2.12.2|bundle

With all the relevant jars downloaded we can try to run Cucumber main method and see what happens.

02 – Our First Run

Now lets see what happens when we try to run Cucumber. Firstly lets ensure we are in the correct directory i.e. in our project directory:

cd cucumber_project

And lets run the following command:

java -cp "jars/*" cucumber.api.cli.Main -p pretty .

You should see something very similar to the outcome below:

No features found at [.]

0 Scenarios
0 Steps
0m0.000s

So what is actually happening here? Firstly we are setting the jars we downloaded to the class path using -cp. Next we invoke Cucumber which tries to run our tests but in this instance there are no results for two reasons. Firstly we did not actually write any Feature files and second we told cucumber that our Feature files are in the current directory (as indicated by the ‘.’ at the end of the command). Finally we are setting the format of the returned results to be Pretty (we will discuss more on this ‘format’ point a little later in another blog. The output above confirms that Cucumber tried to run some Features files but was unable because it could not locate any Feature files to run. This is great news as we have now established a setup where Cucumber will run. This very nicely brings us to the next crucial point i.e. writing our first Feature file.

03 – Our First Feature File

It’s time to finally write our first Feature file. Lets create another directory where we can write and store our Feature files:

cd cucumber_project
mkdir features
cd features
touch myFirstFeature.feature

We have created a new directory to conveniently store our Feature files, lets write out first Feature file. Let’s use VIM to open and edit our Feature file:

vi myFirstFeature.feature

Once the file is opened we will add in the following lines:

Feature: Check TheTestRoom Home Page
Scenario: To confirm TheTestRoom loads correctly
Given I navigate to TheTestRoom
Then the website logo should be visible

cd viSo what is a Feature file and what are we trying to do. The Feature file is divided into multiple sections. The first is the ‘Feature’ section. The purpose of this section is to describe what your Feature file is trying to test. This is supposed to address a very high level test area. A Feature file should ideally contain only one ‘Feature’ section.

The second is the ‘Scenario’ section. This is used to describe the actual test case which you are trying to test. A Feature file may contain multiple Scenarios.

Lastly we have the actual test identified by the ‘Given’ and ‘Then’ keywords. This is used to describe the actual test and are known as test steps. Each test step is used to talk to the actual code. A collection of test steps is known as Gherkin language.

04 – Running our first Feature File

Now lets run the same command line again but this time with the exception being that we tell Cucumber where our Feature files are located:

java -cp "jars/*" cucumber.api.cli.Main -p pretty features

When doing so you should now see an output very similar to the below:

Feature: Check TheTestRoom Home Page

  Scenario: To confirm TheTestRoom loads correctly # myFirstFeature.feature:2
    Given I navigate to TheTestRoom
    Then the website logo should be visible

1 Scenarios (1 undefined)
2 Steps (2 undefined)
0m0.000s

You can implement missing steps with the snippets below:

@Given("^I navigate to TheTestRoom$")
public void i_navigate_to_TheTestRoom() throws Throwable {
    // Write code here that turns the phrase above into concrete actions
    throw new PendingException();
}

@Then("^the website logo should be visible$")
public void the_website_logo_should_be_visible() throws Throwable {
    // Write code here that turns the phrase above into concrete actions
    throw new PendingException();
}

If your output is similar to the above then give yourself a pat on the back as you have now setup Cucumber to read and run Feature files. So what is happening in the output above. The output above confirms that Cucumber was able to run and identify test steps in our Feature file but failed to match up any code for each test step (more on this in a future blog). This is naturally obvious as we have not written any code to match up to any test steps in our Feature file. This however confirms our first initial setup for installing Cucumber has worked.

And that’t it. We have now proved that Cucumber in it’s simplest form is working. Installing Cucumber is as easy as that when approaching from the command line interface. Our next step will be to go ahead and write some very simple Java code and get Cucumber to ‘glue’ each test step in our Feature file to our Java class.

Video tutorial for Installing Cucumber

installing cucumber

Direct Video Link

Mo

I am a passionate tester, father, husband and gamer. I love to upload videos on my YouTube channel, write blogs about Software Testing and generally contribute back to the Software Testing world.

More Posts - Twitter - Facebook - Google Plus

Published by

Mo

I am a passionate tester, father, husband and gamer. I love to upload videos on my YouTube channel, write blogs about Software Testing and generally contribute back to the Software Testing world.

Leave a Reply