Cucumber.js
demo
Find more on the
Cucumber.js repository
.
Feature source
Feature: Simple maths In order to do maths As a developer I want to increment variables Scenario: easy maths Given a variable set to 1 When I increment the variable by 1 Then the variable should contain 2 Scenario Outline: much more complex stuff Given a variable set to
When I increment the variable by
Then the variable should contain
Examples: | var | increment | result | | 100 | 5 | 105 | | 99 | 1234 | 1333 | | 12 | 5 | 18 |
Step definitions
///// Your World ///// // Provide a custom World constructor. It's optional, a default one is supplied. this.World = function(callback) { callback(); }; // Define your World! this.World.prototype.variable = 0; this.World.prototype.setTo = function(number) { this.variable = parseInt(number); }; this.World.prototype.incrementBy = function(number) { this.variable += parseInt(number); }; ///// Your step definitions ///// // use this.Given(), this.When() and this.Then() to declare step definitions // Alternatively you can use this.defineStep and even use it // like this: var Given = When = Then = this.defineStep; Given(/^a variable set to (\d+)$/, function(number, callback) { this.setTo(number); callback(); }); When(/^I increment the variable by (\d+)$/, function(number, callback) { this.incrementBy(number); callback(); }); Then(/^the variable should contain (\d+)$/, function(number, callback) { if (this.variable != parseInt(number)) callback.fail(new Error('Variable should contain ' + number + ' but it contains ' + this.variable + '.')); else callback(); });
Output
Run feature
Errors