Skip to content

Commit

Permalink
Merge pull request #12 from Khady/Khady-patch-1
Browse files Browse the repository at this point in the history
alcotest: reraise Skip
  • Loading branch information
Khady authored Dec 25, 2024
2 parents 914f37d + 67cb3e7 commit c0149de
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 32 deletions.
56 changes: 47 additions & 9 deletions alcotest/junit_alcotest.ml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module A = Alcotest

external reraise : exn -> 'a = "%reraise"

type exit = unit -> unit

let push l v = l := v :: !l
Expand Down Expand Up @@ -29,9 +31,10 @@ let wrap_test ?classname handle_result (name, s, test) =
~message:"test failed"
exn_msg
|> handle_result;
raise exn
| Alcotest_engine__Core.Skip ->
Junit.Testcase.skipped ~name ~classname ~time:0. |> handle_result
reraise exn
| Alcotest_engine__Core.Skip as exn ->
Junit.Testcase.skipped ~name ~classname ~time:0. |> handle_result;
reraise exn
| exn ->
let exn_msg = Printexc.to_string exn in
Junit.Testcase.error
Expand All @@ -42,14 +45,32 @@ let wrap_test ?classname handle_result (name, s, test) =
~message:"test crashed"
exn_msg
|> handle_result;
raise exn
reraise exn
in
name, s, test
;;

let run ?argv name tl = A.run ~and_exit:false ?argv name tl

