You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[Test] Print RNG of a failed testset and add option to set it (#56260)
Also, add a keyword option to `@testset` to let users override the seed
used there, to make testsets more replicable.
To give you a taster of what this PR
enables:
```
julia> using Random, Test
julia> @testset begin
@test rand() == 0
end;
test set: Test Failed at REPL[2]:2
Expression: rand() == 0
Evaluated: 0.559472630416976 == 0
Stacktrace:
[1] top-level scope
@ REPL[2]:2
[2] macro expansion
@ ~/repo/julia/usr/share/julia/stdlib/v1.12/Test/src/Test.jl:1713 [inlined]
[3] macro expansion
@ REPL[2]:2 [inlined]
[4] macro expansion
@ ~/repo/julia/usr/share/julia/stdlib/v1.12/Test/src/Test.jl:679 [inlined]
Test Summary: | Fail Total Time
test set | 1 1 0.9s
ERROR: Some tests did not pass: 0 passed, 1 failed, 0 errored, 0 broken.
Random seed for this testset: Xoshiro(0x2e026445595ed28e, 0x07bb81ac4c54926d, 0x83d7d70843e8bad6, 0xdbef927d150af80b, 0xdbf91ddf2534f850)
julia> @testset rng=Xoshiro(0x2e026445595ed28e, 0x07bb81ac4c54926d, 0x83d7d70843e8bad6, 0xdbef927d150af80b, 0xdbf91ddf2534f850) begin
@test rand() == 0.559472630416976
end;
Test Summary: | Pass Total Time
test set | 1 1 0.0s
```
This also works with nested testsets, and testsets on for loops:
```
julia> @testset rng=Xoshiro(0xc380f460355639ee, 0xb39bc754b7d63bbf, 0x1551dbcfb5ed5668, 0x71ab5a18fec21a25, 0x649d0c1be1ca5436) "Outer" begin
@test rand() == 0.0004120194925605336
@testset rng=Xoshiro(0xee97f5b53f7cdc49, 0x480ac387b0527d3d, 0x614b416502a9e0f5, 0x5250cb36e4a4ceb1, 0xed6615c59e475fa0) "Inner: $(i)" for i in 1:10
@test rand() == 0.39321938407066637
end
end;
Test Summary: | Pass Total Time
Outer | 11 11 0.0s
```
Being able to see what was the seed inside a testset and being able to
set it afterwards should make replicating test failures which only
depend on the state of the RNG much easier to debug.
Copy file name to clipboardExpand all lines: NEWS.md
+9Lines changed: 9 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -183,6 +183,15 @@ Standard library changes
183
183
184
184
#### Test
185
185
186
+
* A failing `DefaultTestSet` now prints to screen the random number generator (RNG) of the failed test, to help reproducing a stochastic failure which only depends on the state of the RNG.
187
+
It is also possible seed a test set by passing the `rng` keyword argument to `@testset`:
188
+
```julia
189
+
using Test, Random
190
+
@testset rng=Xoshiro(0x2e026445595ed28e, 0x07bb81ac4c54926d, 0x83d7d70843e8bad6, 0xdbef927d150af80b, 0xdbf91ddf2534f850) begin
0 commit comments