A global test data set is used to avoid having to re-specify the testing data frame in every test. These functions get and set the global data or set the data for the current context.
set_testdata(data, quosure = TRUE)
get_testdata()
with_testdata(data, code, quosure = TRUE)
data %E>% code
Data frame to be used.
If TRUE
, the default, the data frame is stored as a
quosure and lazily evaluated when get_testdata()
is
called, so get_testdata()
will return the current state of the data
frame.
If FALSE
, the data frame will be copied and get_testdata()
will return
the state of the data frame at the time set_testdata()
was called.
Code to execute with the test data set to data
.
set_testdata()
invisibly returns the previous test data. The test data
is returned as it was stored - if it was stored with quosure = TRUE
it
will be returned as a quosure.
get_testdata()
returns the current test data frame.
with_testdata()
and the test data pipe %E>%
invisibly return the
input data
for easy piping.
set_testdata(mtcars)
head(get_testdata())
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
#> Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
#> Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
#> Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
#> Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
#> Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
with_testdata(iris, {
x <- get_testdata()
print(head(x))
})
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> 1 5.1 3.5 1.4 0.2 setosa
#> 2 4.9 3.0 1.4 0.2 setosa
#> 3 4.7 3.2 1.3 0.2 setosa
#> 4 4.6 3.1 1.5 0.2 setosa
#> 5 5.0 3.6 1.4 0.2 setosa
#> 6 5.4 3.9 1.7 0.4 setosa
mtcars %E>%
expect_base(mpg, TRUE) %E>%
expect_range(carb, 1, 8)