org.scalatest

trait BeforeAndAfterEach

[source: org/scalatest/BeforeAndAfterEach.scala]

trait BeforeAndAfterEach
extends AbstractSuite with AnyRef
Trait that can be mixed into suites that need methods invoked 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 BeforeAndAfterEach {

   // 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.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 BeforeAndAfterEach is "super" to FunSuite:

 class MySuite extends BeforeAndAfterEach with FunSuite 
 

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

 class MySuite extends FunSuite with BeforeAndAfterEach
 
Author
Bill Venners
Direct Known Subclasses:
BeforeAndAfter

Method Summary
protected def afterEach (configMap : scala.collection.immutable.Map[java.lang.String, Any]) : Unit
Defines a method (that takes a configMap) to be run after each of this suite's tests.
protected def afterEach : Unit
Defines a method to be run after each of this suite's tests.
protected def beforeEach (configMap : scala.collection.immutable.Map[java.lang.String, Any]) : Unit
Defines a method (that takes a configMap) to be run before each of this suite's tests.
protected def beforeEach : Unit
Defines a method to be run before each of this suite's tests.
protected def runTest (testName : java.lang.String, reporter : Reporter, stopper : Stopper, configMap : scala.collection.immutable.Map[java.lang.String, Any], tracker : Tracker) : Unit
Run a test surrounded by calls to beforeEach and afterEach.
Methods inherited from AnyRef
getClass, hashCode, equals, clone, toString, notify, notifyAll, wait, wait, wait, finalize, ==, !=, eq, ne, synchronized
Methods inherited from Any
==, !=, isInstanceOf, asInstanceOf
Methods inherited from AbstractSuite
withFixture (abstract), run (abstract), runNestedSuites (abstract), runTests (abstract), testNames (abstract), nestedSuites (abstract), tags (abstract), expectedTestCount (abstract)
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 the overloaded form of this method that takes a configMap before running each test. This trait's implementation of that beforeEach(Map[String, Any]) method simply invokes this beforeEach() method. Thus this method can be used to set up a test fixture needed by each test, when you don't need anything from the configMap. This trait's implementation of this method does nothing.


protected def beforeEach(configMap : scala.collection.immutable.Map[java.lang.String, Any]) : Unit
Defines a method (that takes a configMap) to be run before each of this suite's tests.

This trait's implementation of runTest invokes this method before running each test (passing in the configMap passed to it), thus this method can be used to set up a test fixture needed by each test. This trait's implementation of this method invokes the overloaded form of beforeEach that takes no configMap.


protected def afterEach : Unit
Defines a method to be run after each of this suite's tests.

This trait's implementation of runTest invokes the overloaded form of this method that takes a configMap map after running each test. This trait's implementation of that afterEach(Map[String, Any]) method simply invokes this afterEach() method. Thus this method can be used to tear down a test fixture needed by each test, when you don't need anything from the configMap. This trait's implementation of this method does nothing.


protected def afterEach(configMap : scala.collection.immutable.Map[java.lang.String, Any]) : Unit
Defines a method (that takes a configMap) to be run after each of this suite's tests.

This trait's implementation of runTest invokes this method after running each test (passing in the configMap passed to it), thus this method can be used to tear down a test fixture needed by each test. This trait's implementation of this method invokes the overloaded form of afterEach that takes no configMap.


protected def runTest(testName : java.lang.String, reporter : Reporter, stopper : Stopper, configMap : scala.collection.immutable.Map[java.lang.String, Any], tracker : Tracker) : Unit
Run a test surrounded by calls to beforeEach and afterEach.

This trait's implementation of this method ("this method") invokes beforeEach(configMap) before running each test and afterEach(configMap) after running each test. It runs each test by invoking super.runTest, passing along the five 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 exception thrown by afterEach.

Overrides
AbstractSuite.runTest


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