Skip to content

Commit 99cf12c

Browse files
committed
Merge pull request #14242 from JuliaLang/betternoerror
Improve message for at-test_throw fails
2 parents c741432 + b47150e commit 99cf12c

File tree

2 files changed

+36
-7
lines changed

2 files changed

+36
-7
lines changed

base/test.jl

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ function Base.show(io::IO, t::Pass)
5757
end
5858

5959
"""
60-
Pass
60+
Fail
6161
6262
The test condition was false, i.e. the expression evaluated to false or
6363
the correct exception was not thrown.
@@ -71,10 +71,14 @@ end
7171
function Base.show(io::IO, t::Fail)
7272
print_with_color(:red, io, "Test Failed\n")
7373
print(io, " Expression: ", t.orig_expr)
74-
if t.test_type == :test_throws
75-
# Either no exception, or wrong exception
74+
if t.test_type == :test_throws_wrong
75+
# An exception was thrown, but it was of the wrong type
7676
print(io, "\n Expected: ", t.expr)
7777
print(io, "\n Thrown: ", typeof(t.value))
78+
elseif t.test_type == :test_throws_nothing
79+
# An exception was expected, but no exception was thrown
80+
print(io, "\n Expected: ", t.expr)
81+
print(io, "\n No exception thrown")
7882
elseif !isa(t.expr, Expr)
7983
# Maybe just a constant, like false
8084
print(io, "\n Evaluated: ", t.expr)
@@ -212,14 +216,14 @@ function do_test_throws(predicate, orig_expr, bt, extype)
212216
try
213217
predicate()
214218
# If we hit this line, no exception was thrown. We treat
215-
# this as equivalent to the wrong exception being thrown.
216-
Fail(:test_throws, orig_expr, extype, nothing)
219+
# this a failure equivalent to the wrong exception being thrown.
220+
Fail(:test_throws_nothing, orig_expr, extype, nothing)
217221
catch err
218222
# Check the right type of exception was thrown
219223
if isa(err, extype)
220224
Pass(:test_throws, orig_expr, extype, err)
221225
else
222-
Fail(:test_throws, orig_expr, extype, err)
226+
Fail(:test_throws_wrong, orig_expr, extype, err)
223227
end
224228
end)
225229
end

test/test.jl

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ a[1,1,1,1,1] = 10
1818

1919
@test rand() != rand()
2020

21-
# Test printing of result types
21+
# Test printing of Pass results
2222
# Pass - constant
2323
@test contains(sprint(show, @test true), "Expression: true")
2424
# Pass - expression
@@ -28,6 +28,31 @@ a[1,1,1,1,1] = 10
2828
@test contains(sprint(show, @test_throws ErrorException error()),
2929
"Thrown: ErrorException")
3030

31+
# Test printing of Fail results
32+
type NoThrowTestSet <: Base.Test.AbstractTestSet
33+
results::Vector
34+
NoThrowTestSet(desc) = new([])
35+
end
36+
Base.Test.record(ts::NoThrowTestSet, t::Base.Test.Result) = (push!(ts.results, t); t)
37+
Base.Test.finish(ts::NoThrowTestSet) = ts.results
38+
fails = @testset NoThrowTestSet begin
39+
# Fail - wrong exception
40+
@test_throws OverflowError error()
41+
# Fail - no exception
42+
@test_throws OverflowError 1 + 1
43+
# Fail - const
44+
@test false
45+
# Fail - comparison
46+
@test 1+1 == 2+2
47+
end
48+
for i in 1:4
49+
@test isa(fails[i], Base.Test.Fail)
50+
end
51+
@test contains(sprint(show, fails[1]), "Thrown: ErrorException")
52+
@test contains(sprint(show, fails[2]), "No exception thrown")
53+
@test contains(sprint(show, fails[3]), "Evaluated: false")
54+
@test contains(sprint(show, fails[4]), "Evaluated: 2 == 4")
55+
3156
# Test printing of a TestSetException
3257
tse_str = sprint(show, Test.TestSetException(1,2,3))
3358
@test contains(tse_str, "1 passed")

0 commit comments

Comments
 (0)