Skip to content

Commit 32b9ea5

Browse files
mmiller-maxjohanmon
authored andcommitted
Test: Add information to passing tests (JuliaLang#36879)
Closes JuliaLang#25483
1 parent 48f65f2 commit 32b9ea5

File tree

5 files changed

+56
-8
lines changed

5 files changed

+56
-8
lines changed

stdlib/Test/docs/src/index.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,13 @@ If the condition is true, a `Pass` is returned:
4242
```jldoctest testfoo
4343
julia> @test foo("bar") == 9
4444
Test Passed
45+
Expression: foo("bar") == 9
46+
Evaluated: 9 == 9
4547
4648
julia> @test foo("fizz") >= 10
4749
Test Passed
50+
Expression: foo("fizz") >= 10
51+
Evaluated: 16 >= 10
4852
```
4953

5054
If the condition is false, then a `Fail` is returned and an exception is thrown:
@@ -83,6 +87,7 @@ to check that this occurs:
8387
```jldoctest testfoo
8488
julia> @test_throws MethodError foo(:cat)
8589
Test Passed
90+
Expression: foo(:cat)
8691
Thrown: MethodError
8792
```
8893

@@ -194,6 +199,8 @@ checks using either `@test a ≈ b` (where `≈`, typed via tab completion of `\
194199
```jldoctest
195200
julia> @test 1 ≈ 0.999999999
196201
Test Passed
202+
Expression: 1 ≈ 0.999999999
203+
Evaluated: 1 ≈ 0.999999999
197204
198205
julia> @test 1 ≈ 0.999999
199206
Test Failed at none:1
@@ -206,6 +213,8 @@ after the `≈` comparison:
206213
```jldoctest
207214
julia> @test 1 ≈ 0.999999 rtol=1e-5
208215
Test Passed
216+
Expression: ≈(1, 0.999999, rtol = 1.0e-5)
217+
Evaluated: ≈(1, 0.999999; rtol = 1.0e-5)
209218
```
210219
Note that this is not a specific feature of the `` but rather a general feature of the `@test` macro: `@test a <op> b key=val` is transformed by the macro into `@test op(a, b, key=val)`. It is, however, particularly useful for `` tests.
211220

stdlib/Test/src/Test.jl

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,9 @@ struct Pass <: Result
8585
orig_expr
8686
data
8787
value
88-
function Pass(test_type::Symbol, orig_expr, data, thrown)
89-
return new(test_type, orig_expr, data, thrown isa String ? "String" : thrown)
88+
source::Union{Nothing,LineNumberNode}
89+
function Pass(test_type::Symbol, orig_expr, data, thrown, source)
90+
return new(test_type, orig_expr, data, thrown isa String ? "String" : thrown, source)
9091
end
9192
end
9293

@@ -236,6 +237,7 @@ function Serialization.serialize(s::Serialization.AbstractSerializer, t::Pass)
236237
Serialization.serialize(s, t.orig_expr === nothing ? nothing : string(t.orig_expr))
237238
Serialization.serialize(s, t.data === nothing ? nothing : string(t.data))
238239
Serialization.serialize(s, string(t.value))
240+
Serialization.serialize(s, t.source === nothing ? nothing : t.source)
239241
nothing
240242
end
241243

@@ -353,9 +355,12 @@ Returns a `Pass` `Result` if it does, a `Fail` `Result` if it is
353355
```jldoctest
354356
julia> @test true
355357
Test Passed
358+
Expression: true
356359
357360
julia> @test [1, 2] + [2, 1] == [3, 3]
358361
Test Passed
362+
Expression: [1, 2] + [2, 1] == [3, 3]
363+
Evaluated: [3, 3] == [3, 3]
359364
```
360365
361366
The `@test f(args...) key=val...` form is equivalent to writing
@@ -365,6 +370,8 @@ is a call using infix syntax such as approximate comparisons:
365370
```jldoctest
366371
julia> @test π ≈ 3.14 atol=0.01
367372
Test Passed
373+
Expression: ≈(π, 3.14, atol = 0.01)
374+
Evaluated: ≈(π, 3.14; atol = 0.01)
368375
```
369376
370377
This is equivalent to the uglier test `@test ≈(π, 3.14, atol=0.01)`.
@@ -393,13 +400,17 @@ Test Broken
393400
394401
julia> @test 2 + 2 ≈ 5 atol=1 broken=false
395402
Test Passed
403+
Expression: ≈(2 + 2, 5, atol = 1)
404+
Evaluated: ≈(4, 5; atol = 1)
396405
397406
julia> @test 2 + 2 == 5 skip=true
398407
Test Broken
399408
Skipped: 2 + 2 == 5
400409
401410
julia> @test 2 + 2 == 4 skip=false
402411
Test Passed
412+
Expression: 2 + 2 == 4
413+
Evaluated: 4 == 4
403414
```
404415
405416
!!! compat "Julia 1.7"
@@ -610,7 +621,7 @@ function do_test(result::ExecutionResult, orig_expr)
610621
value = result.value
611622
testres = if isa(value, Bool)
612623
# a true value Passes
613-
value ? Pass(:test, nothing, nothing, value) :
624+
value ? Pass(:test, orig_expr, result.data, value, result.source) :
614625
Fail(:test, orig_expr, result.data, value, result.source)
615626
else
616627
# If the result is non-Boolean, this counts as an Error
@@ -652,10 +663,12 @@ Note that `@test_throws` does not support a trailing keyword form.
652663
```jldoctest
653664
julia> @test_throws BoundsError [1, 2, 3][4]
654665
Test Passed
666+
Expression: ([1, 2, 3])[4]
655667
Thrown: BoundsError
656668
657669
julia> @test_throws DimensionMismatch [1, 2, 3] + [1, 2]
658670
Test Passed
671+
Expression: [1, 2, 3] + [1, 2]
659672
Thrown: DimensionMismatch
660673
```
661674
"""
@@ -713,7 +726,7 @@ function do_test_throws(result::ExecutionResult, orig_expr, extype)
713726
end
714727
end
715728
if success
716-
testres = Pass(:test_throws, nothing, nothing, exc)
729+
testres = Pass(:test_throws, orig_expr, extype, exc, result.source)
717730
else
718731
testres = Fail(:test_throws_wrong, orig_expr, extype, exc, result.source)
719732
end

stdlib/Test/src/logging.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ macro test_logs(exs...)
178178
$(esc(expression))
179179
end
180180
if didmatch
181-
testres = Pass(:test, nothing, nothing, value)
181+
testres = Pass(:test, $orig_expr, nothing, value, $sourceloc)
182182
else
183183
testres = LogTestFailure($orig_expr, $sourceloc,
184184
$(QuoteNode(exs[1:end-1])), logs)

stdlib/Test/test/runtests.jl

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ end
288288

289289
let retval_tests = @testset NoThrowTestSet begin
290290
ts = Test.DefaultTestSet("Mock for testing retval of record(::DefaultTestSet, ::T <: Result) methods")
291-
pass_mock = Test.Pass(:test, 1, 2, LineNumberNode(0, "A Pass Mock"))
291+
pass_mock = Test.Pass(:test, 1, 2, 3, LineNumberNode(0, "A Pass Mock"))
292292
@test Test.record(ts, pass_mock) isa Test.Pass
293293
error_mock = Test.Error(:test, 1, 2, 3, LineNumberNode(0, "An Error Mock"))
294294
@test Test.record(ts, error_mock) isa Test.Error
@@ -988,7 +988,7 @@ end
988988

989989
let ex = :(something_complex + [1, 2, 3])
990990
b = PipeBuffer()
991-
let t = Test.Pass(:test, (ex, 1), (ex, 2), (ex, 3))
991+
let t = Test.Pass(:test, (ex, 1), (ex, 2), (ex, 3), LineNumberNode(@__LINE__, @__FILE__))
992992
serialize(b, t)
993993
@test string(t) == string(deserialize(b))
994994
@test eof(b)
@@ -1177,3 +1177,29 @@ end
11771177
# Decorated LoadErrors are not unwrapped if a LoadError was thrown.
11781178
@test_throws LoadError("file", 111, ErrorException("Real error")) @macroexpand @test_macro_throw_2
11791179
end
1180+
1181+
# Issue 25483
1182+
mutable struct PassInformationTestSet <: Test.AbstractTestSet
1183+
results::Vector
1184+
PassInformationTestSet(desc) = new([])
1185+
end
1186+
Test.record(ts::PassInformationTestSet, t::Test.Result) = (push!(ts.results, t); t)
1187+
Test.finish(ts::PassInformationTestSet) = ts
1188+
@testset "Information in Pass result (Issue 25483)" begin
1189+
ts = @testset PassInformationTestSet begin
1190+
@test 1 == 1
1191+
@test_throws ErrorException throw(ErrorException("Msg"))
1192+
end
1193+
test_line_number = (@__LINE__) - 3
1194+
test_throws_line_number = (@__LINE__) - 3
1195+
@test ts.results[1].test_type == :test
1196+
@test ts.results[1].orig_expr == :(1 == 1)
1197+
@test ts.results[1].data == Expr(:comparison, 1, :(==), 1)
1198+
@test ts.results[1].value == true
1199+
@test ts.results[1].source == LineNumberNode(test_line_number, @__FILE__)
1200+
@test ts.results[2].test_type == :test_throws
1201+
@test ts.results[2].orig_expr == :(throw(ErrorException("Msg")))
1202+
@test ts.results[2].data == ErrorException
1203+
@test ts.results[2].value == ErrorException("Msg")
1204+
@test ts.results[2].source == LineNumberNode(test_throws_line_number, @__FILE__)
1205+
end

test/runtests.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ cd(@__DIR__) do
363363
elseif isa(resp, Test.TestSetException)
364364
fake = Test.DefaultTestSet(testname)
365365
for i in 1:resp.pass
366-
Test.record(fake, Test.Pass(:test, nothing, nothing, nothing))
366+
Test.record(fake, Test.Pass(:test, nothing, nothing, nothing, LineNumberNode(@__LINE__, @__FILE__)))
367367
end
368368
for i in 1:resp.broken
369369
Test.record(fake, Test.Broken(:test, nothing))

0 commit comments

Comments
 (0)