org.scalatest.BeforeAndAfterEach.scala Maven / Gradle / Ivy
Show all versions of scalatest_2.11.0-M3 Show documentation
/* * Copyright 2001-2008 Artima, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.scalatest /** * 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: * *
FunSuite: * ** 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, becauseBeforeAndAfterEach
is "super" to* class MySuite extends BeforeAndAfterEach with FunSuite *** You'd need to turn it around, so that
*FunSuite
is "super" toBeforeAndAfterEach
, like this: ** class MySuite extends FunSuite with BeforeAndAfterEach ** * @author Bill Venners */ trait BeforeAndAfterEach extends AbstractSuite { this: Suite => /** * Defines a method to be run before each of this suite's tests. * ** This trait's implementation * of
*/ protected def beforeEach() = () /** * Defines a method (that takes arunTest
invokes the overloaded form of this method that * takes aconfigMap
before running * each test. This trait's implementation of thatbeforeEach(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 theconfigMap
. * This trait's implementation of this method does nothing. *configMap
) to be run before * each of this suite's tests. * ** This trait's implementation * of
*/ protected def beforeEach(configMap: Map[String, Any]) { beforeEach() } /** * Defines a method to be run after each of this suite's tests. * *runTest
invokes this method before running * each test (passing in theconfigMap
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 ofbeforeEach
that takes noconfigMap
. ** This trait's implementation * of
*/ protected def afterEach() = () /** * Defines a method (that takes arunTest
invokes the overloaded form of this method that * takes aconfigMap
map after running * each test. This trait's implementation of thatafterEach(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 theconfigMap
. * This trait's implementation of this method does nothing. *configMap
) to be run after * each of this suite's tests. * ** This trait's implementation * of
*/ protected def afterEach(configMap: Map[String, Any]) { afterEach() } /** * Run a test surrounded by calls torunTest
invokes this method after running * each test (passing in theconfigMap
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 ofafterEach
that takes noconfigMap
. *beforeEach
andafterEach
. * ** This trait's implementation of this method ("this method") invokes *
* *beforeEach(configMap)
* before running each test andafterEach(configMap)
* after running each test. It runs each test by invokingsuper.runTest
, passing along * the five parameters passed to it. ** If any invocation of
*/ abstract protected override def runTest(testName: String, reporter: Reporter, stopper: Stopper, configMap: Map[String, Any], tracker: Tracker) { var thrownException: Option[Throwable] = None beforeEach(configMap) try { super.runTest(testName, reporter, stopper, configMap, tracker) } catch { case e: Exception => thrownException = Some(e) } finally { try { afterEach(configMap) // Make sure that afterEach is called even if runTest completes abruptly. thrownException match { case Some(e) => throw e case None => } } catch { case laterException: Exception => thrownException match { // If both run and afterAll throw an exception, report the test exception case Some(earlierException) => throw earlierException case None => throw laterException } } } } }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 * invokeafterEach
. IfafterEach also completes abruptly with an exception, this * method will nevertheless complete abruptly with the exception previously thrown by super.runTest
. * Ifsuper.runTest
returns normally, butafterEach
completes abruptly with an * exception, this method will complete abruptly with the exception thrown byafterEach
. *