Skip to content

Commit

Permalink
Merge pull request #278 from 0x53A/testresult-junit
Browse files Browse the repository at this point in the history
add JUnit-like summary file
  • Loading branch information
haf authored Sep 14, 2018
2 parents d9cf529 + 6ed94ab commit f0a1533
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 1 deletion.
6 changes: 5 additions & 1 deletion Expecto.TestResults/CSharp.fs
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,8 @@ module ConfigExt =

[<Extension; CompiledName("AddNUnitSummary")>]
member x.AddNUnitSummary(file:string, assemblyName:string) =
x.appendSummaryHandler (TestResults.writeNUnitSummary(file, assemblyName) )
x.appendSummaryHandler (TestResults.writeNUnitSummary(file, assemblyName) )

[<Extension; CompiledName("AddJUnitSummary")>]
member x.AddJUnitSummary(file:string, assemblyName:string) =
x.appendSummaryHandler (TestResults.writeJUnitSummary(file, assemblyName) )
63 changes: 63 additions & 0 deletions Expecto.TestResults/Library.fs
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,66 @@ let writeNUnitSummary (file, assemblyName) (summary: Impl.TestRunSummary) =
|> Directory.CreateDirectory
|> ignore
doc.Save(path)

let writeJUnitSummary (file, assemblyName) (summary: Impl.TestRunSummary) =
// junit does not have an official xml spec
// this is a minimal implementation to get gitlab to recognize the tests: https://docs.gitlab.com/ee/ci/junit_test_reports.html
let totalTests = summary.errored @ summary.failed @ summary.ignored @ summary.passed
let testCaseElements =
totalTests
|> Seq.map (fun (flatTest, test) ->
let content =
match test.result with
| Impl.TestResult.Passed ->
[|
XAttribute(XName.Get "time",
System.String.Format(System.Globalization.CultureInfo.InvariantCulture,
"{0:0.000}", test.duration.TotalSeconds)) |> box
|]
| Impl.TestResult.Error e ->
[|
XAttribute(XName.Get "time",
System.String.Format(System.Globalization.CultureInfo.InvariantCulture,
"{0:0.000}", test.duration.TotalSeconds)) |> box
XElement(XName.Get "error",
[|
XAttribute(XName.Get "message", e.Message) |> box
XText(e.ToString()) |> box
|]) |> box
|]
| Impl.TestResult.Failed msg ->
[|
XAttribute(XName.Get "time",
System.String.Format(System.Globalization.CultureInfo.InvariantCulture,
"{0:0.000}", test.duration.TotalSeconds)) |> box
XElement(XName.Get "failure",
XAttribute(XName.Get "message", msg))
|]
| Impl.TestResult.Ignored msg ->
[|
XElement(XName.Get "skipped",
XAttribute(XName.Get "message", msg)) |> box
|]

XElement(XName.Get "testcase",
[|
yield XAttribute(XName.Get "name", flatTest.name) |> box
yield! content
|]) |> box)
let element =
XElement(
XName.Get "testsuites",
[|
yield XElement(XName.Get "testsuite",
[|
yield XAttribute(XName.Get "name", assemblyName) |> box
yield! testCaseElements
|]) |> box
|])

let doc = XDocument([|element|])
let path = Path.GetFullPath file
Path.GetDirectoryName path
|> Directory.CreateDirectory
|> ignore
doc.Save(path)

0 comments on commit f0a1533

Please sign in to comment.