Cucumber automated acceptance tests
Cucumber is a tool for running automated acceptance tests written in a behaviour-driven development (BDD) style. Cucumber was originally written in the Ruby programming language, but since the original development and its success it has been implemented in other languages as well.
How it works
It reads in your specification which is written in feature files that have a specific format named Gherkin.Examines the scenarios and runs them on the SUT.The scenarios are a list of steps for cucumber to work through .Besides features you give cucumber a set of step definitions which are in turn mapped to the scenarios written in plain language using the Gherkin.
The following image describes this relationship really well.
As you can see the features of the application are written in English almost as plain text ,and the step definitions are written in Ruby (or in the language that has been used to create the SUT ).
Gherkin
As mentioned previously gherkin is a cucumber specific format, that creates a set of rules for plain text that makes it easy to define step definitions .The main advantage for using Gherkin that it can be localized to be used in any language and because it uses plain text, even non-technical stakeholders can use it to write test specifications if the follow the following rules.
- Every Gherkin file begins with the Feature keyword
- The text following is the name of the feature and rest of text is description used for some summary or documentation
- The Scenario is a single concrete example of how the system should behave in a concrete situation
- Each scenario must make sense and has to be self-contained so there are no dependency issues between tests
- To express a desired behavior several scenarios are used
- The Given keyword is used to set up context for scenarios
- When keyword is used to describe an interaction with the system
- Then keyword check the result of an interaction
- Each line in a scenario is a step the But, And , * are used to link several steps.
An example feature file in gherkin format would look like this (the example is from the cucumber book):
The workflow of writing typical cucumber acceptance test
Every scenario should follow this basic workflow
- Get the system in the desired state
- Poke it (Make any action that triggers something)
- Examine the new state
Step definitions
Step definitions are functions and code snippets that “execute” the written features. They translate the Gherkin based plain language specification into concrete action on the SUT.
These step definition could originally only be written in Ruby in the following matter, but since it has been ported to other platforms they can be run also as JUnit and NUnit tests.
An example of an original Ruby step definition:
The “Cucumber Algorithm”
You can see below the algorithm cucumber follows when executing automated test.
Platforms
Cucumber is implemented for many different platforms (programming languages/frameworks).
The table below should help you choose the right platform.
Platform
|
Install
|
API Docs
|
Java, Groovy, Clojure,Jython, JRuby, Scala,Rhino JavaScript, Ioke
|
Cucumber-JVM
|
Cucumber-JVM
|
Ruby, JRuby
|
Cucumber-Ruby
|
Cucumber-Ruby
|
Ruby on Rails
|
Cucumber-Rails
|
Cucumber-Rails
|
Node.js, Browsers
|
Cucumber-JavaScript
| |
Lua
| ||
C++
|
Cucumber-Cpp
| |
C#, F#
|
SpecFlow
|
SpecFlow
|
Cucumber-JVM
Cucumber-jvm is the java virtual machine based cucumber support most of the widespread languages running on java virtual machine. It was created to aid those who did not want to learn Ruby ,or help maintain all of the code (the application , and test) in the same language.
It consist of several modules (jar) that can be easily obtained from the cucumber site.
You will always need the cucumber-core module, which contains the main logic for parsing and executing your Gherkin feature files.
Installation
Installation can be done in several ways:
- Downloading and adding jars
- Using ANT
- Using Maven
(Further on i will show only Maven as it is more commonly used and simple to add to existing maven projects)
You only need to add the following main dependencies
<dependencies>
<dependency> <groupId>info.cukes</groupId> <artifactId>cucumber-picocontainer</artifactId> <version>1.1.3</version> <scope>test</scope> </dependency> <dependency> <groupId>info.cukes</groupId> <artifactId>cucumber-junit</artifactId> <version>1.1.3</version> <scope>test</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> </dependencies> |
The cucumber maven project structure
Be sure to follow the following structure because cucumber-jvm relies on this structure for accessing the feature and step definition files, and otherwise error can easily occur.
Start cucumber class
This class does not have to contain anything else, it only serves as an initializer and should not be used as a hook for the test cases.
package com.mycompany.lehel;
import cucumber.api.junit.Cucumber;
import org.junit.runner.RunWith;
@RunWith(Cucumber.class)
public class StartCucumber { } |
Feature files
Feature files are the same as previously presented at the Gherkin chapter.
Java uses them the same way as they were parsed in the original Ruby version.
Eg.
Scenario: Some cukes
Given I have 48 cukes in my belly |
Step definitions
Step definitions are written in Java and JUnit is used to run the steps. A simple example for the previously shown feature file.
@Given("I have (\\d+) cukes in my belly")public void I_have_cukes_in_my_belly(int cukes) {
// Do something with the cukes } |
References
1. The Cucumber Book: Behaviour-Driven Development for Testers and Developers by Matt Wynne and Aslak Hellesøy
2. Cucumber Recipes: Automate Anything with BDD Tools and Techniques by Ian Dees, Matt Wynne, Aslak Hellesoy
3. Aslakhellesoy.com. Retrieved 2013-05-24.
Hello,
ReplyDeleteThe Article on Cucumber automated acceptance tests, gives amazing information about it. Thanks for Sharing the information about the features of the automated acceptance tests, For More information check the detail on the User Acceptance Testing here Software Testing Company
Excellent article. Very interesting to read. I really love to read such a nice article. Thanks! keep rocking. Ruby on Rails Online Training
ReplyDelete