Make sure you have the latest version of testthat
installed.
if (!require(pak)) {
install.packages("pak",
repos = "http://cran.us.r-project.org"
)
}
pak::pkg_install("r-lib/testthat", upgrade = TRUE, ask = FALSE)
library(testthat)Inputs
It’s also possible to provide a table in a scenario or feature. Let’s
assume we have a small input that illustrates an example
behavior for our pivot_string_long() function:
input
#> input
#> "one-two-three"We can include input in our bundle with the
feature() and scenario():
bundle(
feature(
title = "Pivot String Long",
as_a = "user of pivot_string_long()",
i_want = "to specify a text column and a pattern",
so_that = "peform computations on the unique items."
),
background(
title = "Input text data",
given = "a string or vector [string] with text columns",
and = "a specified pattern [sep]"),
input = c("one-two-three")
)Feature: Pivot String Long
As a user of pivot_string_long()
I want to specify a text column and a pattern
So that peform computations on the unique items.
Background: Input text data
Given a string or vector [string] with text columns
And a specified pattern [sep]
one-two-three
Outputs
If we know we know what we the output table should look
like, we can include it with the scenario: We can convert
output to a Gherkin-style table using
with_table():
with_table(output)|unique_items |string |
|-------------|--------------|
|one |one-two-three |
|two |NA |
|three |NA |
with_table() creates a slightly modified
knitr::kable(format = "pipe", align = "l") table we can
include in a bundle() with the background and
feature:
bundle(
scenario(
title = "Split a single string with default separator",
given = "a character [string] 'one-two-three'",
when = "I pass the [string] to pivot_string_long()",
then = "Then [unique_items] column should contain rows: 'one, two, three'",
and = "[string] column should contain the original 'one-two-three'"
),
output = c("
|unique_items |string |
|-------------|--------------|
|one |one-two-three |
|two |NA |
|three |NA |
")
)This makes it easier to view them in RMarkdown or Quarto files (with
echo set to FALSE and comment set
to "")
Now our background, feature,
scenario (and tables) are in the Gherkin-style format that
can be placed in a R Markdown file and nicely rendered for stakeholders
or non-technical audiences:
Feature: Pivot String Long
As a user of pivot_string_long()
I want to specify a text column and a pattern
So that peform computations on the unique items.
Background: Input text data
Given a string or vector [string] with text columns
And a specified pattern [sep]
one-two-three
Scenario: Split a single string with default separator
Given a character [string] 'one-two-three'
When I pass the [string] to pivot_string_long()
Then Then [unique_items] column should contain rows: 'one, two, three'
And [string] column should contain the original 'one-two-three'
|unique_items |string |
|-------------|--------------|
|one |one-two-three |
|two |NA |
|three |NA |
Now our test file contains a fully scoped test with everything documented:
describe(
bundle(
feature(
title = "Pivot String Long",
as_a = "user of pivot_string_long()",
i_want = "to specify a text column and a pattern",
so_that = "peform computations on the unique items."
),
background(
title = "Input text data",
given = "a string or vector [string] with text columns",
and = "a specified pattern [sep]"),
input = c("one-two-three")), code = {
it(
bundle(
scenario(
title = "Split a single string with default separator",
given = "a character [string] 'one-two-three'",
when = "I pass the [string] to pivot_string_long()",
then = "Then [unique_items] column should contain rows: 'one, two, three'",
and = "[string] column should contain the original 'one-two-three'"
),
output = c("
|unique_items |string |
|-------------|--------------|
|one |one-two-three |
|two |NA |
|three |NA |")
), code = {
input <- c("one-two-three")
# create observed output
observed <- pivot_string_long(input)
# compare against output
output <- data.frame(
unique_items = c("one", "two", "three"),
string = c("one-two-three", NA, NA))
expect_equal(object = observed, expected = output)
})
})
#> Test passedAnd…
devtools:::test_active_file()it passes!
[ FAIL 0 | WARN 0 | SKIP 0 | PASS 1 ]