Skip to content

Commit

Permalink
Merge pull request #2 from mjambon/more-things
Browse files Browse the repository at this point in the history
Fix Jenkins rendering by setting 'classname'
  • Loading branch information
Khady authored Jan 21, 2020
2 parents a378517 + b9c1797 commit 5e28acc
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
21 changes: 16 additions & 5 deletions alcotest/junit_alcotest.ml
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,30 @@ type exit = unit -> unit

let push l v = l := v :: !l

let wrap_test handle_result (name, s, test) =
let wrap_test ?classname handle_result (name, s, test) =
let classname =
(* The 'classname' attribute may not be empty, and should contain a period
for best rendering in Jenkins by junit-plugin.
For example, classname="foo.bar.baz" is rendered as a package "foo.bar"
containing a class "baz" containing one or more test cases with their
own name. *)
match classname with
| None | Some "" -> name
| Some path -> path
in
let test () =
try
test ();
Junit.Testcase.pass
~name
~classname:""
~classname
~time:0.
|> handle_result
with
| Failure exn_msg as exn ->
Junit.Testcase.failure
~name
~classname:""
~classname
~time:0.
~typ:"not expected result"
~message:"test failed"
Expand All @@ -28,7 +38,7 @@ let wrap_test handle_result (name, s, test) =
let exn_msg = Printexc.to_string exn in
Junit.Testcase.error
~name
~classname:""
~classname
~time:0.
~typ:"exception raised"
~message:"test crashed"
Expand All @@ -46,7 +56,8 @@ let run_and_report ?(and_exit=true) ?package ?timestamp ?argv name tests =
let testsuite = Junit.Testsuite.make ?package ?timestamp ~name () in
let tests =
List.map (fun (title, test_set) ->
(title, List.map (wrap_test (push testcases)) test_set)
let classname = Printf.sprintf "%s.%s" name title in
(title, List.map (wrap_test ~classname (push testcases)) test_set)
) tests
in
let exit =
Expand Down
7 changes: 7 additions & 0 deletions alcotest/junit_alcotest.mli
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*)

val wrap_test :
?classname:string ->
(Junit.Testcase.t -> unit) ->
unit Alcotest.test_case ->
unit Alcotest.test_case
Expand All @@ -13,6 +14,12 @@ val wrap_test :
Can be used with {!run} to create customized Junit testsuites if
the output of {!run_and_report} is not as expected.
@param classname will populate the 'classname' attribute
for the test case. For best hierarchic rendering in Jenkins, it
should contain a period. For example, "foo.bar.baz" will be rendered
a package "foo.bar" that contains a class "baz", which contains the
current test case and others. Defaults to the name of the test case.
*)

val run: ?argv:string array -> string -> unit Alcotest.test list -> unit
Expand Down
6 changes: 3 additions & 3 deletions alcotest/test/alcotest_report.expected
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<testsuites><testsuite package="junit_alcotest" id="0" name="My first test" timestamp="2013-05-24T10:23:58" hostname="localhost" tests="4" failures="1" errors="1" time="0"><properties></properties><testcase name="Test with unexpected exception" classname="" time="0"><error message="test crashed" type="exception raised">Invalid_argument(&quot;7&quot;)</error></testcase><testcase name="Capitalize" classname="" time="0"></testcase><testcase name="Add entries" classname="" time="0"></testcase><testcase name="Test with wrong result" classname="" time="0"><failure message="test failed" type="not expected result">Error string_of_int equals to '7': expecting
<testsuites><testsuite package="junit_alcotest" id="0" name="My first test" timestamp="2013-05-24T10:23:58" hostname="localhost" tests="4" failures="1" errors="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"><failure message="test failed" type="not expected result">Error string_of_int equals to '7': expecting
7, got
8.</failure></testcase></testsuite><testsuite package="junit_alcotest" id="1" name="My second test" timestamp="2013-05-24T10:23:58" hostname="localhost" tests="4" failures="1" errors="1" time="0"><properties></properties><testcase name="Test with unexpected exception" classname="" time="0"><error message="test crashed" type="exception raised">Invalid_argument(&quot;7&quot;)</error></testcase><testcase name="Capitalize" classname="" time="0"></testcase><testcase name="Add entries" classname="" time="0"></testcase><testcase name="Test with wrong result" classname="" time="0"><failure message="test failed" type="not expected result">Error string_of_int equals to '7': expecting
8.</failure></testcase></testsuite><testsuite package="junit_alcotest" id="1" name="My second test" timestamp="2013-05-24T10:23:58" hostname="localhost" tests="4" failures="1" errors="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"><failure message="test failed" type="not expected result">Error string_of_int equals to '7': expecting
7, got
8.</failure></testcase></testsuite><testsuite package="junit_alcotest" id="2" name="Success test suite" timestamp="2013-05-24T10:23:58" hostname="localhost" tests="2" failures="0" errors="0" time="0"><properties></properties><testcase name="Capitalize" classname="" time="0"></testcase><testcase name="Add entries" classname="" time="0"></testcase></testsuite></testsuites>
8.</failure></testcase></testsuite><testsuite package="junit_alcotest" id="2" name="Success test suite" timestamp="2013-05-24T10:23:58" hostname="localhost" tests="2" failures="0" errors="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>

0 comments on commit 5e28acc

Please sign in to comment.