Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support developer tests #1988

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possibly there is room for an explicit opt in/out env var that is very specific to this feature, like:

run_dev_tests <- function() {
  if (env_var_is_true("RUN_DEV_TESTS")) {
     return(TRUE)
  }

  if (env_var_is_false("RUN_DEV_TESTS")) {
     return(FALSE)
  }

  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
Loading