Skip to content

Commit 3849a4c

Browse files
committed
Use IOCapture to silence tests.
1 parent 43cdf2d commit 3849a4c

File tree

2 files changed

+88
-43
lines changed

2 files changed

+88
-43
lines changed

test/Project.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
[deps]
2-
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
2+
IOCapture = "b5f81e59-6552-4d32-b1f0-c071b021bf89"
3+
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

test/runtests.jl

Lines changed: 86 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,100 @@
11
using ParallelTestRunner
2-
using Test
2+
using Test, IOCapture
33

4-
pushfirst!(ARGS, "--verbose")
4+
@testset "ParallelTestRunner" verbose=true begin
55

6-
runtests(ARGS)
6+
@testset "basic test" begin
7+
println()
8+
println("Showing the output of one test run:")
9+
println("-"^80)
10+
c = IOCapture.capture(passthrough=true) do
11+
runtests(["--verbose"])
12+
end
13+
println("-"^80)
14+
println()
15+
@test contains(c.output, r"basic .+ started at")
16+
@test contains(c.output, "SUCCESS")
17+
end
718

8-
# custom tests, and initialization code
9-
init_code = quote
10-
using Test
11-
should_be_defined() = true
19+
@testset "custom tests and init code" begin
20+
init_code = quote
21+
using Test
22+
should_be_defined() = true
1223

13-
macro should_also_be_defined()
14-
return :(true)
24+
macro should_also_be_defined()
25+
return :(true)
26+
end
1527
end
16-
end
17-
custom_tests = Dict(
18-
"custom" => quote
19-
@test should_be_defined()
20-
@test @should_also_be_defined()
28+
custom_tests = Dict(
29+
"custom" => quote
30+
@test should_be_defined()
31+
@test @should_also_be_defined()
32+
end
33+
)
34+
c = IOCapture.capture() do
35+
runtests(["--verbose"]; init_code, custom_tests)
2136
end
22-
)
23-
runtests(ARGS; init_code, custom_tests)
37+
@test contains(c.output, r"basic .+ started at")
38+
@test contains(c.output, r"custom .+ started at")
39+
@test contains(c.output, "SUCCESS")
40+
end
2441

25-
# custom worker
26-
function test_worker(name)
27-
if name == "needs env var"
28-
return addworker(env = ["SPECIAL_ENV_VAR" => "42"])
42+
@testset "custom worker" begin
43+
function test_worker(name)
44+
if name == "needs env var"
45+
return addworker(env = ["SPECIAL_ENV_VAR" => "42"])
46+
end
47+
return nothing
2948
end
30-
return nothing
31-
end
32-
custom_tests = Dict(
33-
"needs env var" => quote
34-
@test ENV["SPECIAL_ENV_VAR"] == "42"
35-
end,
36-
"doesn't need env var" => quote
37-
@test !haskey(ENV, "SPECIAL_ENV_VAR")
49+
custom_tests = Dict(
50+
"needs env var" => quote
51+
@test ENV["SPECIAL_ENV_VAR"] == "42"
52+
end,
53+
"doesn't need env var" => quote
54+
@test !haskey(ENV, "SPECIAL_ENV_VAR")
55+
end
56+
)
57+
c = IOCapture.capture() do
58+
runtests(["--verbose"]; test_worker, custom_tests)
3859
end
39-
)
40-
runtests(ARGS; test_worker, custom_tests)
60+
@test contains(c.output, r"basic .+ started at")
61+
@test contains(c.output, r"needs env var .+ started at")
62+
@test contains(c.output, r"doesn't need env var .+ started at")
63+
@test contains(c.output, "SUCCESS")
64+
end
4165

42-
# failing test
43-
custom_tests = Dict(
44-
"failing test" => quote
45-
@test 1 == 2
66+
@testset "failing test" begin
67+
custom_tests = Dict(
68+
"failing test" => quote
69+
@test 1 == 2
70+
end
71+
)
72+
c = IOCapture.capture(rethrow=Union{}) do
73+
runtests(["--verbose"]; custom_tests)
4674
end
47-
)
48-
@test_throws Test.FallbackTestSetException("Test run finished with errors") runtests(ARGS; custom_tests)
75+
@test contains(c.output, r"basic .+ started at")
76+
@test contains(c.output, r"failing test .+ failed at")
77+
@test contains(c.output, "FAILURE")
78+
@test contains(c.output, "1 == 2")
79+
@test c.error
80+
@test c.value == Test.FallbackTestSetException("Test run finished with errors")
81+
end
4982

50-
# throwing test
51-
custom_tests = Dict(
52-
"throwing test" => quote
53-
error("This test throws an error")
83+
@testset "throwing test" begin
84+
custom_tests = Dict(
85+
"throwing test" => quote
86+
error("This test throws an error")
87+
end
88+
)
89+
c = IOCapture.capture(rethrow=Union{}) do
90+
runtests(["--verbose"]; custom_tests)
5491
end
55-
)
56-
@test_throws Test.FallbackTestSetException("Test run finished with errors") runtests(ARGS; custom_tests)
92+
@test contains(c.output, r"basic .+ started at")
93+
@test contains(c.output, r"throwing test .+ failed at")
94+
@test contains(c.output, "FAILURE")
95+
@test contains(c.output, "Got exception outside of a @test")
96+
@test c.error
97+
@test c.value == Test.FallbackTestSetException("Test run finished with errors")
98+
end
99+
100+
end

0 commit comments

Comments
 (0)