Skip to content

Commit ca0543e

Browse files
authored
Updated source code from rlib/testthat/main (#5)
Give test_dir() a recursive option (#1605) The recursive argument allows test files in nested directories.
1 parent 29018e0 commit ca0543e

File tree

15 files changed

+118
-5
lines changed

15 files changed

+118
-5
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: testthat
22
Title: Unit Testing for R
3-
Version: 3.1.10.9000
3+
Version: 3.1.10.9001
44
Authors@R: c(
55
person("Hadley", "Wickham", , "[email protected]", role = c("aut", "cre")),
66
person("Posit Software, PBC", role = c("cph", "fnd")),

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
* `expect_setequal()` correctly displays results when only one of actual and
1515
expected is missing values (#1835).
16+
17+
* `test_dir()` gains a `recursive` argument which allows test files in nested directories (#1605).
1618

1719
# testthat 3.1.10
1820

R/test-files.R

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#' @param stop_on_failure If `TRUE`, throw an error if any tests fail.
2727
#' @param stop_on_warning If `TRUE`, throw an error if any tests generate
2828
#' warnings.
29+
#' @param recursive If `TRUE` Test that will search for test files in the nested directories.
2930
#' @param load_package Strategy to use for load package code:
3031
#' * "none", the default, doesn't load the package.
3132
#' * "installed", uses [library()] to load an installed package.
@@ -52,6 +53,7 @@ test_dir <- function(path,
5253
stop_on_warning = FALSE,
5354
wrap = lifecycle::deprecated(),
5455
package = NULL,
56+
recursive = FALSE,
5557
load_package = c("none", "installed", "source")
5658
) {
5759

@@ -63,7 +65,8 @@ test_dir <- function(path,
6365
filter = filter,
6466
...,
6567
full.names = FALSE,
66-
start_first = start_first
68+
start_first = start_first,
69+
recursive = recursive
6770
)
6871
if (length(test_paths) == 0) {
6972
abort("No test files found")
@@ -380,8 +383,8 @@ local_teardown_env <- function(frame = parent.frame()) {
380383
#' @return A character vector of paths
381384
#' @keywords internal
382385
#' @export
383-
find_test_scripts <- function(path, filter = NULL, invert = FALSE, ..., full.names = TRUE, start_first = NULL) {
384-
files <- dir(path, "^test.*\\.[rR]$", full.names = full.names)
386+
find_test_scripts <- function(path, filter = NULL, invert = FALSE, ..., full.names = TRUE, start_first = NULL, recursive = FALSE) {
387+
files <- dir(path, "^test.*\\.[rR]$", full.names = full.names, recursive = recursive)
385388
files <- filter_test_scripts(files, filter, invert, ...)
386389
order_test_scripts(files, start_first)
387390
}

man/find_test_scripts.Rd

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/test_dir.Rd

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/_snaps/test-files.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,24 @@
1919
16 test-helper.R helper test 1 0 FALSE FALSE 0 1
2020
17 test-skip.R Skips skip 1 0 TRUE FALSE 0 0
2121

22+
# runs all tests in nested directories and records output
23+
24+
file context test nb failed skipped error warning passed
25+
1 nested_folder/test-errors.R simple 0 0 FALSE TRUE 0 0
26+
2 nested_folder/test-errors.R after one success 1 0 FALSE TRUE 0 1
27+
3 nested_folder/test-errors.R after one failure 1 1 FALSE TRUE 0 0
28+
4 nested_folder/test-errors.R in the test 0 0 FALSE TRUE 0 0
29+
5 nested_folder/test-errors.R in expect_error 1 0 FALSE FALSE 0 1
30+
6 nested_folder/test-failures.R just one failure 1 1 FALSE FALSE 0 0
31+
7 nested_folder/test-failures.R one failure on two 2 1 FALSE FALSE 0 1
32+
8 nested_folder/test-failures.R no failure 2 0 FALSE FALSE 0 2
33+
9 nested_folder/test-skip.R Skips skip 1 0 TRUE FALSE 0 0
34+
10 test-basic.R logical tests act as expected 2 0 FALSE FALSE 0 2
35+
11 test-basic.R logical tests ignore attributes 2 0 FALSE FALSE 0 2
36+
12 test-basic.R equality holds 2 0 FALSE FALSE 0 2
37+
13 test-basic.R can't access variables from other tests 2 1 0 TRUE FALSE 0 0
38+
14 test-basic.R can't access variables from other tests 1 1 0 FALSE FALSE 0 1
39+
15 test-empty.R empty test 1 0 TRUE FALSE 0 0
40+
16 test-empty.R empty test with error 0 0 FALSE TRUE 0 0
41+
17 test-helper.R helper test 1 0 FALSE FALSE 0 1
42+

tests/testthat/test-test-files.R

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,17 @@ test_that("can control if warnings errors", {
4646
expect_error(test_warning(stop_on_warning = FALSE), NA)
4747
})
4848

49+
test_that("runs all tests in nested directories and records output", {
50+
withr::local_envvar(TESTTHAT_PARALLEL = "FALSE")
51+
res <- test_dir(test_path("test_dir_recursive"), reporter = "silent", stop_on_failure = FALSE, recursive = TRUE)
52+
df <- as.data.frame(res)
53+
df$user <- df$system <- df$real <- df$result <- NULL
54+
55+
local_reproducible_output(width = 200)
56+
local_edition(3)
57+
expect_snapshot_output(print(df))
58+
})
59+
4960
# test_file ---------------------------------------------------------------
5061

5162
test_that("can test single file", {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
hello <- function() "Hello World"
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
test_that("simple", {
2+
stop("argh")
3+
})
4+
5+
test_that("after one success", {
6+
expect_true(TRUE)
7+
stop("argh")
8+
expect_true(TRUE)
9+
})
10+
11+
test_that("after one failure", {
12+
expect_true(FALSE)
13+
stop("argh")
14+
})
15+
16+
test_that("in the test", {
17+
expect_true(stop("Argh"))
18+
})
19+
20+
test_that("in expect_error", {
21+
expect_error(stop("Argh"))
22+
})
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
test_that("just one failure", {
2+
expect_true(FALSE)
3+
})
4+
5+
test_that("one failure on two", {
6+
expect_false(FALSE)
7+
expect_true(FALSE)
8+
})
9+
10+
test_that("no failure", {
11+
expect_false(FALSE)
12+
expect_true(TRUE)
13+
})

0 commit comments

Comments
 (0)