Cucumber Test Framework – Cucumber-JVM with Cucumber-Java + Cucumber-Junit

Cucumber is a tool for running automated acceptance tests written in a behavior-driven development (BDD) style. Cucumber is written in the Ruby programming language. Cucumber projects are available for other platforms beyond Ruby. Some use Ruby Cucumber with a bridge into the target language (e.g. cuke4php and cuke4lua). Others use the Gherkin parser but implement everything else in the target language. Cucumber allows the execution of feature documentation written in business-facing text. – http://en.wikipedia.org/wiki/Cucumber_(software)

Cucumber works with Ruby, Java, .NET, Flex or web applications written in any language. It has been translated to over 40 spoken languages. – http://cukes.info/

The language that Cucumber understands is called Gherkin. Here is an example: – https://github.com/cucumber/cucumber/wiki

 

While Cucumber can be thought of as a “testing” tool, the intent of the tool is to support BDD. This means that the “tests” (plain text feature descriptions with scenarios) are typically written before anything else and verified by business analysts, domain experts, etc. non technical stakeholders. The production code is then written outside-in, to make the stories pass.

Cucumber itself is written in Ruby, but it can be used to “test” code written in Ruby or other languages including but not limited to Java, C# and Python.

I will show an example how cucumber-jvm fits with cucumber-java and cucumber-junit

First create a blank maven project in Eclipse. You put values for Group Id, Artifact Id, Packaging, Name and Description. For example,

 

Click “Finish”.

Now open the pom.xml file and add the following dependencies for cucumber and junit

 

A standard Maven project directory structure will have a source folder src/test/resources, this is the directory where you should create your .feature files. So let’s create a feature for adding two numbers.

Create a file called add.feature in src/test/resources. You can of course create packages in this source folder to organize your feature files. I have put the feature file under in.webtuts.cucumber package. So the full path for the feature file is src/test/resources/in/webtuts/cucumber/add.feature.

Now let’s write our first simple scenario. We want to test that when two numbers are added together we will get the expected result.

src/test/resources/in/webtuts/cucumber/add.feature

 

The above first scenario describes the steps that will have to be executed successfully for the Scenario to pass.

We will run the .feature using a JUnit Runner, which we are having from the Cucumber-Junit Library. So all we need to do is to create a blank test class, and annotate it with JUnit’s @RunWith annotation as follows:

 

You can now run TestAdd.java like you would any other JUnit test.

The console output:

 

Take a look at the JUnit Tab in your Eclipse IDE, the scenario steps were actually skipped, they were not executed because no step definitions were found. You’ll see some very helpful information about the scenario that was executed and the steps which were skipped.

The output also has shown you default implementations for the step definitions, how nice is that!

Let’s create a class for the step definitions.

 

At this point we are ready to implement the step definitions. Let’s start with the steps.

 

Now run AddTest.java and you will see that the test steps have been passed.

The AddStepDefinition class can be refactored because num1,num2 etc. variables should not be decraled here and it’s just for testing purpose.

Thanks for your reading. Please leave a comment if you have any query.

Soumitra

Software Professional, I am passionate to work on web/enterprise application. For more information please go to about me. You can follow on Twitter. You can be a friend on Facebook or Google Plus or Linkedin

Leave a Reply

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

Time limit is exhausted. Please reload CAPTCHA.