Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add time element to JUnit Reporter & docs #305

Merged
merged 1 commit into from
Nov 10, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions docs/content/reporting.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,13 @@ open reporters
reporter <- new TeamCityReporter() :> IReporter

//screenshot is TODO

(**
JUnit Reporter
-----------------
Produces test results in basic JUnit format. Compatible with CircleCI.
*)

open configuration
open reporters
reporter <- new JUnitReporter("./TestResults.xml") :> IReporter
26 changes: 19 additions & 7 deletions src/canopy/reporters.fs
Original file line number Diff line number Diff line change
Expand Up @@ -385,8 +385,10 @@ type JUnitReporter(resultFilePath:string) =

let consoleReporter : IReporter = new ConsoleReporter() :> IReporter

let passedTests = ResizeArray<_>()
let failedTests = ResizeArray<_>()
let testStopWatch = System.Diagnostics.Stopwatch()
let testTimes = ResizeArray<string * float>()
let passedTests = ResizeArray<string>()
let failedTests = ResizeArray<Exception * string>()

interface IReporter with

Expand All @@ -409,15 +411,19 @@ type JUnitReporter(resultFilePath:string) =

member this.summary minutes seconds passed failed skipped =
consoleReporter.summary minutes seconds passed failed skipped
let testCount = passed + failed
let getTestTime test =
testTimes.Find(fun (t, _) -> test = t) |> snd
let passedTestsXml =
passedTests
|> Seq.map(sprintf "<testcase name=\"%s\" />")
|> Seq.map(fun id -> sprintf "<testcase name=\"%s\" time=\"%.3f\"/>" id (getTestTime id))
let failedTestsXml =
failedTests
|> Seq.map(fun (ex, id) -> sprintf "<testcase name=\"%s\"><failure>%s</failure></testcase>" id ex.Message)
|> Seq.map(fun (ex, id) -> sprintf "<testcase name=\"%s\" time=\"%.3f\"><failure>%s</failure></testcase>" id (getTestTime id) ex.Message)
let testCount = passed + failed
let testTimeSum = testTimes |> Seq.sumBy snd
let allTestsXml = String.Join(String.Empty, Seq.concat [passedTestsXml; failedTestsXml])
let xml = sprintf "<testsuite tests=\"%i\">%s</testsuite>" testCount allTestsXml
let xml =
sprintf "<testsuite tests=\"%i\" time=\"%.3f\">%s</testsuite>" testCount testTimeSum allTestsXml
let resultFile = System.IO.FileInfo(resultFilePath)
resultFile.Directory.Create()
consoleReporter.write <| sprintf "Saving results to %s" resultFilePath
Expand All @@ -434,8 +440,14 @@ type JUnitReporter(resultFilePath:string) =

member this.testStart id =
consoleReporter.testStart id
testStopWatch.Reset()
testStopWatch.Start()

member this.testEnd id =
testStopWatch.Stop()
let elapsedSeconds = float testStopWatch.ElapsedMilliseconds / 1000.
testTimes.Add(id, elapsedSeconds)

member this.testEnd id = ()
member this.quit () = ()
member this.suiteBegin () = ()
member this.suiteEnd () = ()
Expand Down