Skip to content

Commit

Permalink
Support developer tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
jeroen committed Sep 10, 2024
1 parent fe38519 commit 5977ea6
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 2 deletions.
4 changes: 4 additions & 0 deletions R/test-files.R
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,10 @@ local_teardown_env <- function(frame = parent.frame()) {
#' @export
find_test_scripts <- function(path, filter = NULL, invert = FALSE, ..., full.names = TRUE, start_first = NULL) {
files <- dir(path, "^test.*\\.[rR]$", full.names = full.names)
if (env_var_is_true("CI") || env_var_is_true("NOT_CRAN")) {
devfiles <- dir(path, "^dev-test.*\\.[rR]$", full.names = full.names)
files <- c(files, devfiles)
}
files <- filter_test_scripts(files, filter, invert, ...)
order_test_scripts(files, start_first)
}
Expand Down
9 changes: 8 additions & 1 deletion tests/testthat/test-test-files.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,23 @@ test_that("stops on failure", {
})

test_that("runs all tests and records output", {
withr::local_envvar(TESTTHAT_PARALLEL = "FALSE")
withr::local_envvar(TESTTHAT_PARALLEL = "FALSE", NOT_CRAN = "false", CI = "false")
res <- test_dir(test_path("test_dir"), reporter = "silent", stop_on_failure = FALSE)
df <- as.data.frame(res)
df$user <- df$system <- df$real <- df$result <- NULL

expect_equal(nrow(df), 17)
local_reproducible_output(width = 200)
local_edition(3) # set to 2 in ./test_dir
expect_snapshot_output(print(df))
})

test_that("runs additional dev tests", {
withr::local_envvar(TESTTHAT_PARALLEL = "FALSE", NOT_CRAN = "true")
res <- test_dir(test_path("test_dir"), reporter = "silent", stop_on_failure = FALSE)
expect_equal(nrow(as.data.frame(res)), 18)
})

test_that("complains if no files", {
withr::local_envvar(TESTTHAT_PARALLEL = "FALSE")
path <- withr::local_tempfile()
Expand Down
3 changes: 3 additions & 0 deletions tests/testthat/test_dir/dev-test-basic.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
test_that("test runs in developer mode only", {
expect_true(TRUE)
})
2 changes: 1 addition & 1 deletion vignettes/special-files.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ This vignette describes the various special files that testthat understands: tes
## Test files

These are the bread and butter of testthat.
Test files live in `tests/testthat/`, start with either `test-` or `test_`, and end with `.r` or `.R`.
Public test files live in `tests/testthat/`, start with either `test-` or `test_`, and end with `.r` or `.R`. Additional developer tests use the same pattern but prefixed with `dev-`.
We recommend organising your test files so that there's a one-to-one correspondence between the files in `R/` and the files in `tests/testthat/` so that (e.g.) `R/myfile.R` has a matching `tests/testthat/test-myfile.R`.
This correspondence is maintained by functions like `usethis::use_r()` and `usethis::use_test()` and is taken advantage of by functions like `devtools::test_active_file()` and `devtools::test_coverage_active_file()`.

Expand Down

0 comments on commit 5977ea6

Please sign in to comment.