org.scalatest

trait BeforeAndAfter

[source: org/scalatest/BeforeAndAfter.scala]

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)
   }
 }
 

Because this trait invokes super.execute to execute the suite and super.runTest to run each test, you may need to mix this trait in last to get the desired behavior. For example, this won't work, because BeforeAndAfter is "super" to FunSuite:

 class MySuite extends BeforeAndAfter with FunSuite 
 

You'd need to turn it around, so that FunSuite is "super" to BeforeAndAfter, like this:

 class MySuite extends FunSuite with BeforeAndAfter
 

If you want to do something before and after both the tests and the nested Suites, then you can override execute itself, or use the beforeAll and afterAll methods of BeforeAndAfter.

Author
Bill Venners
Method Summary
protected def afterAll : Unit
Defines a method to be run after all of this suite's tests and nested suites have been executed. This trait's implementation of execute invokes this method after executing all tests and nested suites, thus this method can be used to tear down a test fixture needed by the entire suite. This trait's implementation of this method does nothing.
protected def afterEach : Unit
Defines a method to be run after each of this suite's tests. This trait's implementation of runTest invokes this method after running each test, thus this method can be used to tear down a test fixture needed by each test. This trait's implementation of this method does nothing.
protected def beforeAll : Unit
Defines a method to be run before any of this suite's tests or nested suites are executed. This trait's implementation of execute invokes this method before executing any tests or nested suites, thus this method can be used to set up a test fixture needed by the entire suite. This trait's implementation of this method does nothing.
protected def beforeEach : Unit
Defines a method to be run before each of this suite's tests. This trait's implementation of runTest invokes this method before running each test, thus this method can be used to set up a test fixture needed by each test. This trait's implementation of this method does nothing.
def execute (testName : scala.Option[java.lang.String], reporter : Reporter, stopper : Stopper, includes : scala.collection.immutable.Set[java.lang.String], excludes : scala.collection.immutable.Set[java.lang.String], properties : scala.collection.immutable.Map[java.lang.String, Any], distributor : scala.Option[Distributor]) : Unit
Execute a suite surrounded by calls to beforeAll and afterAll. This trait's implementation of this method ("this method") invokes beforeAll before executing any tests or nested suites and afterAll after executing all tests and nested suites. It executes the suite by invoking super.execute, passing along the seven parameters passed to it.
def runTest (testName : java.lang.String, reporter : Reporter, stopper : Stopper, properties : scala.collection.immutable.Map[java.lang.String, Any]) : Unit
Run a test surrounded by calls to beforeEach and afterEach. This trait's implementation of this method ("this method") invokes beforeEach before running each test and afterEach after running each test. It runs each test by invoking super.runTest, passing along the four parameters passed to it.
Methods inherited from AnyRef
getClass, hashCode, equals, clone, toString, notify, notifyAll, wait, wait, wait, finalize, ==, !=, eq, ne, synchronized
Methods inherited from Any
==, !=, isInstanceOf, asInstanceOf
Method Details
protected def beforeEach : Unit
Defines a method to be run before each of this suite's tests. This trait's implementation of runTest invokes this method before running each test, thus this method can be used to set up a test fixture needed by each test. This trait's implementation of this method does nothing.

protected def afterEach : Unit
Defines a method to be run after each of this suite's tests. This trait's implementation of runTest invokes this method after running each test, thus this method can be used to tear down a test fixture needed by each test. This trait's implementation of this method does nothing.

protected def beforeAll : Unit
Defines a method to be run before any of this suite's tests or nested suites are executed. This trait's implementation of execute invokes this method before executing any tests or nested suites, thus this method can be used to set up a test fixture needed by the entire suite. This trait's implementation of this method does nothing.

protected def afterAll : Unit
Defines a method to be run after all of this suite's tests and nested suites have been executed. This trait's implementation of execute invokes this method after executing all tests and nested suites, thus this method can be used to tear down a test fixture needed by the entire suite. This trait's implementation of this method does nothing.

def runTest(testName : java.lang.String, reporter : Reporter, stopper : Stopper, properties : scala.collection.immutable.Map[java.lang.String, Any]) : Unit
Run a test surrounded by calls to beforeEach and afterEach. This trait's implementation of this method ("this method") invokes beforeEach before running each test and afterEach after running each test. It runs each test by invoking super.runTest, passing along the four parameters passed to it.

If any invocation of beforeEach completes abruptly with an exception, this method will complete abruptly with the same exception. If any call to super.runTest completes abruptly with an exception, this method will complete abruptly with the same exception, however, before doing so, it will invoke afterEach. If afterEach also completes abruptly with an exception, this method will nevertheless complete abruptly with the exception previously thrown by super.runTest. If super.runTest returns normally, but afterEach completes abruptly with an exception, this method will complete abruptly with the same exception.

Overrides
ExecuteAndRun.runTest

def execute(testName : scala.Option[java.lang.String], reporter : Reporter, stopper : Stopper, includes : scala.collection.immutable.Set[java.lang.String], excludes : scala.collection.immutable.Set[java.lang.String], properties : scala.collection.immutable.Map[java.lang.String, Any], distributor : scala.Option[Distributor]) : Unit
Execute a suite surrounded by calls to beforeAll and afterAll. This trait's implementation of this method ("this method") invokes beforeAll before executing any tests or nested suites and afterAll after executing all tests and nested suites. It executes the suite by invoking super.execute, passing along the seven parameters passed to it.

If any invocation of beforeAll completes abruptly with an exception, this method will complete abruptly with the same exception. If any call to super.execute completes abruptly with an exception, this method will complete abruptly with the same exception, however, before doing so, it will invoke afterAll. If afterAll also completes abruptly with an exception, this method will nevertheless complete abruptly with the exception previously thrown by super.execute. If super.execute returns normally, but afterAll completes abruptly with an exception, this method will complete abruptly with the same exception.

Overrides
ExecuteAndRun.execute


Copyright (C) 2001-2009 Artima, Inc. All rights reserved.