Cucumber
What is Cucumber?
Cucumber is a framework for writing and executing high level descriptions of your software's functionality. Specifications are written in a concise human readable form. You can read more about Cucumber here
Where do I put the tests?
Cucumber has the following concepts:
What is a Feature?
A feature file is supposed to describe a single feature of the system, or a particular aspect of a feature. Contains an overall description of a feature as well as a number of scenarios.
What is a Scenario?
A scenario is a concrete example that illustrates a business rule. Scenarios are stored in Feature Files. Every scenario consists of a list of steps, which must start with one of the keywords Given, When, Then or And. A feature may contain a background, scenario outline and examples.
The pattern to write a scenario is:
- Describe an initial context
- Describe an action
- Describe an expected outcome
Feature: Search results in Google
Scenario: Search something in google
Given I'm at http://www.google.com/ncr
When I search for minium github
Then a link for https://github.com/viltgroup/minium is displayed
What is a Step?
A step is analogous to a method or function invocation. A step starts with the keywords Given, When or Then.
Given
- The purpose of givens is to put the system in a known state before the user (or external system) starts interacting with the system
Example:
- Create database records
- Log in a user
When
The purpose of When steps is to describe the key action the user performs.
Example:
- Interact with a web page, clicking in a button or fill a form.
Then
Then steps are used to describe an expected outcome, or result.
Example:
- Assertion to compare the actual outcome to the expected outcome.
What is a Step definition?
Every step need a step definition to translate plain text Gherkin steps into actions that will interact with the system. A step definition consists of a keyword, a string or regular expression, and a block
When Cucumber executes a Step in a Scenario it will look for a matching Step Definition to execute.
Scenario: Search something in google
When I search for minium github
For the step When I search for minium github
the step definition is:
When(/^I search for (.*)$/, function (query) {
var searchbox = $(":input").withName("q");
var button = $("button").withAttr("aria-label", "Google Search");
searchbox.fill(query);
button.click();
});
Step definitions are defined in javascript files under steps/*_steps.js.
Which results can a Scenario have?
A scenario has 2 possible result:
- Passed - if every steps passed
- Failed - if one or more step was marked as failed/undefined/skipped
Which results can a steps have?
It can have 4 possible result:
- Passed - means that the everything run well
- Failed - means that the step has some problem.
- Undefined - When Cucumber can’t find a matching Step Definition the step gets marked as undefined, and all subsequent steps in the scenario are skipped.
- Skipped - Steps that follow undefined, pending or failed steps are never executed (even if there is a matching Step Definition).