Skip to content

Commit c47d6ad

Browse files
authored
Refactoring test_files() and friends (#1086)
* `test_files()` now includes all of the logic needed for running multiple test files, regardless of whether or not it's inside a package. * `test_file()` and `test_dir()` are now wrapper around `test_files()` so they have identical arguments and identical behaviour with helpers/setup/teardown. Fixes #968. * `test_dir()` now sets `stop_on_warning = TRUE` for consistency with other runners.
1 parent b85897e commit c47d6ad

27 files changed

+396
-484
lines changed

DESCRIPTION

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,10 @@ Collate:
115115
'source.R'
116116
'teardown.R'
117117
'test-compiled-code.R'
118-
'test-directory.R'
119118
'test-env.R'
120119
'test-example.R'
121120
'test-files.R'
121+
'test-package.R'
122122
'test-path.R'
123123
'test-that.R'
124124
'try-again.R'

NEWS.md

+9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
# testthat (development version)
22

3+
* `test_file()` now runs helper, setup, and teardown code, and has the
4+
same arguments as `test_dir()` (#968).
5+
6+
* testthat no longer supports tests stored in `inst/tests`. This has been
7+
deprecated since testthat 0.11.0 (released in 2015).
8+
9+
* `test_dir()` now defaults `stop_on_failure` to `TRUE` for consistency with
10+
other `test_` functions.
11+
312
* New `test_local()` tests a local source package directory. It's equivalent
413
to `devtools::test()` but doesn't require devtools and all its dependencies
514
to be installed (#1030).

R/source.R

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
#'
33
#' These are used by [test_dir()] and friends
44
#'
5-
#' @inheritSection test_dir Test files
65
#' @param path Path to files.
76
#' @param pattern Regular expression used to filter files.
87
#' @param env Environment in which to evaluate code.

R/test-directory.R

-226
This file was deleted.

R/test-env.R

+31-6
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,38 @@
1+
#' Determine testing status
2+
#'
3+
#' Use `is_testing()` to determine if code is being run as part of a test and
4+
#' `testing_package()` to retrieve the name of the package being tested. You
5+
#' can also check the underlying env var directly
6+
#' `identical(Sys.getenv("TESTTHAT"), "true")` to avoid creating a run-time
7+
#' dependency on testthat.
8+
#'
9+
#'
10+
#' @export
11+
is_testing <- function() {
12+
identical(Sys.getenv("TESTTHAT"), "true")
13+
}
14+
15+
#' @export
16+
#' @rdname is_testing
17+
testing_package <- function() {
18+
Sys.getenv("TESTTHAT_PKG")
19+
}
20+
121
#' Generate default testing environment.
222
#'
3-
#' We use a new environment which inherits from [globalenv()].
4-
#' In an ideal world, we'd avoid putting the global environment on the
5-
#' search path for tests, but it's not currently possible without losing
23+
#' We use a new environment which inherits from [globalenv()] or a package
24+
#' namespace. In an ideal world, we'd avoid putting the global environment on
25+
#' the search path for tests, but it's not currently possible without losing
626
#' the ability to load packages in tests.
727
#'
828
#' @keywords internal
929
#' @export
10-
test_env <- function() {
11-
new.env(parent = globalenv())
30+
test_env <- function(package = NULL) {
31+
if (is.null(package)) {
32+
child_env(globalenv())
33+
} else {
34+
# Must clone environment so that during R CMD check, it's not locked
35+
# preventing creation of S4 classes
36+
env_clone(asNamespace(package))
37+
}
1238
}
13-

0 commit comments

Comments
 (0)