ScalaTest 0.9.5

This document is the API specification for ScalaTest 0.9.5

Class Summary
trait Assertions extends AnyRef
Trait that contains ScalaTest's basic assertion methods.
trait BeforeAndAfter extends ExecuteAndRun with AnyRef
Trait that can be mixed into suites that need methods invoked before and after executing the suite, and/or before and after running each test. This trait facilitates a style of testing in which mutable fixture objects held in instance variables are replaced or reinitialized before each test or suite. Here's an example:
 import org.scalatest._
 import scala.collection.mutable.ListBuffer

 class MySuite extends BeforeAndAfter {

   // Fixtures as reassignable variables and mutable objects
   var sb: StringBuilder = _
   val lb = new ListBuffer[String]
 
   override def beforeEach() {
     sb = new StringBuilder("ScalaTest is ")
     lb.clear()
   }

   def testEasy() {
     sb.append("easy!")
     assert(sb.toString === "ScalaTest is easy!")
     assert(lb.isEmpty)
     lb += "sweet"
   }

   def testFun() {
     sb.append("fun!")
     assert(sb.toString === "ScalaTest is fun!")
     assert(lb.isEmpty)
   }
 }
 
trait Distributor extends AnyRef
trait ExecuteAndRun extends AnyRef
Trait that abstracts out the execute and runTest methods of Suite. This trait exists to support the use of trait BeforeAndAfter, which is a direct subtrait of this trait. The BeforeAndAfter trait's implementation of runTest surrounds an invocation of super.runTest in calls to beforeEach and afterEach. Similarly, the BeforeAndAfter trait's implementation of execute surrounds an invocation of super.execute in calls to beforeAll and afterAll. This enables trait BeforeAndAfter to be mixed into any Suite, but not into a trait that contains shared examples for a Spec.
trait FunSuite extends Suite
A suite of tests in which each test is represented as a function value. The “Fun” in FunSuite stands for functional. Here's an example FunSuite:
 import org.scalatest.FunSuite

 class MySuite extends FunSuite {

   test("addition") {
     val sum = 1 + 1
     assert(sum === 2)
     assert(sum + 2 === 4)
   }

   test("subtraction") {
     val diff = 4 - 1
     assert(diff === 3)
     assert(diff - 2 === 1)
   }
 }
 
abstract class Group (val name : java.lang.String) extends AnyRef
Abstract class whose subclasses can be passed to FunSuite and Spec test registration methods to place tests into groups. For example, if you define:
 object SlowTest extends Group("SlowTest")
 
then you can place a test into the SlowTest group like this:
 import org.scalatest.FunSuite

 class MySuite extends FunSuite {

   test("my test", SlowTest) {
     Thread.sleep(1000)
   }
 }
 
trait ImpSuite extends Suite
Deprecated: Use trait BeforeAndAfter instead. This trait will be removed in a future version of ScalaTest. Trait that can be mixed into suites that need methods invoked before and after executing the suite, and/or before and after running each test. The "imp" in ImpSuite stands for imperative, because this trait facilitates a style of testing in which mutable fixture objects held in instance variables are replaced or reinitialized before each test or suite. Here's an example:
 import org.scalatest._
 import scala.collection.mutable.ListBuffer

 class MySuite extends ImpSuite {

   // Fixtures as reassignable variables and mutable objects
   var sb: StringBuilder = _
   val lb = new ListBuffer[String]
 
   override def beforeEach() {
     sb = new StringBuilder("ScalaTest is ")
     lb.clear()
   }

   def testEasy() {
     sb.append("easy!")
     assert(sb.toString === "ScalaTest is easy!")
     assert(lb.isEmpty)
     lb += "sweet"
   }

   def testFun() {
     sb.append("fun!")
     assert(sb.toString === "ScalaTest is fun!")
     assert(lb.isEmpty)
   }
 }
 
