Skip to content

Commit

Permalink
Merge pull request #217 from LimoWanKenobi/add_test_performance
Browse files Browse the repository at this point in the history
Improves performance of addition of contexts and addition of tests
  • Loading branch information
lefthandedgoat committed Oct 18, 2015
2 parents e461ac3 + b902913 commit 0c5bf55
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 16 deletions.
37 changes: 22 additions & 15 deletions src/canopy/runner.fs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ open reporters
open types
open OpenQA.Selenium

let rec private last = function
| hd :: [] -> hd
| hd :: tl -> last tl
| _ -> failwith "Empty list."
let private last = function
| hd :: tl -> hd
| [] -> failwith "Empty list."

let mutable suites = [new suite()]
let mutable todo = fun () -> ()
Expand All @@ -26,30 +25,30 @@ let context c =
else
let s = new suite()
s.Context <- c
suites <- suites @ [s]
suites <- s::suites
let private incrementLastTestSuite () =
let lastSuite = last suites
lastSuite.TotalTestsCount <- lastSuite.TotalTestsCount + 1
lastSuite
let ( &&& ) description f =
let lastSuite = incrementLastTestSuite()
lastSuite.Tests <- lastSuite.Tests @ [Test(description, f, lastSuite.TotalTestsCount)]
lastSuite.Tests <- Test(description, f, lastSuite.TotalTestsCount)::lastSuite.Tests
let test f = null &&& f
let ntest description f = description &&& f
let ( &&&& ) description f =
let lastSuite = incrementLastTestSuite()
lastSuite.Wips <- lastSuite.Wips @ [Test(description, f, lastSuite.TotalTestsCount)]
lastSuite.Wips <- Test(description, f, lastSuite.TotalTestsCount)::lastSuite.Wips
let wip f = null &&&& f
let many count f =
let lastSuite = incrementLastTestSuite()
[1 .. count] |> List.iter (fun _ -> lastSuite.Manys <- lastSuite.Manys @ [Test(null, f, lastSuite.TotalTestsCount)])
[1 .. count] |> List.iter (fun _ -> lastSuite.Manys <- Test(null, f, lastSuite.TotalTestsCount)::lastSuite.Manys)
let ( &&! ) description f =
let lastSuite = incrementLastTestSuite()
lastSuite.Tests <- lastSuite.Tests @ [Test(description, skipped, lastSuite.TotalTestsCount)]
lastSuite.Tests <- Test(description, skipped, lastSuite.TotalTestsCount)::lastSuite.Tests
let xtest f = null &&! f
let ( &&&&& ) description f =
let lastSuite = incrementLastTestSuite()
lastSuite.Always <- lastSuite.Always @ [Test(description, f, lastSuite.TotalTestsCount)]
lastSuite.Always <- Test(description, f, lastSuite.TotalTestsCount)::lastSuite.Always
let mutable passedCount = 0
let mutable skippedCount = 0
let mutable failedCount = 0
Expand Down Expand Up @@ -88,8 +87,12 @@ let failSuite (ex: Exception) (suite : suite) =
let reportFailedTest (ex: Exception) (test : Test) =
reporter.testStart test.Id
fail ex test.Id <| safelyGetUrl()
reporter.testEnd test.Id
suite.Tests |> List.iter (fun test -> reportFailedTest ex test)
reporter.testEnd test.Id

// tests are in reverse order and have to be reversed first
do suite.Tests
|> List.rev
|> List.iter (fun test -> reportFailedTest ex test)

let run () =
reporter.suiteBegin()
Expand Down Expand Up @@ -120,6 +123,9 @@ let run () =
reporter.testEnd test.Id

failureMessage <- null

// suites list is in reverse order and have to be reversed before running the tests
suites <- List.rev suites

//run all the suites
if runFailedContextsFirst = true then
Expand Down Expand Up @@ -149,13 +155,13 @@ let run () =
tests |> List.iter (fun w -> runtest s w)
wipTest <- false
else if s.Manys.IsEmpty = false then
s.Manys |> List.iter (fun m -> runtest s m)
s.Manys |> List.rev |> List.iter (fun m -> runtest s m)
else
let tests = s.Tests @ s.Always |> List.sortBy (fun t -> t.Number)
tests |> List.iter (fun t -> runtest s t)
s.Lastly ()

if contextFailed = true then failedContexts <- failedContexts @ [s.Context]
if contextFailed = true then failedContexts <- s.Context::failedContexts
if s.Context <> null then reporter.contextEnd s.Context
)

Expand All @@ -166,7 +172,8 @@ let run () =
reporter.suiteEnd()

let runFor browsers =
let currentSuites = suites
// suites are in reverse order and have to be reversed before running the tests
let currentSuites = suites |> List.rev
suites <- [new suite()]
match box browsers with
| :? (types.BrowserStartMode list) as browsers ->
Expand Down
15 changes: 14 additions & 1 deletion tests/basictests/Program.fs
Original file line number Diff line number Diff line change
Expand Up @@ -699,11 +699,24 @@ context "todo tests"

"write a test that tests the whole internet!" &&& todo

let createTest k =
let testName = sprintf "Testing addition performance %i" k
testName &&& todo

let createTestSuite contextName n =
context contextName

[1..n] |> Seq.iter createTest

start chrome

createTestSuite "Add test performance" 1000

run ()

switchTo mainBrowser
coverage testpage
coverage()
coverage "http://scrumy.com/silenter39delayed"

quit()
quit()

0 comments on commit 0c5bf55

Please sign in to comment.