Showing posts with label test. Show all posts
Showing posts with label test. 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.
Example using Contexts there many more examples at: Shared contexts
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.
- jeichar: git-src$ scala -classpath ~/.m2/repository/org/scala-tools/testing/specs/1.6.1/specs-1.6.1.jar
- scala> import org.specs._
- import org.specs._
- /*
- This example demonstrates before and after actions similar to what is found in XUnit.
- */
- scala> object mySpec extends Specification {
- | "my system" should {
- | doFirst{println("before")} // ran once
- |
- | doBefore{println("before")} // ran for each test
- | doAfter{println("after")} // ran for each test
- |
- | "test spec 1" in { println("test1"); 1 must_== 1}
- | "test spec 2" in { println("test2"); 1 must_== 2}
- |
- | doLast{println("last")} // ran once
- | }}
- defined module mySpec
- scala> mySpec.main(Array())
- Specification "mySpec"
- my system should
- before
- before
- test1
- after
- + test spec 1
- before
- test2
- after
- last
- x test spec 2
- '1' is not equal to '2' (< console>:14)
- Total for specification "mySpec":
- Finished in 0 second, 307 ms
- 2 examples, 2 expectations, 1 failure, 0 error
Example using Contexts there many more examples at: Shared contexts
- scala> import org.specs._
- import org.specs._
- /*
- This specification uses contexts instead of before and after actions.
- 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
- */
- scala> object StackSpecification extends Specification {
- | var list : List[Int] = _
- | val empty = beforeContext(list = Nil)
- | val nonEmpty = beforeContext(list = List(1,2,3))
- |
- | "A full stack" definedAs nonEmpty should {
- | "size of 3" in {
- | list must haveSize (3)
- | }
- | }
- | "A stack" when empty should {
- | "is empty" in {
- | list must beEmpty
- | }
- | }
- | }
- defined module StackSpecification
- scala> StackSpecification.main(Array())
- Specification "StackSpecification"
- A full stack should
- + size of 3
- Total for SUS "A full stack":
- Finished in 0 second, 42 ms
- 1 example, 1 expectation, 0 failure, 0 error
- A stack should
- + is empty
- Total for SUS "A stack":
- Finished in 0 second, 4 ms
- 1 example, 1 expectation, 0 failure, 0 error
- Total for specification "StackSpecification":
- Finished in 0 second, 85 ms
- 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.
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
Monday, November 16, 2009
ScalaTest BDD testing DSL
This is the first of three topics exploring some of the testing tools that you can use with Scala. They all integrate with JUnit and can be used with maven and ant.
The first example is ScalaTest. ScalaTest provides many different usage patterns but the one I will show today is a Behaviour Driven Design test. It provides a DSL for declaring a test that is nearly english in syntax and if done correctly can be given to a non technical person to review. The ScalaTest BDD test style was inspired by the work done on Specs by Eric Torreborre. I will demonstrate that library in the next topic.
I want to reiterate that ScalaTest permits several different styles of test writing. NUnit, JUnit and BDD are the main styles. So anyone that is used to NUnit or JUnit should have no problem using ScalaTest but if you like the look of the BDD test specifications then you can migrate and slowly become accustomed to that style.
If you want to integrate with JUnit one easy way to add the @RunWith annotation for the class. See JUnitRunner for details:
Instructions to run example:
This example is directly taken from the ScalaTest documentation. It tests a Stack implementation.
The first example is ScalaTest. ScalaTest provides many different usage patterns but the one I will show today is a Behaviour Driven Design test. It provides a DSL for declaring a test that is nearly english in syntax and if done correctly can be given to a non technical person to review. The ScalaTest BDD test style was inspired by the work done on Specs by Eric Torreborre. I will demonstrate that library in the next topic.
I want to reiterate that ScalaTest permits several different styles of test writing. NUnit, JUnit and BDD are the main styles. So anyone that is used to NUnit or JUnit should have no problem using ScalaTest but if you like the look of the BDD test specifications then you can migrate and slowly become accustomed to that style.
If you want to integrate with JUnit one easy way to add the @RunWith annotation for the class. See JUnitRunner for details:
- import org.scalatest.junit.JUnitRunner
- import org.junit.runner.RunWith
- @RunWith(classOf[JUnitRunner])
- class StackSpec extends WordSpec {
- ...
- }
Instructions to run example:
- Download ScalaTest: http://www.scalatest.org/download
- Extract archive. For example to /tmp/
- Run scala with the ScalaTest jar on classpath: scala -classpath /tmp/scalatest-1.0/scalatest-1.0.jar
- copy in code
This example is directly taken from the ScalaTest documentation. It tests a Stack implementation.
- scala> import org.scalatest.WordSpec
- import org.scalatest.WordSpec
- scala> import scala.collection.mutable.Stack
- import scala.collection.mutable.Stack
- scala>
- scala> class StackSpec extends WordSpec {
- |
- | "A Stack" should {
- |
- | "pop values in last-in-first-out order" in {
- | val stack = new Stack[Int]
- | stack.push(1)
- | stack.push(2)
- | assert(stack.pop() === 2)
- | assert(stack.pop() === 1)
- | }
- |
- | "throw NoSuchElementException if an empty stack is popped" in {
- | val emptyStack = new Stack[String]
- | intercept[NoSuchElementException] {
- | emptyStack.pop()
- | }
- | }
- | }
- | }
- defined class StackSpec
- scala> new StackSpec().execute()
- A Stack
- - should pop values in last-in-first-out order
- - should throw NoSuchElementException if an empty stack is popped
Subscribe to:
Comments (Atom)