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

2 comments:

  1. Nice, I have to try it out myself, thanks!

    ReplyDelete
  2. Nice job, Jesse, it's nice to see some specs features explained here. BTW, there is a typo in the title, the word 'fixture' is missing an 'r'.

    Eric.

    ReplyDelete