Specs is focussed on BDD testing (Behaviour Driven Design) and is the inspiration for ScalaTests WordSpec. From what I understand Specs was in-turn inspired by RSpec the Ruby BDD testing framework.
Specs has some truly unique features to it which we will encounter in future topics. But for now just the basics. The following example tests a couple of Scala's XML support in order to demonstract the general pattern followed when writing Specs' tests.
- # scala -classpath ~/.m2/repository/org/scala-tools/testing/specs/1.6.1/specs-1.6.1.jar
- scala> import org.specs._
- import org.specs._
- scala> object XmlSpec extends Specification {
- | val sample = <library>
- | <videos>
- | <video type="dvd">Seven</video>
- | <video type="blue-ray">The fifth element</video>
- | <video type="hardcover">Gardens of the moon</video>
- | </videos>
- | <books>
- | <book type="softcover">Memories of Ice</book>
- | </books>
- | </library>
- | "Scala XML" should {
- | "allow xpath-like selection" in {
- | (sample \\ "video").size must be (3)
- | }
- | "select child nodes" in {
- | // This test fails because child is a sequence not a string
- | // See the results of the tests
- | sample.child must contain (<videos/>)
- | }
- | }
- | }
- defined module XmlSpec
- scala> XmlSpec.main(Array[String]())
- Specification "XmlSpec"
- Scala XML should
- + allow xpath-like selection
- x select child nodes <-- x indicates failure.
- 'ArrayBuffer(
- , <videos>
- <video type="dvd">Seven</video>
- <video type="blue-ray">The fifth element</video>
- <video type="hardcover">Gardens of the moon</video>
- </videos>,
- , <books>
- <book type="softcover">Memories of Ice</book>
- </books>,
- )' doesn't contain '<videos></videos>' (< console>:24)
- Total for specification "XmlSpec":
- Finished in 0 second, 52 ms
- 2 examples, 2 expectations, 1 failure, 0 error
cheers mate, you saved me 1 day :)
ReplyDelete