let run_and_report ?(and_exit = true) ?package ?timestamp ?argv name tests =
let run_and_report
?stdout
?stderr
?(and_exit = true)
?verbose
?compact
?tail_errors
?quick_only
?show_errors
?json
?filter
?log_dir
?bail
?record_backtrace
?ci
?package
?timestamp
?argv
name
tests
=
let testcases = ref [] in
let testsuite = Junit.Testsuite.make ?package ?timestamp ~name () in
let tests =
Expand All @@ -61,10 +82,27 @@ let run_and_report ?(and_exit = true) ?package ?timestamp ?argv name tests =
in
let exit =
try
run ?argv name tests;
A.run
?stdout
?stderr
?verbose
?compact
?tail_errors
?quick_only
?show_errors
?json
?filter
?log_dir
?bail
?record_backtrace
?ci
?argv
~and_exit:false
name
tests;
fun () -> if and_exit then exit 0 else ()
with
| A.Test_error -> fun () -> if and_exit then exit 1 else raise A.Test_error
| A.Test_error as exn -> fun () -> if and_exit then exit 1 else reraise exn
in
Junit.Testsuite.add_testcases !testcases testsuite, exit
;;
27 changes: 10 additions & 17 deletions alcotest/junit_alcotest.mli
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
(** [wrap_test handle_result test_cases] wraps test cases to create
Junit testcases and pass them to [handle_result].
Can be used with {!run} to create customized Junit testsuites if
Can be used with {!Alcotest.run} to create customized Junit testsuites if
the output of {!run_and_report} is not as expected.
@param classname
Expand All @@ -21,20 +21,13 @@ val wrap_test
-> unit Alcotest.test_case
-> unit Alcotest.test_case

(** [run ?argv n t] is a wrapper around {!Alcotest.run}, only setting
[and_exit] to false. It is mandatory to be able to process results
after the end of the run.
Low level function. It is easier to use {!run_and_report}. *)
val run : ?argv:string array -> string -> unit Alcotest.test list -> unit

(** [exit ()] exits with appropriate code if {!run_and_report}'s
[and_exit] was [true] or raise {!Alcotest.Test_error} in case of
error. *)
type exit = unit -> unit

(** [run_and_report name tests] is a wrapper around {!run} and {!wrap_test}. It
runs the tests and creates a Junit testsuite from the results.
(** [run_and_report name tests] is a wrapper around {!Alcotest.run} and {!wrap_test}.
It runs the tests and creates a Junit testsuite from the results.
As {!Alcotest.run} is always called with [and_exit = false] to be
able to produce a report, the behavior is emulated by the returned
Expand All @@ -49,10 +42,10 @@ type exit = unit -> unit
[?argv] is forwarded to {!run}. [?package] and [?timestamp] are
forwarded to {!Junit.Testsuite.make}. *)
val run_and_report
: ?and_exit:bool
-> ?package:string
-> ?timestamp:Ptime.t
-> ?argv:string array
-> string
-> (string * unit Alcotest.test_case list) list
-> Junit.Testsuite.t * exit
: (?package:string
-> ?timestamp:Ptime.t
-> ?argv:string array
-> string
-> (string * unit Alcotest.test_case list) list
-> Junit.Testsuite.t * exit)
Alcotest.with_options
10 changes: 5 additions & 5 deletions alcotest/test/alcotest_report.expected
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<testsuites><testsuite package="junit_alcotest" id="0" name="Skip test suite" timestamp="2013-05-24T10:23:58" hostname="localhost" tests="2" failures="0" errors="0" skipped="2" time="0"><properties></properties><testcase name="Skipped quick" classname="Skip test suite.Skipped tests" time="0"><skipped></skipped></testcase><testcase name="Skipped slow" classname="Skip test suite.Skipped tests" time="0"><skipped></skipped></testcase></testsuite><testsuite package="junit_alcotest" id="1" name="My first test" timestamp="2013-05-24T10:23:58" hostname="localhost" tests="4" failures="0" errors="2" skipped="0" time="0"><properties></properties><testcase name="Test with unexpected exception" classname="My first test.Basic tests" time="0"><error message="test crashed" type="exception raised">Invalid_argument(&quot;7&quot;)</error></testcase><testcase name="Capitalize" classname="My first test.Basic tests" time="0"></testcase><testcase name="Add entries" classname="My first test.Basic tests" time="0"></testcase><testcase name="Test with wrong result" classname="My first test.Basic tests" time="0"><error message="test crashed" type="exception raised">Alcotest assertion failure
&#27;[1mFile &quot;alcotest/junit_alcotest.ml&quot;, line 20, character 6:
<testsuites><testsuite package="junit_alcotest" id="0" name="Skip test suite" timestamp="2013-05-24T10:23:58" hostname="localhost" tests="2" failures="0" errors="0" skipped="2" time="0"><properties></properties><testcase name="Skipped quick" classname="Skip test suite.Skipped tests" time="0"><skipped></skipped></testcase><testcase name="Skipped slow" classname="Skip test suite.Skipped tests" time="0"><skipped></skipped></testcase></testsuite><testsuite package="junit_alcotest" id="1" name="My first test" timestamp="2013-05-24T10:23:58" hostname="localhost" tests="5" failures="0" errors="2" skipped="1" time="0"><properties></properties><testcase name="Test with unexpected exception" classname="My first test.Basic tests" time="0"><error message="test crashed" type="exception raised">Invalid_argument(&quot;7&quot;)</error></testcase><testcase name="Capitalize" classname="My first test.Basic tests" time="0"></testcase><testcase name="Add entries" classname="My first test.Basic tests" time="0"></testcase><testcase name="Test with wrong result" classname="My first test.Basic tests" time="0"><error message="test crashed" type="exception raised">Alcotest assertion failure
&#27;[1mFile &quot;alcotest/junit_alcotest.ml&quot;, line 22, character 6:
&#27;[0m&#27;[31mFAIL&#27;[0m string_of_int equals to '7'

Expected: `&#27;[32m&quot;7&quot;&#27;[0m'
Received: `&#27;[31m&quot;8&quot;&#27;[0m'

</error></testcase></testsuite><testsuite package="junit_alcotest" id="2" name="My second test" timestamp="2013-05-24T10:23:58" hostname="localhost" tests="4" failures="0" errors="2" skipped="0" time="0"><properties></properties><testcase name="Test with unexpected exception" classname="My second test.Basic tests" time="0"><error message="test crashed" type="exception raised">Invalid_argument(&quot;7&quot;)</error></testcase><testcase name="Capitalize" classname="My second test.Basic tests" time="0"></testcase><testcase name="Add entries" classname="My second test.Basic tests" time="0"></testcase><testcase name="Test with wrong result" classname="My second test.Basic tests" time="0"><error message="test crashed" type="exception raised">Alcotest assertion failure
&#27;[1mFile &quot;alcotest/junit_alcotest.ml&quot;, line 20, character 6:
</error></testcase><testcase name="Test skipped" classname="My first test.Basic tests" time="0"><skipped></skipped></testcase></testsuite><testsuite package="junit_alcotest" id="2" name="My second test" timestamp="2013-05-24T10:23:58" hostname="localhost" tests="5" failures="0" errors="2" skipped="1" time="0"><properties></properties><testcase name="Test with unexpected exception" classname="My second test.Basic tests" time="0"><error message="test crashed" type="exception raised">Invalid_argument(&quot;7&quot;)</error></testcase><testcase name="Capitalize" classname="My second test.Basic tests" time="0"></testcase><testcase name="Add entries" classname="My second test.Basic tests" time="0"></testcase><testcase name="Test with wrong result" classname="My second test.Basic tests" time="0"><error message="test crashed" type="exception raised">Alcotest assertion failure
&#27;[1mFile &quot;alcotest/junit_alcotest.ml&quot;, line 22, character 6:
&#27;[0m&#27;[31mFAIL&#27;[0m string_of_int equals to '7'

Expected: `&#27;[32m&quot;7&quot;&#27;[0m'
Received: `&#27;[31m&quot;8&quot;&#27;[0m'

</error></testcase></testsuite><testsuite package="junit_alcotest" id="3" name="Success test suite" timestamp="2013-05-24T10:23:58" hostname="localhost" tests="2" failures="0" errors="0" skipped="0" time="0"><properties></properties><testcase name="Capitalize" classname="Success test suite.Good tests" time="0"></testcase><testcase name="Add entries" classname="Success test suite.Good tests" time="0"></testcase></testsuite></testsuites>
</error></testcase><testcase name="Test skipped" classname="My second test.Basic tests" time="0"><skipped></skipped></testcase></testsuite><testsuite package="junit_alcotest" id="3" name="Success test suite" timestamp="2013-05-24T10:23:58" hostname="localhost" tests="2" failures="0" errors="0" skipped="0" time="0"><properties></properties><testcase name="Capitalize" classname="Success test suite.Good tests" time="0"></testcase><testcase name="Add entries" classname="Success test suite.Good tests" time="0"></testcase></testsuite></testsuites>
1 change: 1 addition & 0 deletions alcotest/test/alcotest_report.ml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ let test_set =
; A.test_case "Capitalize" `Quick capit
; A.test_case "Add entries" `Slow plus
; A.test_case "Test with wrong result" `Quick wrong_result
; A.test_case "Test skipped" `Quick (fun () -> A.skip ())
]
;;

Expand Down
2 changes: 1 addition & 1 deletion junit_alcotest.opam
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ tags: ["junit" "jenkins" "alcotest"]
depends: [
"dune" {>= "3.0"}
"odoc" {with-doc & >= "1.1.1"}
"alcotest" {>= "1.7.0"}
"alcotest" {>= "1.8.0"}
"junit" {= version}
"ocamlformat" {= "0.27.0" & with-dev-setup}
]
Expand Down

0 comments on commit c0149de

Please sign in to comment.