Title: | Behavior-Driven Development for R |
---|---|
Description: | Write executable specifications in a natural language that describes how your code should behave. Write specifications in feature files using 'Gherkin' language and execute them using functions implemented in R. Use them as an extension to your 'testthat' tests to provide a high level description of how your code works. |
Authors: | Jakub Sobolewski [aut, cre] |
Maintainer: | Jakub Sobolewski <[email protected]> |
License: | MIT + file LICENSE |
Version: | 1.0.4 |
Built: | 2024-09-28 07:18:03 UTC |
Source: | CRAN |
The following parameter types are available by default:
Type | Description |
{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. |
To use custom parameter types, call define_parameter_type
before cucumber::test
is called.
define_parameter_type(name, regexp, transformer)
define_parameter_type(name, regexp, transformer)
name |
The name of the parameter. |
regexp |
A regular expression that the parameter will match on. Note that if you want to escape a special character, you need to use four backslashes. |
transformer |
A function that will transform the parameter from a string to the desired type. Must be a function that requires only a single argument. |
An object of class parameter
, invisibly. Function should be called for side effects.
define_parameter_type("color", "red|blue|green", as.character) define_parameter_type( name = "sci_number", regexp = "[+-]?\\\\d*\\\\.?\\\\d+(e[+-]?\\\\d+)?", transform = as.double ) ## Not run: #' tests/testthat/test-cucumber.R cucumber::define_parameter_type("color", "red|blue|green", as.character) cucumber::test(".", "./steps") ## End(Not run)
define_parameter_type("color", "red|blue|green", as.character) define_parameter_type( name = "sci_number", regexp = "[+-]?\\\\d*\\\\.?\\\\d+(e[+-]?\\\\d+)?", transform = as.double ) ## Not run: #' tests/testthat/test-cucumber.R cucumber::define_parameter_type("color", "red|blue|green", as.character) cucumber::test(".", "./steps") ## End(Not run)
Provide a description that matches steps in feature files and the implementation function that will be run.
given(description, implementation) when(description, implementation) then(description, implementation)
given(description, implementation) when(description, implementation) then(description, implementation)
description |
A description of the step. A simple version of a Cucumber expression.
The description is used by the |
implementation |
A function that will be run when the step is executed.
The implementation function should always have the last parameter named If a step has a description If a table or a docstring is defined for a step, it will be passed as an argument after plceholder parameters
and before |
Placeholders in expressions are replaced with regular expressions that match values in the feature file.
The regular expressions are generated during runtime based on defined parameter types.
The expression "I have {int} cucumbers in my basket"
will be converted to
"I have [+-]?(?<![.])[:digit:]+(?![.]) cucumbers in my basket"
. The extracted value of {int}
will be passed to the implementation function after being transformed with as.integer
.
To define your own parameter types use define_parameter_type
.
A function of class step
, invisibly. Function should be called for side effects.
given("I have {int} cucumbers in my basket", function(n_cucumbers, context) { context$n_cucumbers <- n_cucumbers }) given("I have {int} cucumbers in my basket and a table", function(n_cucumbers, table, context) { context$n_cucumbers <- n_cucumbers context$table <- table }) when("I eat {int} cucumbers", function(n_cucumbers, context) { context$n_cucumbers <- context$n_cucumbers - n_cucumbers }) then("I should have {int} cucumbers in my basket", function(n_cucumbers, context) { expect_equal(context$n_cucumbers, n_cucumbers) })
given("I have {int} cucumbers in my basket", function(n_cucumbers, context) { context$n_cucumbers <- n_cucumbers }) given("I have {int} cucumbers in my basket and a table", function(n_cucumbers, table, context) { context$n_cucumbers <- n_cucumbers context$table <- table }) when("I eat {int} cucumbers", function(n_cucumbers, context) { context$n_cucumbers <- context$n_cucumbers - n_cucumbers }) then("I should have {int} cucumbers in my basket", function(n_cucumbers, context) { expect_equal(context$n_cucumbers, n_cucumbers) })
This command runs all Cucumber tests. It takes all .feature files
from the features_dir
and runs them using the steps from the steps_dir
.
test( features_dir, steps_dir, steps_loader = .default_steps_loader, test_interactive = getOption("cucumber.interactive", default = FALSE) )
test( features_dir, steps_dir, steps_loader = .default_steps_loader, test_interactive = getOption("cucumber.interactive", default = FALSE) )
features_dir |
A character string of the directory containing the feature files. |
steps_dir |
A character string of the directory containing the step files. |
steps_loader |
A function that loads the steps implementations. By default it sources all files
from the |
test_interactive |
A logical value indicating whether to ask which feature files to run. |
None, function called for side effects.