Skip to contents

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)

Bundling

Ideally, pickler’s functions are developed independently and then wrapped in a bundle() before passing to the testthat BDD functions. Consider the feature() and scenario() below for the split_cols() function:

bundle(
  feature(
    title = "Split a single column into multiple columns using a pattern",
    as_a = "user of split_cols()",
    i_want = "to specify a column and a pattern",
    so_that = "I can quickly generate multiple columns."
  ),
  background(
    title = "Input dataframe with text data", 
    given = "a dataframe [data] with text columns", 
    and = list(
               "a column prefix [cols_]", 
               "a specified column [col]"))
)

Bundling makes it easier to combine features and backgrounds into a single call:

Feature: Split a single column into multiple columns using a pattern
  As a user of split_cols()
  I want to specify a column and a pattern
  So that I can quickly generate multiple columns.
Background: Input dataframe with text data
  Given a dataframe [data] with text columns
  And a column prefix [cols_]
  And a specified column [col]
    

bundle() also allows us to pass a feature() and background() to describe(), then test a particular scenario() with it():

describe(
  # bundle feature and background
  bundle(
  feature(
    title = "Split a single column into multiple columns using a pattern",
    as_a = "user of split_cols()",
    i_want = "to specify a column and a pattern",
    so_that = "I can quickly generate multiple columns."
  ),
  background(
    title = "Input dataframe with text data", 
    given = "a dataframe [data] with text columns", 
    and = list(
               "a column prefix [cols_]", 
               "a specified column [col]"))
), code = {
    it(
    # test scenario 1
     scenario(
        title = "Split column with a default pattern",
        given = "a dataframe [data] with a specified column [col]",
        when = "I split the [col] column using the default [[^[:alnum:]]+]",
        then = "the [col] column should be split into multiple columns"
      ), code = {
      
      expect_true(TRUE)
      
    })
    it(
    # test scenario 2
     scenario(
        title = "Split column with a default pattern",
        given = "a dataframe [data] with a specified column [col]",
        when = "I split the [col] column using the default [[^[:alnum:]]+]",
        then = "the new columns should be named with the default prefix [cols_]"
      ), code = {
      
      expect_true(TRUE)
      
    })
  })
#> Test passed 
#> Test passed