Skip to content

Instantly share code, notes, and snippets.

@whysoserious
Created February 17, 2014 10:13
Show Gist options
  • Save whysoserious/9048036 to your computer and use it in GitHub Desktop.
Save whysoserious/9048036 to your computer and use it in GitHub Desktop.
Specs2 use cases
package org.jz.s2ue
import org.specs2.mutable.Specification
class SkipSpec extends Specification {
skipAllUnless(isServerStarted && isDbAvailable)
"This test should not start" >> {
ok
}
private def isServerStarted: Boolean = true
private def isDbAvailable: Boolean = false
}
package org.jz.s2ue
import org.specs2.execute.{Result, ResultExecution, AsResult}
import org.specs2.specification.{AroundContextExample, Around}
import org.specs2.time.SimpleTimer
import org.specs2.Specification
class Timed extends Around {
def around[T: AsResult](t: => T): Result = {
val (result, timer) = withTimer(ResultExecution.execute(AsResult(t)))
result.updateExpected("Execution time: " + timer.time)
}
def withTimer[T](t: => T): (T, SimpleTimer) = {
val timer = (new SimpleTimer).start
val result = t
(result, timer.stop)
}
}
class TimeOfExecutionSpec extends Specification with AroundContextExample[Timed] { def is = s2"""
This specification should
Display execution time of this test: $e1
And of this one: $e2
This one doesn't pass so we don't know the execution time $e3
"""
override protected def aroundContext: Timed = new Timed
def e1 = {
ok
}
def e2 = {
Thread.sleep(500)
ok
}
def e3 = {
Thread.sleep(100)
ko
}.pendingUntilFixed("[Fix me!]")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment