There are 2 major styles of specifications with specs2:
unit specifications where the specification text is interleaved with the specification code. It is generally used to
specify a single class
acceptance specifications where all the specification text stands as one and the implementation code is elsewhere.
It is generally used for acceptance or integration scenarios
Unit specifications extend the org.specs2.mutable.Specification
trait and are using the should/in
format:
import org.specs2.mutable._
class HelloWorldSpec extends Specification {
"The 'Hello world' string" should {
"contain 11 characters" in {
"Hello world" must have size(11)
}
"start with 'Hello'" in {
"Hello world" must startWith("Hello")
}
"end with 'world'" in {
"Hello world" must endWith("world")
}
}
}
Acceptance specifications extend the org.specs2.Specification
trait and must define a method called is
:
import org.specs2._
class HelloWorldSpec extends Specification { def is = s2"""
This is a specification to check the 'Hello world' string
The 'Hello world' string should
contain 11 characters $e1
start with 'Hello' $e2
end with 'world' $e3
"""
def e1 = "Hello world" must have size(11)
def e2 = "Hello world" must startWith("Hello")
def e3 = "Hello world" must endWith("world")
}
The is
method lists specification fragments which can be interpolated from a s2
String where you can inject:
Result
And this is it! Now to execute your specification, you use a runner which will display the results:
> scala -cp ... specs2.run HelloWorldSpec
HelloWorldSpec
This is a specification to check the 'Hello world' string
The 'Hello world' string should
+ contain 11 characters
+ start with 'Hello'
+ end with 'world'
Total for specification HelloWorldSpec
Finished in 0 second, 58 ms
3 examples, 0 failure, 0 error
You can explore the rest of this User Guide to learn how to:
Total for specification QuickStart | |
---|---|
Finished in | 49 ms |
Results | 6 examples, 0 failure, 0 error |