diff --git a/alcotest/junit_alcotest.ml b/alcotest/junit_alcotest.ml index a5b99ed..200ada9 100644 --- a/alcotest/junit_alcotest.ml +++ b/alcotest/junit_alcotest.ml @@ -1,5 +1,7 @@ module A = Alcotest +external reraise : exn -> 'a = "%reraise" + type exit = unit -> unit let push l v = l := v :: !l @@ -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 @@ -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 = @@ -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 ;; diff --git a/alcotest/junit_alcotest.mli b/alcotest/junit_alcotest.mli index 515bfa7..72b6243 100644 --- a/alcotest/junit_alcotest.mli +++ b/alcotest/junit_alcotest.mli @@ -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 @@ -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 @@ -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 diff --git a/alcotest/test/alcotest_report.expected b/alcotest/test/alcotest_report.expected index cd0c37c..e9c88a3 100644 --- a/alcotest/test/alcotest_report.expected +++ b/alcotest/test/alcotest_report.expected @@ -1,15 +1,15 @@ -Invalid_argument("7")Alcotest assertion failure -File "alcotest/junit_alcotest.ml", line 20, character 6: +Invalid_argument("7")Alcotest assertion failure +File "alcotest/junit_alcotest.ml", line 22, character 6: FAIL string_of_int equals to '7' Expected: `"7"' Received: `"8"' -Invalid_argument("7")Alcotest assertion failure -File "alcotest/junit_alcotest.ml", line 20, character 6: +Invalid_argument("7")Alcotest assertion failure +File "alcotest/junit_alcotest.ml", line 22, character 6: FAIL string_of_int equals to '7' Expected: `"7"' Received: `"8"' - + diff --git a/alcotest/test/alcotest_report.ml b/alcotest/test/alcotest_report.ml index d8fab98..6bd449a 100644 --- a/alcotest/test/alcotest_report.ml +++ b/alcotest/test/alcotest_report.ml @@ -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 ()) ] ;; diff --git a/junit_alcotest.opam b/junit_alcotest.opam index f8aa7d2..1f636b6 100644 --- a/junit_alcotest.opam +++ b/junit_alcotest.opam @@ -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} ]