Showing posts with label specs. Show all posts
Showing posts with label specs. Show all posts

Friday, February 12, 2010

Specs and Fixtures/Contexts

This topic revisits the Specs BDD testing library. It is a continuation of the previous post Specs BDD Testing Framework.

This topic will look at how to setup fixtures in Specs. This is only a sample to give a feeling of Specs a much more complete guide is available on the Specs website.
  1. jeichar: git-src$ scala -classpath ~/.m2/repository/org/scala-tools/testing/specs/1.6.1/specs-1.6.1.jar
  2. scala> import org.specs._
  3. import org.specs._
  4. /*
  5. This example demonstrates before and after actions similar to what is found in XUnit.
  6. */
  7. scala> object mySpec extends Specification {           
  8.      | "my system" should {
  9.      | doFirst{println("before")}  // ran once
  10.      | 
  11.      | doBefore{println("before")} // ran for each test
  12.      | doAfter{println("after")} // ran for each test
  13.      | 
  14.      | "test spec 1" in { println("test1"); 1 must_== 1}
  15.      | "test spec 2" in { println("test2"); 1 must_== 2}
  16.      | 
  17.      | doLast{println("last")} // ran once
  18.      | }}
  19. defined module mySpec
  20. scala> mySpec.main(Array())                             
  21. Specification "mySpec"
  22.   my system should
  23. before
  24. before
  25. test1
  26. after
  27.   + test spec 1
  28. before
  29. test2
  30. after
  31. last
  32.   x test spec 2
  33.     '1' is not equal to '2' (< console>:14)
  34. Total for specification "mySpec":
  35. Finished in 0 second, 307 ms
  36. 2 examples, 2 expectations, 1 failure, 0 error

Example using Contexts there many more examples at: Shared contexts
  1. scala> import org.specs._
  2. import org.specs._
  3. /*
  4. This specification uses contexts instead of before and after actions.
  5. My personal preference is to use contexts because they are more flexible and can have names associated with them.  In addition contexts can be shared between specifications and multiple contexts can be used within a single specification.  This example is kept very simple for demonstration purposes
  6. */
  7. scala> object StackSpecification extends Specification {
  8.      |   var list : List[Int] = _
  9.      |   val empty = beforeContext(list = Nil)  
  10.      |   val nonEmpty = beforeContext(list = List(1,2,3))
  11.      |   
  12.      |   "A full stack" definedAs nonEmpty should { 
  13.      |     "size of 3" in {
  14.      |       list must haveSize (3)
  15.      |     }
  16.      |   }
  17.      |   "A stack" when empty should { 
  18.      |     "is empty" in {
  19.      |       list must beEmpty
  20.      |     }
  21.      |   }
  22.      | }
  23. defined module StackSpecification
  24. scala> StackSpecification.main(Array())
  25. Specification "StackSpecification"
  26.   A full stack should
  27.   + size of 3
  28.   Total for SUS "A full stack":
  29.   Finished in 0 second, 42 ms
  30.   1 example, 1 expectation, 0 failure, 0 error
  31.   A stack should
  32.   + is empty
  33.   Total for SUS "A stack":
  34.   Finished in 0 second, 4 ms
  35.   1 example, 1 expectation, 0 failure, 0 error
  36. Total for specification "StackSpecification":
  37. Finished in 0 second, 85 ms
  38. 2 examples, 2 expectations, 0 failure, 0 error

Wednesday, December 16, 2009

Specs BDD Testing framework

This is the second Scala test framework topic and focuses on the excellent Specs framework. The other topic was on ScalaTest, another excellent testing framework for Scala.

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.

  1. # scala -classpath ~/.m2/repository/org/scala-tools/testing/specs/1.6.1/specs-1.6.1.jar
  2. scala> import org.specs._
  3. import org.specs._
  4. scala> object XmlSpec extends Specification {
  5.      |  val sample = <library>
  6.      |         <videos>
  7.      |         <video type="dvd">Seven</video>
  8.      |         <video type="blue-ray">The fifth element</video>
  9.      |         <video type="hardcover">Gardens of the moon</video>
  10.      |         </videos>
  11.      |         <books>
  12.      |         <book type="softcover">Memories of Ice</book>
  13.      |         </books>
  14.      |         </library>
  15.      |  "Scala XML" should {
  16.      |   "allow xpath-like selection" in {
  17.      |    (sample \\ "video").size must be (3)    
  18.      |   }
  19.      |   "select child nodes" in {
  20.      |    // This test fails because child is a sequence not a string
  21.      |    // See the results of the tests
  22.      |    sample.child must contain (<videos/>)
  23.      |   }
  24.      |  }
  25.      |  }
  26. defined module XmlSpec
  27. scala> XmlSpec.main(Array[String]())
  28. Specification "XmlSpec"
  29.   Scala XML should
  30.   + allow xpath-like selection
  31.   x select child nodes <-- x indicates failure.
  32.     'ArrayBuffer(
  33.                    , <videos>
  34.                    <video type="dvd">Seven</video>
  35.                    <video type="blue-ray">The fifth element</video>
  36.                    <video type="hardcover">Gardens of the moon</video>
  37.                    </videos>, 
  38.                    , <books>
  39.                    <book type="softcover">Memories of Ice</book>
  40.                    </books>, 
  41.                    )' doesn't contain '<videos></videos>' (< console>:24)
  42. Total for specification "XmlSpec":
  43. Finished in 0 second, 52 ms
  44. 2 examples, 2 expectations, 1 failure, 0 error