Skip to content

Commit cb3a719

Browse files
committed
Improve unpredictable parallel stdout test case
Assemble messages from each worker separately before testing.
1 parent 14b0541 commit cb3a719

File tree

1 file changed

+21
-27
lines changed

1 file changed

+21
-27
lines changed
Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,26 @@
11
test_that("stdout/stderr in parallel code", {
22
skip_on_covr()
33
withr::local_envvar(TESTTHAT_PARALLEL = "TRUE")
4-
out <- capture.output(suppressMessages(testthat::test_local(
5-
test_path("test-parallel", "stdout"),
6-
reporter = "summary"
7-
)))
8-
expect_match(
9-
out,
10-
"> test-stdout-2.R: This is a message!",
11-
fixed = TRUE,
12-
all = FALSE
13-
)
14-
expect_match(out, "test-stdout-3.R: [1] 1 2 3", fixed = TRUE, all = FALSE)
154

16-
out2 <- capture.output(suppressMessages(testthat::test_local(
17-
test_path("test-parallel", "stdout"),
18-
reporter = "progress"
19-
)))
20-
expect_match(
21-
out2,
22-
"> test-stdout-2.R: This is a message!",
23-
fixed = TRUE,
24-
all = FALSE
25-
)
26-
expect_match(
27-
out2,
28-
"test-stdout-3.R: [1] 1 2 3",
29-
fixed = TRUE,
30-
all = FALSE
31-
)
5+
assemble_msgs <- function(txt, test_name) {
6+
prefix <- paste0("> ", test_name, ": ")
7+
parts <- sub(
8+
prefix,
9+
"",
10+
grep(prefix, out, fixed = TRUE, value = TRUE),
11+
fixed = TRUE
12+
)
13+
paste(parts, collapse = "")
14+
}
15+
16+
for (reporter in c("summary", "progress")) {
17+
out <- capture.output(suppressMessages(testthat::test_local(
18+
test_path("test-parallel", "stdout"),
19+
reporter = reporter
20+
)))
21+
msg2 <- assemble_msgs(out, "test-stdout-2.R")
22+
expect_match(msg2, "This is a message!", fixed = TRUE)
23+
msg3 <- assemble_msgs(out, "test-stdout-3.R")
24+
expect_match(msg3, "[1] 1 2 3", fixed = TRUE)
25+
}
3226
})

0 commit comments

Comments
 (0)