trait Informer extends AnyRef
Trait to which custom information about a running suite of tests can be reported. Informer contains two apply methods, one which takes a string and the other a Report. An Informer is essentially used to wrap a Reporter and provide easy ways to send custom information to that Reporter via its infoProvided method.
trait PrivateMethodTester extends AnyRef
Trait that facilitates the testing of private methods. To test a private method, mix in trait PrivateMethodTester and create a PrivateMethod object, like this:
 val decorateToStringValue = PrivateMethod[String]('decorateToStringValue)
 
class Report (val name : java.lang.String, val message : java.lang.String, val throwable : scala.Option[java.lang.Throwable], val rerunnable : scala.Option[Rerunnable], val threadName : java.lang.String, val date : java.util.Date) extends AnyRef
trait Reporter extends AnyRef
Trait whose instances collect the results of a running suite of tests and presents those results in some way to the user.
trait Rerunnable extends AnyRef
Trait whose instances can rerun tests or other entities (such as suites). An object extending this trait can be passed to a Reporter as part of a Report. The test or other entity about which the report is made can then be rerun by invoking the rerun method on the Rerunnable.
trait Spec extends Suite
Trait that facilitates a “behavior-driven” style of development (BDD), in which tests are combined with text that specifies the behavior the tests verify. Here's an example Spec:
 import org.scalatest.Spec
 import scala.collection.mutable.Stack

 class StackSpec extends Spec {

   describe("A Stack") {

     it("should pop values in last-in-first-out order") {
       val stack = new Stack[Int]
       stack.push(1)
       stack.push(2)
       assert(stack.pop() === 2)
       assert(stack.pop() === 1)
     }

     it("should throw NoSuchElementException if an empty stack is popped") {
       val emptyStack = new Stack[String]
       intercept[NoSuchElementException] {
         emptyStack.pop()
       }
     }
   }
 }
 
class SpecReport (val override name : java.lang.String, val override message : java.lang.String, val plainSpecText : java.lang.String, val formattedSpecText : java.lang.String, val includeInSpecOutput : Boolean, val override throwable : scala.Option[java.lang.Throwable], val override rerunnable : scala.Option[Rerunnable], val override threadName : java.lang.String, val override date : java.util.Date) extends Report
trait Stopper extends AnyRef
Trait whose instances can indicate whether a stop has been requested. This is passed in to the execute method of Suite, so that running suites of tests can be requested to stop early.
trait Suite extends Assertions with ExecuteAndRun
class SuperSuite (val override nestedSuites : scala.List[Suite]) extends Suite
A Suite class that takes a List[Suite] parameter, which overrides the nestedSuites method of trait Suite.
class TestFailedException (val message : scala.Option[java.lang.String], val cause : scala.Option[java.lang.Throwable], val failedTestCodeStackDepth : Int) extends java.lang.RuntimeException with AnyRef
Exception that indicates a test failed. The purpose of this exception is to encapsulate information about the stack depth at which the line of test code that failed resides, so that information can be presented to the user that makes it quick to find the failing line of test code. (I.e., the user need not scan through the stack trace to find the correct filename and line number of the failing test.)
Object Summary
object Assertions extends Assertions
Companion object that facilitates the importing of Assertions members as an alternative to mixing it in. One use case is to import Assertions members so you can use them in the Scala interpreter:
 $scala -classpath scalatest.jar
 Welcome to Scala version 2.7.3.final (Java HotSpot(TM) Client VM, Java 1.5.0_16).
 Type in expressions to have them evaluated.
 Type :help for more information.
 
 scala> import org.scalatest.Assertions._
 import org.scalatest.Assertions._
 
 scala> assert(1 === 2)
 org.scalatest.TestFailedException: 1 did not equal 2
 	at org.scalatest.Assertions$class.assert(Assertions.scala:211)
 	at org.scalatest.Assertions$.assert(Assertions.scala:511)
 	at .(:7)
 	at .()
 	at RequestResult$.(:3)
 	at RequestResult$.()
 	at RequestResult$result()
 	at sun.reflect.NativeMethodAccessorImpl.invoke...

 scala> expect(3) { 1 + 3 }
 org.scalatest.TestFailedException: Expected 3, but got 4
 	at org.scalatest.Assertions$class.expect(Assertions.scala:447)
 	at org.scalatest.Assertions$.expect(Assertions.scala:511)
 	at .(:7)
 	at .()
 	at RequestResult$.(:3)
 	at RequestResult$.()
 	at RequestResult$result()
 	at sun.reflect.NativeMethodAccessorImpl.in...

 scala> val caught = intercept[StringIndexOutOfBoundsException] { "hi".charAt(-1) }
 caught: StringIndexOutOfBoundsException = java.lang.StringIndexOutOfBoundsException: String index out of range: -1