Description Usage Arguments Details Value Assumptions Examples
Wrapping test code with this function ensures that the appropriate working
directory is chosen regardless of how test code is executed (see details).
This is especially useful when test code itself is still at a development stage,
i.e. not completely fit for execution via test_dir
or
similar testing routines yet.
1 2 | withCorrectWorkingDir(code, pattern = "/tests/testthat$",
target_wd = "tests/testthat")
|
code |
Test code containing expectations |
pattern |
|
target_wd |
|
In case test code is executed via "stand-alone" calls to either
expect_that
(or functions calling that function) or
test_that
instead of a "global" call to
test_dir
, the working directory is temporarily
set to "~/tests/testthat"
.
Whatever the code
returns.
The function assumes that your initial working directory points to a
directory containing a valid R package (project) as created via
functions like package.skeleton
,
create
or the like.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | ## Not run:
# Prepare:
old <- setwd(tempdir())
dir.create("tests/testthat/test_dir", recursive = TRUE)
# Assumes that the following file exists: ~/tests/testthat/test_dir
# Failing test when executed in a "stand-alone" manner:
try(expect_true(file.exists("test_dir")))
# Ensuring the appropriate working directory:
withCorrectWorkingDir(
expect_true(file.exists("test_dir"))
)
# Clean up:
if (grepl(basename(getwd()), old)) {
unlink("tests", recursive = TRUE)
}
setwd(old)
## End(Not run)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.