@@ -214,9 +214,10 @@ struct Error <: Result
214214 orig_expr:: String
215215 value:: String
216216 backtrace:: String
217+ context:: Union{Nothing, String}
217218 source:: LineNumberNode
218219
219- function Error (test_type:: Symbol , orig_expr, value, bt, source:: LineNumberNode )
220+ function Error (test_type:: Symbol , orig_expr, value, bt, source:: LineNumberNode , context :: Union{Nothing, String} = nothing )
220221 if test_type === :test_error
221222 bt = scrub_exc_stack (bt, nothing , extract_file (source))
222223 end
@@ -249,8 +250,14 @@ struct Error <: Result
249250 string (orig_expr),
250251 value,
251252 bt_str,
253+ context,
252254 source)
253255 end
256+
257+ # Internal constructor for creating Error with pre-processed values (used by ContextTestSet)
258+ function Error (test_type:: Symbol , orig_expr:: String , value:: String , backtrace:: String , context:: Union{Nothing, String} , source:: LineNumberNode )
259+ return new (test_type, orig_expr, value, backtrace, context, source)
260+ end
254261end
255262
256263function Base. show (io:: IO , t:: Error )
@@ -268,6 +275,9 @@ function Base.show(io::IO, t::Error)
268275 elseif t. test_type === :test_error
269276 println (io, " Test threw exception" )
270277 println (io, " Expression: " , t. orig_expr)
278+ if t. context != = nothing
279+ println (io, " Context: " , t. context)
280+ end
271281 # Capture error message and indent to match
272282 join (io, (" " * line for line in filter! (! isempty, split (t. backtrace, " \n " ))), " \n " )
273283 elseif t. test_type === :test_unbroken
@@ -752,13 +762,13 @@ function do_test(result::ExecutionResult, orig_expr)
752762 Fail (:test , orig_expr, result. data, value, nothing , result. source, false )
753763 else
754764 # If the result is non-Boolean, this counts as an Error
755- Error (:test_nonbool , orig_expr, value, nothing , result. source)
765+ Error (:test_nonbool , orig_expr, value, nothing , result. source, nothing )
756766 end
757767 else
758768 # The predicate couldn't be evaluated without throwing an
759769 # exception, so that is an Error and not a Fail
760770 @assert isa (result, Threw)
761- testres = Error (:test_error , orig_expr, result. exception, result. backtrace:: Vector{Any} , result. source)
771+ testres = Error (:test_error , orig_expr, result. exception, result. backtrace:: Vector{Any} , result. source, nothing )
762772 end
763773 isa (testres, Pass) || trigger_test_failure_break (result)
764774 record (get_testset (), testres)
@@ -771,11 +781,11 @@ function do_broken_test(result::ExecutionResult, orig_expr)
771781 value = result. value
772782 if isa (value, Bool)
773783 if value
774- testres = Error (:test_unbroken , orig_expr, value, nothing , result. source)
784+ testres = Error (:test_unbroken , orig_expr, value, nothing , result. source, nothing )
775785 end
776786 else
777787 # If the result is non-Boolean, this counts as an Error
778- testres = Error (:test_nonbool , orig_expr, value, nothing , result. source)
788+ testres = Error (:test_nonbool , orig_expr, value, nothing , result. source, nothing )
779789 end
780790 end
781791 record (get_testset (), testres)
@@ -1109,6 +1119,13 @@ function record(c::ContextTestSet, t::Fail)
11091119 context = t. context === nothing ? context : string (t. context, " \n " , context)
11101120 record (c. parent_ts, Fail (t. test_type, t. orig_expr, t. data, t. value, context, t. source, t. message_only))
11111121end
1122+ function record (c:: ContextTestSet , t:: Error )
1123+ context = string (c. context_name, " = " , c. context)
1124+ context = t. context === nothing ? context : string (t. context, " \n " , context)
1125+ # Create a new Error with the same data but updated context using internal constructor
1126+ new_error = Error (t. test_type, t. orig_expr, t. value, t. backtrace, context, t. source)
1127+ record (c. parent_ts, new_error)
1128+ end
11121129
11131130# -----------------------------------------------------------------------
11141131
@@ -1845,7 +1862,7 @@ function testset_beginend_call(args, tests, source)
18451862 if is_failfast_error (err)
18461863 get_testset_depth () > 1 ? rethrow () : failfast_print ()
18471864 else
1848- record (ts, Error (:nontest_error , Expr (:tuple ), err, Base. current_exceptions (), $ (QuoteNode (source))))
1865+ record (ts, Error (:nontest_error , Expr (:tuple ), err, Base. current_exceptions (), $ (QuoteNode (source)), nothing ))
18491866 end
18501867 finally
18511868 copy! (default_rng (), default_rng_orig)
@@ -1933,7 +1950,7 @@ function testset_forloop(args, testloop, source)
19331950 if is_failfast_error (err)
19341951 get_testset_depth () > 1 ? rethrow () : failfast_print ()
19351952 else
1936- record (ts, Error (:nontest_error , Expr (:tuple ), err, Base. current_exceptions (), $ (QuoteNode (source))))
1953+ record (ts, Error (:nontest_error , Expr (:tuple ), err, Base. current_exceptions (), $ (QuoteNode (source)), nothing ))
19371954 end
19381955 end
19391956 end
0 commit comments