Skip to content

Commit 758d2ce

Browse files
committed
Reduce duplication in parallel tests
1 parent 5ce84f0 commit 758d2ce

File tree

8 files changed

+36
-65
lines changed

8 files changed

+36
-65
lines changed

tests/testthat/helper.R

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
local_parallel_test_config <- function(frame = caller_env()) {
2+
skip_on_covr()
3+
withr::local_envvar(
4+
c(
5+
TESTTHAT_PARALLEL = "TRUE",
6+
TESTTHAT_GHA_SUMMARY = "FALSE"
7+
),
8+
.local_envir = frame
9+
)
10+
}
11+
12+
capture_parallel_error <- function(path) {
13+
tryCatch(
14+
capture.output(suppressMessages(testthat::test_local(
15+
path,
16+
reporter = "summary"
17+
))),
18+
error = function(e) e
19+
)
20+
}

tests/testthat/test-parallel-crash.R

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
11
test_that("crash", {
2+
local_parallel_test_config()
23
skip_on_cran()
3-
skip_on_covr()
44
skip_if_not(getRversion() >= "4.4.0")
55

6-
withr::local_envvar(c(
7-
TESTTHAT_PARALLEL = "TRUE",
8-
TESTTHAT_GHA_SUMMARY = "FALSE"
9-
))
10-
116
pkg <- test_path("test-parallel", "crash")
127
err <- callr::r(
138
function() {

tests/testthat/test-parallel-errors.R

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
11
test_that("error in parallel setup code", {
2-
skip_on_covr()
3-
withr::local_envvar(TESTTHAT_PARALLEL = "TRUE")
4-
err <- tryCatch(
5-
capture.output(suppressMessages(testthat::test_local(
6-
test_path("test-parallel", "syntax-error"),
7-
reporter = "summary"
8-
))),
9-
error = function(e) e
10-
)
2+
local_parallel_test_config()
3+
4+
err <- capture_parallel_error(test_path("test-parallel", "syntax-error"))
115
expect_s3_class(err, "testthat_process_error")
126
# contains test file's name
137
expect_match(conditionMessage(err), "test-error-1.R")
Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,6 @@
11
test_that("error outside of test_that()", {
2-
skip_on_covr()
3-
withr::local_envvar(c(
4-
TESTTHAT_PARALLEL = "TRUE",
5-
TESTTHAT_GHA_SUMMARY = "FALSE"
6-
))
7-
err <- tryCatch(
8-
capture.output(suppressMessages(testthat::test_local(
9-
test_path("test-parallel", "outside"),
10-
reporter = "summary"
11-
))),
12-
error = function(e) e
13-
)
2+
local_parallel_test_config()
143

4+
err <- capture_parallel_error(test_path("test-parallel", "outside"))
155
expect_match(err$message, "Test failures")
166
})
Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,7 @@
11
test_that("error in parallel setup code", {
2-
skip_on_covr()
3-
withr::local_envvar(c(
4-
TESTTHAT_PARALLEL = "TRUE",
5-
TESTTHAT_GHA_SUMMARY = "FALSE"
6-
))
7-
err <- tryCatch(
8-
capture.output(suppressMessages(testthat::test_local(
9-
test_path("test-parallel", "setup"),
10-
reporter = "summary"
11-
))),
12-
error = function(e) e
13-
)
2+
local_parallel_test_config()
3+
4+
err <- capture_parallel_error(test_path("test-parallel", "setup"))
145
expect_s3_class(err, "testthat_process_error")
156
expect_match(conditionMessage(err), "Error in setup", fixed = TRUE)
167
})
Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,7 @@
11
test_that("startup error", {
2-
skip_on_covr()
3-
withr::local_envvar(c(
4-
TESTTHAT_PARALLEL = "TRUE",
5-
TESTTHAT_GHA_SUMMARY = "FALSE"
6-
))
7-
err <- tryCatch(
8-
capture.output(suppressMessages(testthat::test_local(
9-
test_path("test-parallel", "startup"),
10-
reporter = "summary"
11-
))),
12-
error = function(e) e
13-
)
2+
local_parallel_test_config()
3+
4+
err <- capture_parallel_error(test_path("test-parallel", "startup"))
145
expect_s3_class(err, "testthat_process_error")
156
expect_match(conditionMessage(err), "This will fail", fixed = TRUE)
167
})

tests/testthat/test-parallel-stdout.R

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
test_that("stdout/stderr in parallel code", {
2-
skip_on_covr()
3-
withr::local_envvar(TESTTHAT_PARALLEL = "TRUE")
2+
local_parallel_test_config()
43

54
assemble_msgs <- function(txt, test_name) {
65
prefix <- paste0("> ", test_name, ": ")
Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,8 @@
11
test_that("teardown error", {
22
skip("teardown errors are ignored")
3-
skip_on_covr()
4-
withr::local_envvar(c(
5-
TESTTHAT_PARALLEL = "TRUE",
6-
TESTTHAT_GHA_SUMMARY = "FALSE"
7-
))
8-
err <- tryCatch(
9-
capture.output(suppressMessages(testthat::test_local(
10-
test_path("test-parallel", "teardown"),
11-
reporter = "summary"
12-
))),
13-
error = function(e) e
14-
)
3+
local_parallel_test_config()
4+
5+
err <- capture_parallel_error(test_path("test-parallel", "teardown"))
156
expect_s3_class(err, "testthat_process_error")
167
expect_match(err$message, "Error in teardown", fixed = TRUE)
178
})

0 commit comments

Comments
 (0)