From 2bf5c17bee3f4e88c411e3cef10d7a7931928fd6 Mon Sep 17 00:00:00 2001 From: Louis Date: Wed, 25 Dec 2024 09:31:33 +0100 Subject: [PATCH 1/4] alcotest: reraise Skip --- alcotest/junit_alcotest.ml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/alcotest/junit_alcotest.ml b/alcotest/junit_alcotest.ml index a5b99ed..a574888 100644 --- a/alcotest/junit_alcotest.ml +++ b/alcotest/junit_alcotest.ml @@ -30,8 +30,9 @@ let wrap_test ?classname handle_result (name, s, test) = exn_msg |> handle_result; raise exn - | Alcotest_engine__Core.Skip -> - Junit.Testcase.skipped ~name ~classname ~time:0. |> handle_result + | Alcotest_engine__Core.Skip as exn -> + Junit.Testcase.skipped ~name ~classname ~time:0. |> handle_result; + raise exn | exn -> let exn_msg = Printexc.to_string exn in Junit.Testcase.error From d17151d0fc139e42d710efbfc51ac94ec4aff262 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Louis=20Roch=C3=A9?= Date: Wed, 25 Dec 2024 12:32:17 +0100 Subject: [PATCH 2/4] alcotest: reraise exn --- alcotest/junit_alcotest.ml | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/alcotest/junit_alcotest.ml b/alcotest/junit_alcotest.ml index a574888..1cd1d31 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,10 +31,10 @@ let wrap_test ?classname handle_result (name, s, test) = ~message:"test failed" exn_msg |> handle_result; - raise exn + reraise exn | Alcotest_engine__Core.Skip as exn -> Junit.Testcase.skipped ~name ~classname ~time:0. |> handle_result; - raise exn + reraise exn | exn -> let exn_msg = Printexc.to_string exn in Junit.Testcase.error @@ -43,7 +45,7 @@ let wrap_test ?classname handle_result (name, s, test) = ~message:"test crashed" exn_msg |> handle_result; - raise exn + reraise exn in name, s, test ;; @@ -65,7 +67,7 @@ let run_and_report ?(and_exit = true) ?package ?timestamp ?argv name tests = run ?argv 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 ;; From 270100f0be307d5cbf0209389f3224e5ae502990 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Louis=20Roch=C3=A9?= Date: Wed, 25 Dec 2024 12:46:39 +0100 Subject: [PATCH 3/4] alcotest: expose run arguments --- alcotest/junit_alcotest.ml | 43 +++++++++++++++++++++++--- alcotest/junit_alcotest.mli | 27 ++++++---------- alcotest/test/alcotest_report.expected | 4 +-- junit_alcotest.opam | 2 +- 4 files changed, 52 insertions(+), 24 deletions(-) diff --git a/alcotest/junit_alcotest.ml b/alcotest/junit_alcotest.ml index 1cd1d31..200ada9 100644 --- a/alcotest/junit_alcotest.ml +++ b/alcotest/junit_alcotest.ml @@ -50,9 +50,27 @@ let wrap_test ?classname handle_result (name, s, test) = 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 = @@ -64,7 +82,24 @@ 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 as exn -> fun () -> if and_exit then exit 1 else reraise exn 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..072574f 100644 --- a/alcotest/test/alcotest_report.expected +++ b/alcotest/test/alcotest_report.expected @@ -1,12 +1,12 @@ Invalid_argument("7")Alcotest assertion failure -File "alcotest/junit_alcotest.ml", line 20, character 6: +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: +File "alcotest/junit_alcotest.ml", line 22, character 6: FAIL string_of_int equals to '7' Expected: `"7"' 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} ] From 67cb3e7c4d7f1038b6a095ba4d73b294142ab54e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Louis=20Roch=C3=A9?= Date: Wed, 25 Dec 2024 12:50:06 +0100 Subject: [PATCH 4/4] alcotest: add skipped test --- alcotest/test/alcotest_report.expected | 6 +++--- alcotest/test/alcotest_report.ml | 1 + 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/alcotest/test/alcotest_report.expected b/alcotest/test/alcotest_report.expected index 072574f..e9c88a3 100644 --- a/alcotest/test/alcotest_report.expected +++ b/alcotest/test/alcotest_report.expected @@ -1,15 +1,15 @@ -Invalid_argument("7")Alcotest assertion failure +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 +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 ()) ] ;;