org.scalatest

trait OneInstancePerTest

[source: org/scalatest/OneInstancePerTest.scala]

trait OneInstancePerTest
extends AbstractSuite with AnyRef
Trait that facilitates a style of testing in which each test is run in its own instance of the suite class to isolate each test from the side effects of the other tests in the suite.

If you mix this trait into a Suite, you can initialize shared reassignable fixture variables as well as shared mutable fixture objects in the constructor of the class. Because each test will run in its own instance of the class, each test will get a fresh copy of the instance variables. This is the approach to test isolation taken, for example, by the JUnit framework.

Author
Bill Venners
Direct Known Subclasses:
ParallelTestExecution

Method Summary
def newInstance : Suite
Construct a new instance of this Suite.
protected def runTests (testName : scala.Option[java.lang.String], reporter : Reporter, stopper : Stopper, filter : Filter, configMap : scala.collection.immutable.Map[java.lang.String, Any], distributor : scala.Option[Distributor], tracker : Tracker) : Unit
Run this Suite's tests each in their own instance of this Suite's class.
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), runTest (abstract), testNames (abstract), nestedSuites (abstract), tags (abstract), expectedTestCount (abstract)
Method Details
protected def runTests(testName : scala.Option[java.lang.String], reporter : Reporter, stopper : Stopper, filter : Filter, configMap : scala.collection.immutable.Map[java.lang.String, Any], distributor : scala.Option[Distributor], tracker : Tracker) : Unit
Run this Suite's tests each in their own instance of this Suite's class.

If the passed testName is None, this trait's implementation of this method will for each test name returned by testNames, invoke newInstance to get a new instance of this Suite, and call run on it, passing in the test name wrapped in a Some. If the passed testName is defined, this trait's implementation of this method will simply forward all passed parameters to super.run. If the invocation of either newInstance on this Suite or run on a newly created instance of this Suite completes abruptly with an exception, then this runTests method will complete abruptly with the same exception.

Parameters
testName - an optional name of one test to run. If None, all relevant tests should be run. I.e., None acts like a wildcard that means run all relevant tests in this Suite.
reporter - the Reporter to which results will be reported
stopper - the Stopper that will be consulted to determine whether to stop execution early.
filter - a Filter with which to filter tests based on their tags
configMap - a Map of key-value pairs that can be used by the executing Suite of tests.
distributor - an optional Distributor, into which to put nested Suites to be run by another entity, such as concurrently by a pool of threads. If None, nested Suites will be run sequentially.
tracker - a Tracker tracking Ordinals being fired by the current thread.
Throws
NullPointerException - if any of the passed parameters is null.
IllegalArgumentException - if testName is defined, but no test with the specified test name exists in this Suite
Overrides
AbstractSuite.runTests

def newInstance : Suite
Construct a new instance of this Suite.

This trait's implementation of runTests invokes this method to create a new instance of this Suite for each test. This trait's implementation of this method uses reflection to call this.getClass.newInstance. This approach will succeed only if this Suite's class has a public, no-arg constructor. In most cases this is likely to be true, because to be instantiated by ScalaTest's Runner a Suite needs a public, no-arg constructor. However, this will not be true of any Suite defined as an inner class of another class or trait, because every constructor of an inner class type takes a reference to the enclosing instance. In such cases, and in cases where a Suite class is explicitly defined without a public, no-arg constructor, you will need to override this method to construct a new instance of the Suite in some other way.

Here's an example of how you could override newInstance to construct a new instance of an inner class:

   import org.scalatest.Suite
  
   class Outer {
     class InnerSuite extends Suite with OneInstancePerTest {
       def testOne() {}
       def testTwo() {}
       override def newInstance = new InnerSuite
     }
   }
   


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