An implementation of the Cucumber testing framework in R.
IntroductionThe package parses Gherkin documents
# tests/acceptance/addition.feature
Feature: Addition
Scenario: Adding 2 integers
When I add 1 and 1
Then the result is 2
Scenario: Adding integer and float
When I add 1 and 1.1
Then the result is 2.1
Scenario: Adding float and float
When I add 1.1 and 1.1
Then the result is 2.2
Scenario: Adding float and float with signs
When I add +11.1 and +11.1
Then the result is +22.2
Scenario: Adding float and float of opposite signs
When I add +11.11 and -11.1
Then the result is +0.01
and uses step definitions to run the tests
# tests/acceptance/setups-steps.R
when("I add {int} and {int}", function(x, y, context) {
context$result <- x + y
})
then("the result is {int}", function(expected, context) {
expect_equal(context$result, expected)
})
when("I add {int} and {float}", function(x, y, context) {
context$result <- x + y
})
when("I add {float} and {float}", function(x, y, context) {
context$result <- x + y
})
then("the result is {float}", function(expected, context) {
expect_equal(context$result, expected)
})
The building blocks of the cucumber tests are Features and Scenarios.
test-*.R
files, e.g. 'test-Feature: Addition.R'
.testthat::test_that
or testthat::it
case. You get feedback on each Scenario separately. Only if all steps in a scenario are successful, the scenario is considered successful.That means a successful run for an Addition
feature would produce the following output (with ProgressReporter).
â | F W S OK | Context
â | 5 | Feature: Addition
ââ Results âââââââââââââââââââââââââââââââââââââââââââ
[ FAIL 0 | WARN 0 | SKIP 0 | PASS 5 ]
and a failing one would produce:
â | F W S OK | Context
â | 1 4 | Feature: Addition
ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ
Failure (test-__cucumber__.R:2:1): Scenario: Adding float and float of opposite signs
context$result (`actual`) not equal to `expected` (`expected`).
`actual`: 0.0100
`expected`: 0.0010
Backtrace:
â
1. ââcucumber (local) call() at cucumber/R/parse_token.R:23:13
2. ââcucumber (local) x(context = context, ...)
3. ââstep(expected = 0.001, ...)
4. ââtestthat::expect_equal(context$result, expected) at tests/acceptance/setup-steps-addition.R:19:3
ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ
ââ Results âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ
ââ Failed tests ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ
Failure (test-__cucumber__.R:2:1): Scenario: Adding float and float of opposite signs
context$result (`actual`) not equal to `expected` (`expected`).
`actual`: 0.0100
`expected`: 0.0010
Backtrace:
â
1. ââcucumber (local) call() at cucumber/R/parse_token.R:23:13
2. ââcucumber (local) x(context = context, ...)
3. ââstep(expected = 0.001, ...)
4. ââtestthat::expect_equal(context$result, expected) at tests/acceptance/setup-steps-addition.R:19:3
[ FAIL 1 | WARN 0 | SKIP 0 | PASS 4 ]
Put your acceptance tests in a directory separate to your unit tests:
tests/
âââ acceptance/
â âââ setup-steps_1.R
â âââ setup-steps_2.R
â âââ feature_1.feature
â âââ feature_2.feature
âââ testthat/
â âââ test-unit_test_1.R
â âââ test-unit_test_2.R
or alongside your unit tests:
tests/
âââ testthat/
â âââ test-cucumber.R
â âââ test-unit_test_1.R
â âââ test-unit_test_2.R
â âââ setup-steps_1.R
â âââ setup-steps_2.R
â âââ feature_1.feature
â âââ feature_2.feature
Examples
See the examples directory to help you get started.
How it worksThe .feature
files are parsed and matched against step definitions.
Step functions are defined using:
description
: a cucumber expression.context
parameter - an environment for managing state between steps.If a step parsed from one of .feature
files is not found, an error will be thrown.
Step implementations receive data from the .feature
files as parameters. The values are detected via regular expressions and cast with a transformer function.
The following parameter types are available by default:
{int}
Matches integers, for example 71
or -19
. Converts value with as.integer
. {float}
Matches floats, for example 3.6
, .8
or -9.2
. Converts value with as.double
. {word}
Matches words without whitespace, for example banana (but not banana split). {string}
Matches single-quoted or double-quoted strings, for example âbanana splitâ or âbanana splitâ (but not banana split). Only the text between the quotes will be extracted. The quotes themselves are discarded.
See cucumber::define_parameter_type()
how to define your own parameter types.
"""
(Doc Strings)|
(Data Tables)@
(Tags)#
(Comments)To install the stable version from CRAN:
install.packages("cucumber")
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4