Skip to content

Commit

Permalink
Merge pull request #305 from blair55/master
Browse files Browse the repository at this point in the history
Add time element to JUnit Reporter & docs
  • Loading branch information
lefthandedgoat authored Nov 10, 2016
2 parents 932fdcc + 794ce4c commit 282f31f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
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

0 comments on commit 282f31f

Please sign in to comment.