Skip to content

Commit 7188305

Browse files
authored
store exceptions and backtraces as strings in test Error results (#26279)
fixes #25297
1 parent 7055d4c commit 7188305

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

stdlib/Test/src/Test.jl

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,18 @@ mutable struct Error <: Result
132132
value
133133
backtrace
134134
source::LineNumberNode
135+
136+
function Error(test_type, orig_expr, value, bt, source)
137+
if test_type === :test_error
138+
bt = scrub_backtrace(bt)
139+
end
140+
if test_type === :test_error || test_type === :nontest_error
141+
bt_str = sprint(showerror, value, bt)
142+
else
143+
bt_str = ""
144+
end
145+
new(test_type, orig_expr, repr(value), bt_str, source)
146+
end
135147
end
136148
function Base.show(io::IO, t::Error)
137149
if t.test_type == :test_interrupted
@@ -146,24 +158,22 @@ function Base.show(io::IO, t::Error)
146158
println(io, " Expression: ", t.orig_expr)
147159
print( io, " Value: ", t.value)
148160
elseif t.test_type == :test_error
149-
println(io, " Test threw an exception of type ", typeof(t.value))
161+
println(io, " Test threw exception ", t.value)
150162
println(io, " Expression: ", t.orig_expr)
151163
# Capture error message and indent to match
152-
errmsg = sprint(showerror, t.value, scrub_backtrace(t.backtrace))
153164
print(io, join(map(line->string(" ",line),
154-
split(errmsg, "\n")), "\n"))
165+
split(t.backtrace, "\n")), "\n"))
155166
elseif t.test_type == :test_unbroken
156167
# A test that was expected to fail did not
157168
println(io, " Unexpected Pass")
158169
println(io, " Expression: ", t.orig_expr)
159170
println(io, " Got correct result, please change to @test if no longer broken.")
160171
elseif t.test_type == :nontest_error
161172
# we had an error outside of a @test
162-
println(io, " Got an exception of type $(typeof(t.value)) outside of a @test")
173+
println(io, " Got exception $(t.value) outside of a @test")
163174
# Capture error message and indent to match
164-
errmsg = sprint(showerror, t.value, t.backtrace)
165175
print(io, join(map(line->string(" ",line),
166-
split(errmsg, "\n")), "\n"))
176+
split(t.backtrace, "\n")), "\n"))
167177
end
168178
end
169179

0 commit comments

Comments
 (0)