Knowing that an example passed is fine but sometimes you want to display more information, like the time spent executing the example for instance.
This can be done by creating a Context
object which will update the Result
of the example execution with whatever you want to display:
import org.specs2._
import time._
import specification._
import execute._
trait Timed extends Around {
def around[T : AsResult](t: =>T): Result = {
// use `ResultExecution.execute` to catch possible exceptions
val (result, timer) = withTimer(ResultExecution.execute(AsResult(t)))
// update the result with a piece of text which will be displayed in the console
result.updateExpected("Execution time: "+timer.time)
}
/** mesure the execution time of a piece of code */
def withTimer[T](t: =>T): (T, SimpleTimer) = {
val timer = (new SimpleTimer).start
val result = t
(result, timer.stop)
}
}
When you execute a specification where each example uses this Around
context (by implementing the AroundExampleContext
trait for example) you should see the timing of each example displayed in the console:
[info] TimedExecutionSpecification
[info]
[info] + example 1
[info] Execution time: 94 ms
[info] + example 2
[info] Execution time: 11 ms
Nice one Eric!
Diego
Sent from my android cell
--
You received this message because you are subscribed to the Google Groups "specs2-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to specs2-users...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.