Skip to content

Commit a67d42e

Browse files
committed
Add information to passing tests (#25483)
1 parent 907264b commit a67d42e

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

@@ -193,6 +198,8 @@ checks using either `@test a ≈ b` (where `≈`, typed via tab completion of `\
193198
```jldoctest
194199
julia> @test 1 ≈ 0.999999999
195200
Test Passed
201+
Expression: 1 ≈ 0.999999999
202+
Evaluated: 1 ≈ 0.999999999
196203
197204
julia> @test 1 ≈ 0.999999
198205
Test Failed at none:1
@@ -205,6 +212,8 @@ after the `≈` comparison:
205212
```jldoctest
206213
julia> @test 1 ≈ 0.999999 rtol=1e-5
207214
Test Passed
215+
Expression: ≈(1, 0.999999, rtol = 1.0e-5)
216+
Evaluated: ≈(1, 0.999999; rtol = 1.0e-5)
208217
```
209218
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.
210219

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"
@@ -604,7 +615,7 @@ function do_test(result::ExecutionResult, orig_expr)
604615
value = result.value
605616
testres = if isa(value, Bool)
606617
# a true value Passes
607-
value ? Pass(:test, nothing, nothing, value) :
618+
value ? Pass(:test, orig_expr, result.data, value, result.source) :
608619
Fail(:test, orig_expr, result.data, value, result.source)
609620
else
610621
# If the result is non-Boolean, this counts as an Error
@@ -646,10 +657,12 @@ Note that `@test_throws` does not support a trailing keyword form.
646657
```jldoctest
647658
julia> @test_throws BoundsError [1, 2, 3][4]
648659
Test Passed
660+
Expression: ([1, 2, 3])[4]
649661
Thrown: BoundsError
650662
651663
julia> @test_throws DimensionMismatch [1, 2, 3] + [1, 2]
652664
Test Passed
665+
Expression: [1, 2, 3] + [1, 2]
653666
Thrown: DimensionMismatch
654667
```
655668
"""
@@ -707,7 +720,7 @@ function do_test_throws(result::ExecutionResult, orig_expr, extype)
707720
end
708721
end
709722
if success
710-
testres = Pass(:test_throws, nothing, nothing, exc)
723+
testres = Pass(:test_throws, orig_expr, extype, exc, result.source)
711724
else
712725
testres = Fail(:test_throws_wrong, orig_expr, extype, exc, result.source)
713726
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
@@ -281,7 +281,7 @@ end
281281

282282
let retval_tests = @testset NoThrowTestSet begin
283283
ts = Test.DefaultTestSet("Mock for testing retval of record(::DefaultTestSet, ::T <: Result) methods")
284-
pass_mock = Test.Pass(:test, 1, 2, LineNumberNode(0, "A Pass Mock"))
284+
pass_mock = Test.Pass(:test, 1, 2, 3, LineNumberNode(0, "A Pass Mock"))
285285
@test Test.record(ts, pass_mock) isa Test.Pass
286286
error_mock = Test.Error(:test, 1, 2, 3, LineNumberNode(0, "An Error Mock"))
287287
@test Test.record(ts, error_mock) isa Test.Error
@@ -981,7 +981,7 @@ end
981981

982982
let ex = :(something_complex + [1, 2, 3])
983983
b = PipeBuffer()
984-
let t = Test.Pass(:test, (ex, 1), (ex, 2), (ex, 3))
984+
let t = Test.Pass(:test, (ex, 1), (ex, 2), (ex, 3), LineNumberNode(@__LINE__, @__FILE__))
985985
serialize(b, t)
986986
@test string(t) == string(deserialize(b))
987987
@test eof(b)
@@ -1170,3 +1170,29 @@ end
11701170
# Decorated LoadErrors are not unwrapped if a LoadError was thrown.
11711171
@test_throws LoadError("file", 111, ErrorException("Real error")) @macroexpand @test_macro_throw_2
11721172
end
1173+
1174+
# Issue 25483
1175+
mutable struct PassInformationTestSet <: Test.AbstractTestSet
1176+
results::Vector
1177+
PassInformationTestSet(desc) = new([])
1178+
end
1179+
Test.record(ts::PassInformationTestSet, t::Test.Result) = (push!(ts.results, t); t)
1180+
Test.finish(ts::PassInformationTestSet) = ts
1181+
@testset "Information in Pass result (Issue 25483)" begin
1182+
ts = @testset PassInformationTestSet begin
1183+
@test 1 == 1
1184+
@test_throws ErrorException throw(ErrorException("Msg"))
1185+
end
1186+
test_line_number = (@__LINE__) - 3
1187+
test_throws_line_number = (@__LINE__) - 3
1188+
@test ts.results[1].test_type == :test
1189+
@test ts.results[1].orig_expr == :(1 == 1)
1190+
@test ts.results[1].data == Expr(:comparison, 1, :(==), 1)
1191+
@test ts.results[1].value == true
1192+
@test ts.results[1].source == LineNumberNode(test_line_number, @__FILE__)
1193+
@test ts.results[2].test_type == :test_throws
1194+
@test ts.results[2].orig_expr == :(throw(ErrorException("Msg")))
1195+
@test ts.results[2].data == ErrorException
1196+
@test ts.results[2].value == ErrorException("Msg")
1197+
@test ts.results[2].source == LineNumberNode(test_throws_line_number, @__FILE__)
1198+
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)