org.scalatest.fixture.FixtureSpec.scala Maven / Gradle / Ivy
/* * Copyright 2001-2009 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.fixture import org.scalatest._ import FixtureNodeFamily._ import scala.collection.immutable.ListSet import org.scalatest.StackDepthExceptionHelper.getStackDepth import java.util.concurrent.atomic.AtomicReference import java.util.ConcurrentModificationException import org.scalatest.events._ import Suite.anErrorThatShouldCauseAnAbort import verb.BehaveWord /** * A sister trait to
, see the Shared tests section * in the main documentation for traitorg.scalatest.Spec
, which passes a fixture object into each test. * ** This trait behaves similarly to trait
* *org.scalatest.Spec
, except that tests may take a fixture object. The type of the * fixture object passed is defined by the abstractFixture
type, which is declared as a member of this trait (inherited * from supertraitFixtureSuite
). * This trait also inherits the abstract methodwithFixture
from supertraitFixtureSuite
. ThewithFixture
method * takes aOneArgTest
, which is a nested trait defined as a member of supertraitFixtureSuite
. *OneArgTest
has anapply
method that takes aFixture
. * Thisapply
method is responsible for running a test. * This trait'srunTest
method delegates the actual running of each test towithFixture
, passing * in the test code to run via theOneArgTest
argument. ThewithFixture
method (abstract in this trait) is responsible * for creating the fixture and passing it to the test function. ** Subclasses of this trait must, therefore, do three things differently from a plain old
* *org.scalatest.Spec
: **
* *- define the type of the fixture object by specifying type
*Fixture
- define the
*withFixture
method- write tests that take a
*Fixture
(You can also define tests that don't take aFixture
.)* Here's an example: *
* ** import org.scalatest.fixture.FixtureSpec * import java.io.FileReader * import java.io.FileWriter * import java.io.File * * class MySpec extends FixtureSpec { * * // 1. define type FixtureParam * type FixtureParam = FileReader * * // 2. define the withFixture method * def withFixture(test: OneArgTest) { * * val FileName = "TempFile.txt" * * // Set up the temp file needed by the test * val writer = new FileWriter(FileName) * try { * writer.write("Hello, test!") * } * finally { * writer.close() * } * * // Create the reader needed by the test * val reader = new FileReader(FileName) * * try { * // Run the test using the temp file * test(reader) * } * finally { * // Close and delete the temp file * reader.close() * val file = new File(FileName) * file.delete() * } * } * * // 3. write tests that take a fixture parameter * it("should read from the temp file") { reader => * var builder = new StringBuilder * var c = reader.read() * while (c != -1) { * builder.append(c.toChar) * c = reader.read() * } * assert(builder.toString === "Hello, test!") * } * * it("should read the first char of the temp file") { reader => * assert(reader.read() === 'H') * } * * // (You can also write tests that don't take a fixture parameter.) * it("should work without a fixture") { () => * assert(1 + 1 === 2) * } * } ** ** If the fixture you want to pass into your tests consists of multiple objects, you will need to combine * them into one object to use this trait. One good approach to passing multiple fixture objects is * to encapsulate them in a tuple. Here's an example that takes the tuple approach: *
* ** import org.scalatest.fixture.FixtureSpec * import scala.collection.mutable.ListBuffer * * class MySpec extends FixtureSpec { * * type FixtureParam = (StringBuilder, ListBuffer[String]) * * def withFixture(test: OneArgTest) { * * // Create needed mutable objects * val stringBuilder = new StringBuilder("ScalaTest is ") * val listBuffer = new ListBuffer[String] * * // Invoke the test function, passing in the mutable objects * test(stringBuilder, listBuffer) * } * * it("should mutate shared fixture objects") { fixture => * val (builder, buffer) = fixture * builder.append("easy!") * assert(builder.toString === "ScalaTest is easy!") * assert(buffer.isEmpty) * buffer += "sweet" * } * * it("should get a fresh set of mutable fixture objects") { fixture => * val (builder, buffer) = fixture * builder.append("fun!") * assert(builder.toString === "ScalaTest is fun!") * assert(buffer.isEmpty) * } * } ** ** When using a tuple to pass multiple fixture objects, it is usually helpful to give names to each * individual object in the tuple with a pattern-match assignment, as is done at the beginning * of each test here with: *
* ** val (builder, buffer) = fixture ** ** Another good approach to passing multiple fixture objects is * to encapsulate them in a case class. Here's an example that takes the case class approach: *
* ** import org.scalatest.fixture.FixtureSpec * import scala.collection.mutable.ListBuffer * * class MySpec extends FixtureSpec { * * case class FixtureHolder(builder: StringBuilder, buffer: ListBuffer[String]) * * type FixtureParam = FixtureHolder * * def withFixture(test: OneArgTest) { * * // Create needed mutable objects * val stringBuilder = new StringBuilder("ScalaTest is ") * val listBuffer = new ListBuffer[String] * * // Invoke the test function, passing in the mutable objects * test(FixtureHolder(stringBuilder, listBuffer)) * } * * it("should mutate shared fixture objects") { fixture => * import fixture._ * builder.append("easy!") * assert(builder.toString === "ScalaTest is easy!") * assert(buffer.isEmpty) * buffer += "sweet" * } * * it("should get a fresh set of mutable fixture objects") { fixture => * fixture.builder.append("fun!") * assert(fixture.builder.toString === "ScalaTest is fun!") * assert(fixture.buffer.isEmpty) * } * } ** ** When using a case class to pass multiple fixture objects, it can be helpful to make the names of each * individual object available as a single identifier with an import statement. This is the approach * taken by the
* *testEasy
method in the previous example. Because it imports the members * of the fixture object, the test code can just use them as unqualified identifiers: ** it("should mutate shared fixture objects") { fixture => * import fixture._ * builder.append("easy!") * assert(builder.toString === "ScalaTest is easy!") * assert(buffer.isEmpty) * buffer += "sweet" * } ** ** Alternatively, you may sometimes prefer to qualify each use of a fixture object with the name * of the fixture parameter. This approach, taken by the
* *testFun
method in the previous * example, makes it more obvious which variables in your test * are part of the passed-in fixture: ** it("should mutate shared fixture objects") { fixture => * fixture.builder.append("fun!") * assert(fixture.builder.toString === "ScalaTest is fun!") * assert(fixture.buffer.isEmpty) * } ** *Configuring fixtures and tests
* ** Sometimes you may want to write tests that are configurable. For example, you may want to write * a suite of tests that each take an open temp file as a fixture, but whose file name is specified * externally so that the file name can be can be changed from run to run. To accomplish this * the
* *OneArgTest
trait has aconfigMap
* method, which will return aMap[String, Any]
from which configuration information may be obtained. * TherunTest
method of this trait will pass aOneArgTest
towithFixture
* whoseconfigMap
method returns theconfigMap
passed torunTest
. * Here's an example in which the name of a temp file is taken from the passedconfigMap
: ** import org.scalatest.fixture.FixtureSpec * import java.io.FileReader * import java.io.FileWriter * import java.io.File * * class MySpec extends FixtureSpec { * * type FixtureParam = FileReader * * def withFixture(test: OneArgTest) { * * require( * test.configMap.contains("TempFileName"), * "This suite requires a TempFileName to be passed in the configMap" * ) * * // Grab the file name from the configMap * val FileName = test.configMap("TempFileName") * * // Set up the temp file needed by the test * val writer = new FileWriter(FileName) * try { * writer.write("Hello, test!") * } * finally { * writer.close() * } * * // Create the reader needed by the test * val reader = new FileReader(FileName) * * try { * // Run the test using the temp file * test(reader) * } * finally { * // Close and delete the temp file * reader.close() * val file = new File(FileName) * file.delete() * } * } * * test("reading from the temp file") { reader => * var builder = new StringBuilder * var c = reader.read() * while (c != -1) { * builder.append(c.toChar) * c = reader.read() * } * assert(builder.toString === "Hello, test!") * } * * test("first char of the temp file") { reader => * assert(reader.read() === 'H') * } * } ** ** If you want to pass into each test the entire
* *configMap
that was passed torunTest
, you * can mix in traitConfigMapFixture
. See the documentation * forConfigMapFixture
for the details, but here's a quick * example of how it looks: ** import org.scalatest.fixture.FixtureSpec * import org.scalatest.fixture.ConfigMapFixture * * class MySpec extends FixtureSpec with ConfigMapFixture { * * it("should contain hello") { configMap => * // Use the configMap passed to runTest in the test * assert(configMap.contains("hello") * } * * it("should contain world") { configMap => * assert(configMap.contains("world") * } * } ** **
* * @author Bill Venners */ trait FixtureSpec extends FixtureSuite { thisSuite => private val IgnoreTagName = "org.scalatest.Ignore" private class Bundle private( val trunk: Trunk, val currentBranch: Branch, val tagsMap: Map[String, Set[String]], // All tests, in reverse order of registration val testsList: List[FixtureTestLeaf[FixtureParam]], // Used to detect at runtime that they've stuck a describe or an it inside an it, // which should result in a TestRegistrationClosedException val registrationClosed: Boolean ) { def unpack = (trunk, currentBranch, tagsMap, testsList, registrationClosed) } private object Bundle { def apply( trunk: Trunk, currentBranch: Branch, tagsMap: Map[String, Set[String]], testsList: List[FixtureTestLeaf[FixtureParam]], registrationClosed: Boolean ): Bundle = new Bundle(trunk, currentBranch, tagsMap, testsList, registrationClosed) def initialize( trunk: Trunk, tagsMap: Map[String, Set[String]], testsList: List[FixtureTestLeaf[FixtureParam]], registrationClosed: Boolean ): Bundle = new Bundle(trunk, trunk, tagsMap, testsList, registrationClosed) } private val atomic = new AtomicReference[Bundle]( Bundle.initialize(new Trunk, Map(), List[FixtureTestLeaf[FixtureParam]](), false) ) private def updateAtomic(oldBundle: Bundle, newBundle: Bundle) { val shouldBeOldBundle = atomic.getAndSet(newBundle) if (!(shouldBeOldBundle eq oldBundle)) throw new ConcurrentModificationException(Resources("concurrentFixtureSpecBundleMod")) } private def registerTest(specText: String, f: FixtureParam => Any) = { val oldBundle = atomic.get var (trunk, currentBranch, tagsMap, testsList, registrationClosed) = oldBundle.unpack val testName = getTestName(specText, currentBranch) if (testsList.exists(_.testName == testName)) { throw new DuplicateTestNameException(testName, getStackDepth("Spec.scala", "it")) } val testShortName = specText val test = FixtureTestLeaf[FixtureParam](currentBranch, testName, specText, f) currentBranch.subNodes ::= test testsList ::= test updateAtomic(oldBundle, Bundle(trunk, currentBranch, tagsMap, testsList, registrationClosed)) testName } private class RegistrationInformer extends Informer { def apply(message: String) { if (message == null) throw new NullPointerException val oldBundle = atomic.get var (trunk, currentBranch, tagsMap, testsList, registrationClosed) = oldBundle.unpack currentBranch.subNodes ::= InfoLeaf(currentBranch, message) updateAtomic(oldBundle, Bundle(trunk, currentBranch, tagsMap, testsList, registrationClosed)) } } // The informer will be a registration informer until run is called for the first time. (This // is the registration phase of a FixtureSpec's lifecycle.) private final val atomicInformer = new AtomicReference[Informer](new RegistrationInformer) /** * Returns anConfigMapFixture
can also be used to facilitate writingFixtureSpec
s that include tests * that take different fixture types. See the documentation forMultipleFixtureSpec
for more information. *Informer
that during test execution will forward strings (and other objects) passed to its *apply
method to the current reporter. If invoked in a constructor, it * will register the passed string for forwarding later during test execution. If invoked while this *FixtureSpec
is being executed, such as from inside a test function, it will forward the information to * the current reporter immediately. If invoked at any other time, it will * throw an exception. This method can be called safely by any thread. */ implicit protected def info: Informer = atomicInformer.get private val zombieInformer = new Informer { private val complaint = Resources("cantCallInfoNow", "FixtureSpec") def apply(message: String) { if (message == null) throw new NullPointerException throw new IllegalStateException(complaint) } } /** * Class that, via an instance referenced from theit
field, * supports test (and shared test) registration inFixtureSpec
s. * ** This class supports syntax such as the following: *
* ** it("should be empty") * ^ ** ** it should behave like nonFullStack(stackWithOneItem) * ^ ** ** For more information and examples, see the main documentation for
*/ protected final class ItWord { /** * Register a test with the given spec text, optional tags, and test function value that takes no arguments. * An invocation of this method is called an “example.” * * This method will register the test for later execution via an invocation of one of theSpec
. *execute
* methods. The name of the test will be a concatenation of the text of all surrounding describers, * from outside in, and the passed spec text, with one space placed between each item. (See the documenation * fortestNames
for an example.) The resulting test name must not have been registered previously on * thisSpec
instance. * * @param specText the specification text, which will be combined with the descText of any surrounding describers * to form the test name * @param testTags the optional list of tags for this test * @param testFun the test function * @throws DuplicateTestNameException if a test with the same name has been registered previously * @throws TestRegistrationClosedException if invoked afterrun
has been invoked on this suite * @throws NullPointerException ifspecText
or any passed test tag isnull
*/ def apply(specText: String, testTags: Tag*)(testFun: FixtureParam => Any) { if (atomic.get.registrationClosed) throw new TestRegistrationClosedException(Resources("itCannotAppearInsideAnotherIt"), getStackDepth("Spec.scala", "it")) if (specText == null) throw new NullPointerException("specText was null") if (testTags.exists(_ == null)) throw new NullPointerException("a test tag was null") val testName = registerTest(specText, testFun) val oldBundle = atomic.get var (trunk, currentBranch, tagsMap, testsList, registrationClosed2) = oldBundle.unpack val tagNames = Set[String]() ++ testTags.map(_.name) if (!tagNames.isEmpty) tagsMap += (testName -> tagNames) updateAtomic(oldBundle, Bundle(trunk, currentBranch, tagsMap, testsList, registrationClosed2)) } /** * Register a test with the given spec text and test function value that takes no arguments. * * This method will register the test for later execution via an invocation of one of theexecute
* methods. The name of the test will be a concatenation of the text of all surrounding describers, * from outside in, and the passed spec text, with one space placed between each item. (See the documenation * fortestNames
for an example.) The resulting test name must not have been registered previously on * thisSpec
instance. * * @param specText the specification text, which will be combined with the descText of any surrounding describers * to form the test name * @param testFun the test function * @throws DuplicateTestNameException if a test with the same name has been registered previously * @throws TestRegistrationClosedException if invoked afterrun
has been invoked on this suite * @throws NullPointerException ifspecText
or any passed test tag isnull
*/ def apply(specText: String)(testFun: FixtureParam => Any) { if (atomic.get.registrationClosed) throw new TestRegistrationClosedException(Resources("itCannotAppearInsideAnotherIt"), getStackDepth("Spec.scala", "it")) apply(specText, Array[Tag](): _*)(testFun) } /** * Supports the registration of shared tests. * ** This method supports syntax such as the following: *
* ** it should behave like nonFullStack(stackWithOneItem) * ^ ** ** For examples of shared tests, see the Shared tests section * in the main documentation for trait
*/ def should(behaveWord: BehaveWord) = behaveWord /** * Supports the registration of shared tests. * *Spec
. ** This method supports syntax such as the following: *
* ** it must behave like nonFullStack(stackWithOneItem) * ^ ** ** For examples of shared tests, see the Shared tests section * in the main documentation for trait
*/ def must(behaveWord: BehaveWord) = behaveWord } /** * Supports test (and shared test) registration inSpec
. *FixtureSpec
s. * ** This field supports syntax such as the following: *
* ** it("should be empty") * ^ ** ** it should behave like nonFullStack(stackWithOneItem) * ^ ** ** For more information and examples of the use of the
*/ protected val it = new ItWord /** * Register a test to ignore, which has the given spec text, optional tags, and test function value that takes no arguments. * This method will register the test for later ignoring via an invocation of one of theit
field, see * the main documentation forSpec
. *execute
* methods. This method exists to make it easy to ignore an existing test by changing the call toit
* toignore
without deleting or commenting out the actual test code. The test will not be executed, but a * report will be sent that indicates the test was ignored. The name of the test will be a concatenation of the text of all surrounding describers, * from outside in, and the passed spec text, with one space placed between each item. (See the documenation * fortestNames
for an example.) The resulting test name must not have been registered previously on * thisSpec
instance. * * @param specText the specification text, which will be combined with the descText of any surrounding describers * to form the test name * @param testTags the optional list of tags for this test * @param testFun the test function * @throws DuplicateTestNameException if a test with the same name has been registered previously * @throws TestRegistrationClosedException if invoked afterrun
has been invoked on this suite * @throws NullPointerException ifspecText
or any passed test tag isnull
*/ protected def ignore(specText: String, testTags: Tag*)(testFun: FixtureParam => Any) { if (atomic.get.registrationClosed) throw new TestRegistrationClosedException(Resources("ignoreCannotAppearInsideAnIt"), getStackDepth("Spec.scala", "ignore")) if (specText == null) throw new NullPointerException("specText was null") if (testTags.exists(_ == null)) throw new NullPointerException("a test tag was null") val testName = registerTest(specText, testFun) val tagNames = Set[String]() ++ testTags.map(_.name) val oldBundle = atomic.get var (trunk, currentBranch, tagsMap, testsList, registrationClosed) = oldBundle.unpack tagsMap += (testName -> (tagNames + IgnoreTagName)) updateAtomic(oldBundle, Bundle(trunk, currentBranch, tagsMap, testsList, registrationClosed)) } /** * Register a test to ignore, which has the given spec text and test function value that takes no arguments. * This method will register the test for later ignoring via an invocation of one of theexecute
* methods. This method exists to make it easy to ignore an existing test by changing the call toit
* toignore
without deleting or commenting out the actual test code. The test will not be executed, but a * report will be sent that indicates the test was ignored. The name of the test will be a concatenation of the text of all surrounding describers, * from outside in, and the passed spec text, with one space placed between each item. (See the documenation * fortestNames
for an example.) The resulting test name must not have been registered previously on * thisSpec
instance. * * @param specText the specification text, which will be combined with the descText of any surrounding describers * to form the test name * @param testFun the test function * @throws DuplicateTestNameException if a test with the same name has been registered previously * @throws TestRegistrationClosedException if invoked afterrun
has been invoked on this suite * @throws NullPointerException ifspecText
or any passed test tag isnull
*/ protected def ignore(specText: String)(testFun: FixtureParam => Any) { if (atomic.get.registrationClosed) throw new TestRegistrationClosedException(Resources("ignoreCannotAppearInsideAnIt"), getStackDepth("Spec.scala", "ignore")) ignore(specText, Array[Tag](): _*)(testFun) } /** * Describe a “subject” being specified and tested by the passed function value. The * passed function value may contain more describers (defined withdescribe
) and/or tests * (defined withit
). This trait's implementation of this method will register the * description string and immediately invoke the passed function. */ protected def describe(description: String)(f: => Unit) { if (atomic.get.registrationClosed) throw new TestRegistrationClosedException(Resources("describeCannotAppearInsideAnIt"), getStackDepth("Spec.scala", "describe")) def createNewBranch() = { val oldBundle = atomic.get var (trunk, currentBranch, tagsMap, testsList, registrationClosed) = oldBundle.unpack val newBranch = DescriptionBranch(currentBranch, description) val oldBranch = currentBranch currentBranch.subNodes ::= newBranch currentBranch = newBranch updateAtomic(oldBundle, Bundle(trunk, currentBranch, tagsMap, testsList, registrationClosed)) oldBranch } val oldBranch = createNewBranch() f val oldBundle = atomic.get val (trunk, currentBranch, tagsMap, testsList, registrationClosed) = oldBundle.unpack updateAtomic(oldBundle, Bundle(trunk, oldBranch, tagsMap, testsList, registrationClosed)) } /** * AMap
whose keys areString
tag names to which tests in thisSpec
belong, and values * theSet
of test names that belong to each tag. If thisSpec
contains no tags, this method returns an emptyMap
. * ** This trait's implementation returns tags that were passed as strings contained in
*/ override def tags: Map[String, Set[String]] = atomic.get.tagsMap private def runTestsInBranch(branch: Branch, reporter: Reporter, stopper: Stopper, filter: Filter, configMap: Map[String, Any], tracker: Tracker) { val stopRequested = stopper // Wrap any non-DispatchReporter, non-CatchReporter in a CatchReporter, // so that exceptions are caught and transformed // into error messages on the standard error stream. val report = wrapReporterIfNecessary(reporter) branch match { case desc @ DescriptionBranch(_, descriptionName) => def sendInfoProvidedMessage() { // Need to use the full name of the description, which includes all the descriptions it is nested inside // Call getPrefix and pass in this Desc, to get the full name val descriptionFullName = getPrefix(desc).trim report(InfoProvided(tracker.nextOrdinal(), descriptionFullName, Some(NameInfo(thisSuite.suiteName, Some(thisSuite.getClass.getName), None)), None, None, Some(IndentedText(descriptionFullName, descriptionFullName, 0)))) } // Only send an infoProvided message if the first thing in the subNodes is *not* sub-description, i.e., // it is a test, because otherwise we get a lame description that doesn't have any tests under it. // But send it if the list is empty. if (desc.subNodes.isEmpty) sendInfoProvidedMessage() else desc.subNodes.reverse.head match { case ex: FixtureTestLeaf[FixtureParam] => sendInfoProvidedMessage() case _ => // Do nothing in this case } case _ => } branch.subNodes.reverse.foreach( _ match { case FixtureTestLeaf(_, tn, specText, _) => if (!stopRequested()) { // TODO: Seems odd to me to check for stop here but still fire infos val (filterTest, ignoreTest) = filter(tn, tags) if (!filterTest) if (ignoreTest) { val testSucceededIcon = Resources("testSucceededIconChar") val formattedSpecText = Resources("iconPlusShortName", testSucceededIcon, specText) report(TestIgnored(tracker.nextOrdinal(), thisSuite.suiteName, Some(thisSuite.getClass.getName), tn, Some(IndentedText(formattedSpecText, specText, 1)))) } else runTest(tn, report, stopRequested, configMap, tracker) } case InfoLeaf(_, message) => val infoProvidedIcon = Resources("infoProvidedIconChar") val formattedText = Resources("iconPlusShortName", infoProvidedIcon, message) report(InfoProvided(tracker.nextOrdinal(), message, Some(NameInfo(thisSuite.suiteName, Some(thisSuite.getClass.getName), None)), None, None, Some(IndentedText(formattedText, message, 1)))) case branch: Branch => runTestsInBranch(branch, reporter, stopRequested, filter, configMap, tracker) } ) } /** * Run a test. This trait's implementation runs the test registered with the name specified by *Tag
objects passed to * methodstest
andignore
. *testName
. Each test's name is a concatenation of the text of all describers surrounding a test, * from outside in, and the test's spec text, with one space placed between each item. (See the documenation * fortestNames
for an example.) * * @param testName the name of one test to execute. * @param reporter theReporter
to which results will be reported * @param stopper theStopper
that will be consulted to determine whether to stop execution early. * @param configMap aMap
of properties that can be used by thisSpec
's executing tests. * @throws NullPointerException if any oftestName
,reporter
,stopper
, orconfigMap
* isnull
. */ protected override def runTest(testName: String, reporter: Reporter, stopper: Stopper, configMap: Map[String, Any], tracker: Tracker) { if (testName == null || reporter == null || stopper == null || configMap == null) throw new NullPointerException atomic.get.testsList.find(_.testName == testName) match { case None => throw new IllegalArgumentException("Requested test doesn't exist: " + testName) case Some(test) => { val report = wrapReporterIfNecessary(reporter) val testSucceededIcon = Resources("testSucceededIconChar") val formattedSpecText = Resources("iconPlusShortName", testSucceededIcon, test.specText) // Create a Rerunner if the Spec has a no-arg constructor val hasPublicNoArgConstructor = org.scalatest.Suite.checkForPublicNoArgConstructor(getClass) val rerunnable = if (hasPublicNoArgConstructor) Some(new TestRerunner(getClass.getName, testName)) else None val testStartTime = System.currentTimeMillis // A TestStarting event won't normally show up in a specification-style output, but // will show up in a test-style output. report(TestStarting(tracker.nextOrdinal(), thisSuite.suiteName, Some(thisSuite.getClass.getName), test.testName, Some(MotionToSuppress), rerunnable)) val formatter = IndentedText(formattedSpecText, test.specText, 1) val informerForThisTest = new MessageRecordingInformer(NameInfo(thisSuite.suiteName, Some(thisSuite.getClass.getName), Some(testName))) { def apply(message: String) { if (message == null) throw new NullPointerException if (shouldRecord) record(message) else { val infoProvidedIcon = Resources("infoProvidedIconChar") val formattedText = " " + Resources("iconPlusShortName", infoProvidedIcon, message) report(InfoProvided(tracker.nextOrdinal(), message, nameInfoForCurrentThread, None, None, Some(IndentedText(formattedText, message, 2)))) } } } val oldInformer = atomicInformer.getAndSet(informerForThisTest) var testWasPending = false var swapAndCompareSucceeded = false try { test.f match { case wrapper: NoArgTestWrapper[_] => withFixture(new FixturelessTestFunAndConfigMap(testName, wrapper.test, configMap)) case f => withFixture(new TestFunAndConfigMap(testName, f, configMap)) } val duration = System.currentTimeMillis - testStartTime report(TestSucceeded(tracker.nextOrdinal(), thisSuite.suiteName, Some(thisSuite.getClass.getName), test.testName, Some(duration), Some(formatter), rerunnable)) } catch { case _: TestPendingException => report(TestPending(tracker.nextOrdinal(), thisSuite.suiteName, Some(thisSuite.getClass.getName), test.testName, Some(formatter))) testWasPending = true case e if !anErrorThatShouldCauseAnAbort(e) => val duration = System.currentTimeMillis - testStartTime handleFailedTest(e, false, test.testName, test.specText, formattedSpecText, rerunnable, report, tracker, duration) case e => throw e } finally { // send out any recorded messages for (message <- informerForThisTest.recordedMessages) { val infoProvidedIcon = Resources("infoProvidedIconChar") val formattedText = " " + Resources("iconPlusShortName", infoProvidedIcon, message) report(InfoProvided(tracker.nextOrdinal(), message, informerForThisTest.nameInfoForCurrentThread, Some(testWasPending), None, Some(IndentedText(formattedText, message, 2)))) } val shouldBeInformerForThisTest = atomicInformer.getAndSet(oldInformer) swapAndCompareSucceeded = shouldBeInformerForThisTest eq informerForThisTest } if (!swapAndCompareSucceeded) // Do outside finally to workaround Scala compiler bug throw new ConcurrentModificationException(Resources("concurrentInformerMod", thisSuite.getClass.getName)) } } } private def handleFailedTest(throwable: Throwable, hasPublicNoArgConstructor: Boolean, testName: String, specText: String, formattedSpecText: String, rerunnable: Option[Rerunner], report: Reporter, tracker: Tracker, duration: Long) { val message = if (throwable.getMessage != null) // [bv: this could be factored out into a helper method] throwable.getMessage else throwable.toString val formatter = IndentedText(formattedSpecText, specText, 1) report(TestFailed(tracker.nextOrdinal(), message, thisSuite.suiteName, Some(thisSuite.getClass.getName), testName, Some(throwable), Some(duration), Some(formatter), rerunnable)) } /** ** Run zero to many of this
* *Spec
's tests. ** This method takes a
* *testName
parameter that optionally specifies a test to invoke. * IftestName
isSome
, this trait's implementation of this method * invokesrunTest
on this object, passing in: **
* *- *
testName
- theString
value of thetestName
Option
passed * to this method- *
reporter
- theReporter
passed to this method, or one that wraps and delegates to it- *
stopper
- theStopper
passed to this method, or one that wraps and delegates to it- *
configMap
- theconfigMap
passed to this method, or one that wraps and delegates to it* This method takes a
* *Set
of tag names that should be included (tagsToInclude
), and aSet
* that should be excluded (tagsToExclude
), when deciding which of thisSuite
's tests to execute. * IftagsToInclude
is empty, all tests will be executed * except those those belonging to tags listed in thetagsToExclude
Set
. IftagsToInclude
is non-empty, only tests * belonging to tags mentioned intagsToInclude
, and not mentioned intagsToExclude
* will be executed. However, iftestName
isSome
,tagsToInclude
andtagsToExclude
are essentially ignored. * Only iftestName
isNone
willtagsToInclude
andtagsToExclude
be consulted to * determine which of the tests named in thetestNames
Set
should be run. For more information on trait tags, see the main documentation for this trait. ** If
* *testName
isNone
, this trait's implementation of this method * invokestestNames
on thisSuite
to get aSet
of names of tests to potentially execute. * (AtestNames
value ofNone
essentially acts as a wildcard that means all tests in * thisSuite
that are selected bytagsToInclude
andtagsToExclude
should be executed.) * For each test in thetestName
Set
, in the order * they appear in the iterator obtained by invoking theelements
method on theSet
, this trait's implementation * of this method checks whether the test should be run based on thetagsToInclude
andtagsToExclude
Set
s. * If so, this implementation invokesrunTest
, passing in: **
* * @param testName an optional name of one test to execute. If- *
testName
- theString
name of the test to run (which will be one of the names in thetestNames
Set
)- *
reporter
- theReporter
passed to this method, or one that wraps and delegates to it- *
stopper
- theStopper
passed to this method, or one that wraps and delegates to it- *
configMap
- theconfigMap
passed to this method, or one that wraps and delegates to itNone
, all relevant tests should be executed. * I.e.,None
acts like a wildcard that means execute all relevant tests in thisSpec
. * @param reporter theReporter
to which results will be reported * @param stopper theStopper
that will be consulted to determine whether to stop execution early. * @param tagsToInclude aSet
ofString
tag names to include in the execution of thisSpec
* @param tagsToExclude aSet
ofString
tag names to exclude in the execution of thisSpec
* @param configMap aMap
of key-value pairs that can be used by thisSpec
's executing tests. * @throws NullPointerException if any oftestName
,reporter
,stopper
,tagsToInclude
, *tagsToExclude
, orconfigMap
isnull
. */ protected override def runTests(testName: Option[String], reporter: Reporter, stopper: Stopper, filter: Filter, configMap: Map[String, Any], distributor: Option[Distributor], tracker: Tracker) { if (testName == null) throw new NullPointerException("testName was null") if (reporter == null) throw new NullPointerException("reporter was null") if (stopper == null) throw new NullPointerException("stopper was null") if (filter == null) throw new NullPointerException("filter was null") if (configMap == null) throw new NullPointerException("configMap was null") if (distributor == null) throw new NullPointerException("distributor was null") if (tracker == null) throw new NullPointerException("tracker was null") val stopRequested = stopper testName match { case None => runTestsInBranch(atomic.get.trunk, reporter, stopRequested, filter, configMap, tracker) case Some(tn) => runTest(tn, reporter, stopRequested, configMap, tracker) } } /** * An immutableSet
of test names. If thisFixtureFeatureSpec
contains no tests, this method returns an * emptySet
. * ** This trait's implementation of this method will return a set that contains the names of all registered tests. The set's * iterator will return those names in the order in which the tests were registered. Each test's name is composed * of the concatenation of the text of each surrounding describer, in order from outside in, and the text of the * example itself, with all components separated by a space. *
*/ override def testNames: Set[String] = ListSet(atomic.get.testsList.map(_.testName): _*) override def run(testName: Option[String], reporter: Reporter, stopper: Stopper, filter: Filter, configMap: Map[String, Any], distributor: Option[Distributor], tracker: Tracker) { val stopRequested = stopper // Set the flag that indicates registration is closed (because run has now been invoked), // which will disallow any further invocations of "describe", it", or "ignore" with // an RegistrationClosedException. val oldBundle = atomic.get var (trunk, currentBranch, tagsMap, testsList, registrationClosed) = oldBundle.unpack if (!registrationClosed) updateAtomic(oldBundle, Bundle(trunk, currentBranch, tagsMap, testsList, true)) val report = wrapReporterIfNecessary(reporter) val informerForThisSuite = new ConcurrentInformer(NameInfo(thisSuite.suiteName, Some(thisSuite.getClass.getName), None)) { def apply(message: String) { if (message == null) throw new NullPointerException report(InfoProvided(tracker.nextOrdinal(), message, nameInfoForCurrentThread)) } } atomicInformer.set(informerForThisSuite) var swapAndCompareSucceeded = false try { super.run(testName, report, stopRequested, filter, configMap, distributor, tracker) } finally { val shouldBeInformerForThisSuite = atomicInformer.getAndSet(zombieInformer) swapAndCompareSucceeded = shouldBeInformerForThisSuite eq informerForThisSuite } if (!swapAndCompareSucceeded) // Do outside finally to workaround Scala compiler bug throw new ConcurrentModificationException(Resources("concurrentInformerMod", thisSuite.getClass.getName)) } /** * Supports shared test registration inFixtureSpec
s. * ** This field supports syntax such as the following: *
* ** it should behave like nonFullStack(stackWithOneItem) * ^ ** ** For more information and examples of the use of
behave Spec
. * */ protected val behave = new BehaveWord /** * Implicitly converts a function that takes no parameters and results inPendingNothing
to * a function fromFixture
toAny
, to enable pending tests to registered as by-name parameters * by methods that require a test function that takes aFixture
. * ** This method makes it possible to write pending tests as simply
*/ protected implicit def convertPendingToFixtureFunction(f: => PendingNothing): FixtureParam => Any = { fixture => f } /** * Implicitly converts a function that takes no parameters and results in(pending)
, without needing * to write(fixture => pending)
. *Any
to * a function fromFixture
toAny
, to enable no-arg tests to registered * by methods that require a test function that takes aFixture
. */ protected implicit def convertNoArgToFixtureFunction(fun: () => Any): (FixtureParam => Any) = new NoArgTestWrapper(fun) }