@@ -978,8 +978,11 @@ mutable struct DefaultTestSet <: AbstractTestSet
978978 n_passed:: Int
979979 anynonpass:: Bool
980980 verbose:: Bool
981+ showtiming:: Bool
982+ time_start:: Float64
983+ time_end:: Union{Float64,Nothing}
981984end
982- DefaultTestSet (desc:: AbstractString ; verbose:: Bool = false ) = DefaultTestSet (String (desc):: String , [], 0 , false , verbose)
985+ DefaultTestSet (desc:: AbstractString ; verbose:: Bool = false , showtiming :: Bool = true ) = DefaultTestSet (String (desc):: String , [], 0 , false , verbose, showtiming, time (), nothing )
983986
984987# For a broken result, simply store the result
985988record (ts:: DefaultTestSet , t:: Broken ) = (push! (ts. results, t); t)
@@ -1026,7 +1029,7 @@ end
10261029function print_test_results (ts:: DefaultTestSet , depth_pad= 0 )
10271030 # Calculate the overall number for each type so each of
10281031 # the test result types are aligned
1029- passes, fails, errors, broken, c_passes, c_fails, c_errors, c_broken = get_test_counts (ts)
1032+ passes, fails, errors, broken, c_passes, c_fails, c_errors, c_broken, duration = get_test_counts (ts)
10301033 total_pass = passes + c_passes
10311034 total_fail = fails + c_fails
10321035 total_error = errors + c_errors
@@ -1044,6 +1047,7 @@ function print_test_results(ts::DefaultTestSet, depth_pad=0)
10441047 error_width = dig_error > 0 ? max (length (" Error" ), dig_error) : 0
10451048 broken_width = dig_broken > 0 ? max (length (" Broken" ), dig_broken) : 0
10461049 total_width = dig_total > 0 ? max (length (" Total" ), dig_total) : 0
1050+ duration_width = max (length (" Time" ), length (duration))
10471051 # Calculate the alignment of the test result counts by
10481052 # recursively walking the tree of test sets
10491053 align = max (get_alignment (ts, 0 ), length (" Test Summary:" ))
@@ -1063,11 +1067,14 @@ function print_test_results(ts::DefaultTestSet, depth_pad=0)
10631067 printstyled (lpad (" Broken" , broken_width, " " ), " " ; bold= true , color= Base. warn_color ())
10641068 end
10651069 if total_width > 0
1066- printstyled (lpad (" Total" , total_width, " " ); bold= true , color= Base. info_color ())
1070+ printstyled (lpad (" Total" , total_width, " " ), " " ; bold= true , color= Base. info_color ())
1071+ end
1072+ if ts. showtiming
1073+ printstyled (lpad (" Time" , duration_width, " " ); bold= true )
10671074 end
10681075 println ()
10691076 # Recursively print a summary at every level
1070- print_counts (ts, depth_pad, align, pass_width, fail_width, error_width, broken_width, total_width)
1077+ print_counts (ts, depth_pad, align, pass_width, fail_width, error_width, broken_width, total_width, duration_width, ts . showtiming )
10711078end
10721079
10731080
@@ -1076,6 +1083,7 @@ const TESTSET_PRINT_ENABLE = Ref(true)
10761083# Called at the end of a @testset, behaviour depends on whether
10771084# this is a child of another testset, or the "root" testset
10781085function finish (ts:: DefaultTestSet )
1086+ ts. time_end = time ()
10791087 # If we are a nested test set, do not print a full summary
10801088 # now - let the parent test set do the printing
10811089 if get_testset_depth () != 0
@@ -1084,7 +1092,7 @@ function finish(ts::DefaultTestSet)
10841092 record (parent_ts, ts)
10851093 return ts
10861094 end
1087- passes, fails, errors, broken, c_passes, c_fails, c_errors, c_broken = get_test_counts (ts)
1095+ passes, fails, errors, broken, c_passes, c_fails, c_errors, c_broken, duration = get_test_counts (ts)
10881096 total_pass = passes + c_passes
10891097 total_fail = fails + c_fails
10901098 total_error = errors + c_errors
@@ -1148,24 +1156,36 @@ function get_test_counts(ts::DefaultTestSet)
11481156 isa (t, Error) && (errors += 1 )
11491157 isa (t, Broken) && (broken += 1 )
11501158 if isa (t, DefaultTestSet)
1151- np, nf, ne, nb, ncp, ncf, nce , ncb = get_test_counts (t)
1159+ np, nf, ne, nb, ncp, ncf, nce , ncb, duration = get_test_counts (t)
11521160 c_passes += np + ncp
11531161 c_fails += nf + ncf
11541162 c_errors += ne + nce
11551163 c_broken += nb + ncb
11561164 end
11571165 end
11581166 ts. anynonpass = (fails + errors + c_fails + c_errors > 0 )
1159- return passes, fails, errors, broken, c_passes, c_fails, c_errors, c_broken
1167+ duration = if isnothing (ts. time_end)
1168+ " "
1169+ else
1170+ dur_s = ts. time_end - ts. time_start
1171+ if dur_s < 60
1172+ string (round (dur_s, digits = 1 ), " s" )
1173+ else
1174+ m, s = divrem (dur_s, 60 )
1175+ s = lpad (string (round (s, digits = 1 )), 4 , " 0" )
1176+ string (round (Int, m), " m" , s, " s" )
1177+ end
1178+ end
1179+ return passes, fails, errors, broken, c_passes, c_fails, c_errors, c_broken, duration
11601180end
11611181
11621182# Recursive function that prints out the results at each level of
11631183# the tree of test sets
11641184function print_counts (ts:: DefaultTestSet , depth, align,
1165- pass_width, fail_width, error_width, broken_width, total_width)
1185+ pass_width, fail_width, error_width, broken_width, total_width, duration_width, showtiming )
11661186 # Count results by each type at this level, and recursively
11671187 # through any child test sets
1168- passes, fails, errors, broken, c_passes, c_fails, c_errors, c_broken = get_test_counts (ts)
1188+ passes, fails, errors, broken, c_passes, c_fails, c_errors, c_broken, duration = get_test_counts (ts)
11691189 subtotal = passes + fails + errors + broken + c_passes + c_fails + c_errors + c_broken
11701190 # Print test set header, with an alignment that ensures all
11711191 # the test results appear above each other
@@ -1204,9 +1224,13 @@ function print_counts(ts::DefaultTestSet, depth, align,
12041224 end
12051225
12061226 if np == 0 && nf == 0 && ne == 0 && nb == 0
1207- printstyled (" No tests " , color= Base. info_color ())
1227+ printstyled (lpad ( " None " , total_width, " " ), " " , color= Base. info_color ())
12081228 else
1209- printstyled (lpad (string (subtotal), total_width, " " ), color= Base. info_color ())
1229+ printstyled (lpad (string (subtotal), total_width, " " ), " " , color= Base. info_color ())
1230+ end
1231+
1232+ if showtiming
1233+ printstyled (lpad (string (duration), duration_width, " " ))
12101234 end
12111235 println ()
12121236
@@ -1216,7 +1240,7 @@ function print_counts(ts::DefaultTestSet, depth, align,
12161240 for t in ts. results
12171241 if isa (t, DefaultTestSet)
12181242 print_counts (t, depth + 1 , align,
1219- pass_width, fail_width, error_width, broken_width, total_width)
1243+ pass_width, fail_width, error_width, broken_width, total_width, duration_width, ts . showtiming )
12201244 end
12211245 end
12221246 end
@@ -1256,8 +1280,11 @@ along with a summary of the test results.
12561280Any custom testset type (subtype of `AbstractTestSet`) can be given and it will
12571281also be used for any nested `@testset` invocations. The given options are only
12581282applied to the test set where they are given. The default test set type
1259- accepts the `verbose` boolean option: if `true`, the result summary of the
1260- nested testsets is shown even when they all pass (the default is `false`).
1283+ accepts two boolean options:
1284+ - `verbose`: if `true`, the result summary of the nested testsets is shown even
1285+ when they all pass (the default is `false`).
1286+ - `showtiming`: if `true`, the duration of each displayed testset is shown
1287+ (the default is `true`).
12611288
12621289The description string accepts interpolation from the loop indices.
12631290If no description is provided, one is constructed based on the variables.
@@ -1278,16 +1305,16 @@ re-arrangements of `@testset`s regardless of their side-effect on the
12781305global RNG state.
12791306
12801307# Examples
1281- ```jldoctest
1308+ ```jldoctest; filter = r"trigonometric identities | 4 4 [0-9 \\ .]+s"
12821309julia> @testset "trigonometric identities" begin
12831310 θ = 2/3*π
12841311 @test sin(-θ) ≈ -sin(θ)
12851312 @test cos(-θ) ≈ cos(θ)
12861313 @test sin(2θ) ≈ 2*sin(θ)*cos(θ)
12871314 @test cos(2θ) ≈ cos(θ)^2 - sin(θ)^2
12881315 end;
1289- Test Summary: | Pass Total
1290- trigonometric identities | 4 4
1316+ Test Summary: | Pass Total Time
1317+ trigonometric identities | 4 4 0.2s
12911318```
12921319"""
12931320macro testset (args... )
0 commit comments