import org.scalatest.FlatSpec import org.scalatest.matchers.ShouldMatchers class StackSpec extends FlatSpec with ShouldMatchers { "A Stack" should "pop values in last-in-first-out order" in { val stack = new Stack[Int] stack.push(1) stack.push(2) stack.pop() should equal (2) stack.pop() should equal (1) } it should "throw NoSuchElementException if an empty stack is popped" in { val emptyStack = new Stack[String] evaluating { emptyStack.pop() } should produce [NoSuchElementException] } }
With ScalaTest, you can test either Scala or Java code. By integrating with popular tools such as JUnit, TestNG, Ant, and Maven, ScalaTest makes it easy to take your testing to a higher, more productive level in new or existing Scala or Java projects.
Because different developers take different approaches to creating software, ScalaTest is designed to facilitate different styles of testing. ScalaTest provides several traits that you can mix together into whatever combination makes you feel the most productive. For instance, the above example illustrates a Behavior-Driven Development (BDD) style. When you run it, you get an informal specification of the software being tested:
$ scala -cp scalatest.jar org.scalatest.tools.Runner -p . -o -s StackSpec Run starting. Expected test count is: 2 StackSpec: A Stack - should pop values in last-in-first-out order - should throw NoSuchElementException if an empty stack is popped Run completed in 96 milliseconds. Total number of tests run: 2 Suites: completed 1, aborted 0 Tests: succeeded 2, failed 0, ignored 0, pending 0 All tests passed.
Not into BDD? Not a problem. ScalaTest has many other traits to choose from. If you want to continue writing tests with JUnit or TestNG, but write them in Scala, ScalaTest has traits for that. If you prefer functional to unit testing, or need to do integration or acceptance testing, ScalaTest has traits for that. And much more. In short, ScalaTest is highly customizable, providing many extension points that let you design the testing experience you want even if none of its built-in traits exactly fit your needs.
Why not give it a try? Just Download ScalaTest, then visit the Quick Start page.
ScalaTest is brought to you by Bill Venners, with contributions from several other folks. It is sponsored by Artima, Inc.
ScalaTest is free, open-source software released under the Apache 2.0 license.
Copyright © 2009 Artima, Inc. All Rights Reserved.