All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.scalatest.BeforeAndAfterEach.scala Maven / Gradle / Ivy

There is a newer version: 2.0.M6-SNAP27
Show newest version
/*
 * 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:
 *
 * 
 * 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 */ trait BeforeAndAfterEach extends AbstractSuite { this: Suite => /** * 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() = () /** * 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 beforeEach(configMap: Map[String, Any]) { beforeEach() } /** * 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() = () /** * 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 afterEach(configMap: Map[String, Any]) { afterEach() } /** * 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. *

*/ 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 } } } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy