Skip to content

Commit 91ae620

Browse files
Surface test error if running testitem directly (#178)
* Surface test error if running testitem directly So scripts doing this would return non-zero exit code * Add test * Update test/testfiles/_direct_testitem.jl
1 parent d624548 commit 91ae620

File tree

4 files changed

+27
-6
lines changed

4 files changed

+27
-6
lines changed

src/ReTestItems.jl

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -919,7 +919,7 @@ end
919919
# when `runtestitem` called directly or `@testitem` called outside of `runtests`.
920920
function runtestitem(
921921
ti::TestItem, ctx::TestContext;
922-
test_end_expr::Union{Nothing,Expr}=nothing, logs::Symbol=:eager, verbose_results::Bool=true, finish_test::Bool=true,
922+
test_end_expr::Union{Nothing,Expr}=nothing, logs::Symbol=:eager, verbose_results::Bool=true, finish_test::Bool=true, catch_test_error::Bool=true,
923923
)
924924
if should_skip(ti)::Bool
925925
return skiptestitem(ti, ctx; verbose_results)
@@ -1017,10 +1017,16 @@ function runtestitem(
10171017
ts1 = Test.pop_testset()
10181018
task_local_storage()[:__TESTITEM_ACTIVE__] = prev
10191019
@assert ts1 === ts
1020-
try
1021-
finish_test && Test.finish(ts) # This will throw an exception if any of the tests failed.
1022-
catch e
1023-
e isa TestSetException || rethrow()
1020+
if finish_test
1021+
if catch_test_error
1022+
try
1023+
Test.finish(ts) # This will throw an exception if any of the tests failed.
1024+
catch e
1025+
e isa TestSetException || rethrow()
1026+
end
1027+
else
1028+
Test.finish(ts)
1029+
end
10241030
end
10251031
end
10261032
@debugv 1 "Test item $(repr(name)) done$(_on_worker())."

src/macros.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ macro testitem(nm, exs...)
320320
$store_test_item($ti)
321321
$ti
322322
else # We are not in a `runtests` call, so we run the testitem immediately.
323-
$runtestitem($ti)
323+
$runtestitem($ti; catch_test_error=false)
324324
$nothing
325325
end
326326
end

test/integrationtests.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1280,4 +1280,12 @@ end
12801280
end
12811281
end
12821282

1283+
# see https://github.com/JuliaTesting/ReTestItems.jl/issues/177
1284+
@testset "error code from running `@testitem` directly" begin
1285+
filename = joinpath(TEST_FILES_DIR, "_direct_testitem.jl")
1286+
cmd = `$(Base.julia_cmd()) --project $filename`
1287+
p = run(pipeline(ignorestatus(cmd); stdout, stderr), wait=true)
1288+
@test !success(p)
1289+
end
1290+
12831291
end # integrationtests.jl testset

test/testfiles/_direct_testitem.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# This file is incorrect usage of ReTestItems
2+
# but we want to check that a failed `@testitem` used directly in a script
3+
# means the script exits with a non-zero exit code.
4+
using ReTestItems
5+
@testitem "should fail" begin
6+
@test 1 == 2
7+
end

0 commit comments

Comments
 (0)