org.scalatest.fixture

MultipleFixtureFeatureSpec

trait MultipleFixtureFeatureSpec extends FixtureFeatureSpec with ConfigMapFixture

A sister trait to org.scalatest.FeatureSpec that can pass multiple types of fixture objects into its tests.

This trait behaves similarly to trait org.scalatest.FeatureSpec, except that tests may take a fixture object, and unlike a FixtureFeatureSpec, different tests may take different types of fixtures. This trait extends FixtureFeatureSpecand mixes in ConfigMapFixture, which defines the Fixture type to be the configMap's type (Map[String, Any]) and defines the withFixture method to simply pass the configMapto the test function. To write tests that take fixtures of types other than Fixture (i.e.,Map[String, Any]), you can define implicit conversions from a function of type (<the fixture type>) => Unitto a function of type (FixtureParam) => Unit. Each implicit conversion method serves as the with-fixture method for that type.

Subclasses of this trait must, therefore, do two things differently from a plain old org.scalatest.FeatureSpec:

Here's an example that has two fixture types, String and List[Int]:

import org.scalatest.fixture.MultipleFixtureFeatureSpec

class MyFeatureSpec extends MultipleFixtureFeatureSpec {

// The "with-fixture" method for tests that take a String fixture implicit def withStringFixture(testFunction: String => Unit): FixtureParam => Unit = testFunction("howdy")

// The "with-fixture" method for tests that take a List[Int] fixture implicit def withListFixture(testFunction: List[Int] => Unit): FixtureParam => Unit = configMap => testFunction(List(configMap.size))

// A test that takes a String fixture scenario("takes a string fixture") { (s: String) => assert(s === "howdy") }

// A test that takes a List[Int] fixture scenario("takes a list fixture") { (list: List[Int]) => assert(list.size === 1) }

// A test that takes no fixture scenario("takes no fixture") { () => assert(1 === 1) } }

The first method in this class, withStringFixture, is the implicit conversion function for tests that take a fixture of type String. In this contrived example, the hard-coded string "howdy" is passed into the test:

implicit def withStringFixture(testFunction: String => Unit): FixtureParam => Unit =
  testFunction("howdy")

Although the result type of this implicit conversion method is FixtureParam => Unit, if a fixture doesn't need anything from the configMap, you can leave off the configMap => at the beginning of the result function. The reason this works is that MultipleFixtureFeatureSpec inherits an implicit conversion from a by-name parameter toFixtureParam => Unit from supertrait FixtureFeatureSpec. This implicit conversion is used to enable tests that take no fixture (such as the one named takes no fixture in this example) to be included in the same class as tests that take type Fixture. That same implicit conversion is used here to allow you to leave off the configMap => except when you actually need something from the configMap. By leaving it off, you indicte to readers of the code that this fixture doesn't require anything from the configMap.

The next method in this class, withListFixture, is the implicit conversion function for tests that take a fixture of type List[Int]. In this contrived example, a List[Int] that contains one element, the size of the configMap, is passed to the test function. Because the fixture uses the configMap, it has an explicit configMap => parameter at the beginning of the result. (Remember, the Fixturetype is defined to be Map[String, Any], the type of the configMap, in supertrait ConfigMapFixture.

Following the implicit conversion methods are the test declarations. One test is written to take the String fixture:

scenario("takes a string fixture") { (s: String) =>
  assert(s === "howdy")
}

What happens at compile time is that because the Fixture type is Map[String, Any], the test method should be passed a function from type (Map[String, Any]) => Unit, or using the type alias, (FixtureParam) => Unit. Passing a function of type String => Unit as is attempted here is a type error. Thus the compiler will look around for an implicit conversion that will fix the type error, and will find the withStringFixture method. Because this is the only implicit conversion that fixes the type error, it will apply it, effectively generating this code:

// after the implicit withStringFixture method is applied by the compiler
scenario("takes a string fixture") {
  withStringFixture { (s: String) =>
    assert(s === "howdy")
  }
}

After passing the (String) => Unit function to withStringFixture, the result will be of type (FixtureParam) => Unit, which the test method expects.

The next test is written to take the List[Int] fixture:

scenario("takes a list fixture") { (list: List[Int]) =>
  assert(list.size === 1)
}

The compiler will apply the withListFixture implicit conversion in this case, effectively generating the following code:

scenario("takes a list fixture") {
  withListFixture { (list: List[Int]) =>
    assert(list.size === 1)
  }
}

Note that in a FixtureFeatureSpec, you need to specify the type of the fixture explicitly so the compiler knows the type to convert from. So you must, for example, write:

scenario("takes a list fixture") { (list: List[Int]) =>
  assert(list.size === 1)
}

The following attempt will fail to compile:

// won't compile, because list is inferred to be of type FixtureParam
scenario("takes a list fixture") { list =>
  assert(list.size === 1)
}

    authors:
  1. Bill Venners

Inherited
  1. Hide All
  2. Show all
  1. ConfigMapFixture
  2. FixtureFeatureSpec
  3. FixtureSuite
  4. Suite
  5. AbstractSuite
  6. Assertions
  7. AnyRef
  8. Any
Visibility
  1. Public
  2. All

Type Members

  1. class Equalizer extends AnyRef

    Class used via an implicit conversion to enable any two objects to be compared with=== in assertions in tests.

  2. type FixtureParam = Map[String, Any]

    The type of the configMap, which is Map[String, Any].

  3. trait NoArgTest extends () ⇒ Unit

    A test function taking no arguments, which also provides a test name and config map.

  4. trait OneArgTest extends (FixtureParam) ⇒ Unit

    Trait whose instances encapsulate a test function that takes a fixture and config map.

Value Members

  1. def !=(arg0: AnyRef): Boolean

  2. def !=(arg0: Any): Boolean

    o != arg0 is the same as !(o == (arg0)).

  3. def ##(): Int

  4. def $asInstanceOf[T0](): T0

  5. def $isInstanceOf[T0](): Boolean

  6. def ==(arg0: AnyRef): Boolean

    o == arg0 is the same as if (o eq null) arg0 eq null else o.equals(arg0).

  7. def ==(arg0: Any): Boolean

    o == arg0 is the same as o.equals(arg0).

  8. def asInstanceOf[T0]: T0

    This method is used to cast the receiver object to be of type T0.

  9. def assert(o: Option[String]): Unit

    Assert that an Option[String] is None.

  10. def assert(o: Option[String], clue: Any): Unit

    Assert that an Option[String] is None.

  11. def assert(condition: Boolean, clue: Any): Unit

    Assert that a boolean condition, described in Stringmessage, is true.

  12. def assert(condition: Boolean): Unit

    Assert that a boolean condition is true.

  13. def clone(): AnyRef

    This method creates and returns a copy of the receiver object.

  14. implicit def convertNoArgToFixtureFunction(fun: () ⇒ Any): (Map[String, Any]) ⇒ Any

    Implicitly converts a function that takes no parameters and results in Any to a function from Fixture to Any, to enable no-arg tests to registered by methods that require a test function that takes a Fixture.

  15. implicit def convertPendingToFixtureFunction(f: ⇒ PendingNothing): (Map[String, Any]) ⇒ Any

    Implicitly converts a function that takes no parameters and results in PendingNothing to a function from Fixture to Any, to enable pending tests to registered as by-name parameters by methods that require a test function that takes a Fixture.

  16. implicit def convertToEqualizer(left: Any): Equalizer

    Implicit conversion from Any to Equalizer, used to enable assertions with === comparisons.

  17. def eq(arg0: AnyRef): Boolean

    This method is used to test whether the argument (arg0) is a reference to the receiver object (this).

  18. def equals(arg0: Any): Boolean

    This method is used to compare the receiver object (this) with the argument object (arg0) for equivalence.

  19. def execute(testName: String, configMap: Map[String, Any]): Unit

    Executes the test specified as testName in this Suite with the specified configMap, printing results to the standard output.

  20. def execute(testName: String): Unit

    Executes the test specified as testName in this Suite, printing results to the standard output.

  21. def execute(configMap: Map[String, Any]): Unit

    Executes this Suite with the specified configMap, printing results to the standard output.

  22. def execute(): Unit

    Executes this Suite, printing results to the standard output.

  23. def expect(expected: Any)(actual: Any): Unit

    Expect that the value passed as expected equals the value passed as actual.

  24. def expect(expected: Any, clue: Any)(actual: Any): Unit

    Expect that the value passed as expected equals the value passed as actual.

  25. def expectedTestCount(filter: Filter): Int

    The total number of tests that are expected to run when this Suite's run method is invoked.

  26. def fail(cause: Throwable): Nothing

    Throws TestFailedException, with the passedThrowable cause, to indicate a test failed.

  27. def fail(message: String, cause: Throwable): Nothing

    Throws TestFailedException, with the passedString message as the exception's detail message and Throwable cause, to indicate a test failed.

  28. def fail(message: String): Nothing

    Throws TestFailedException, with the passedString message as the exception's detail message, to indicate a test failed.

  29. def fail(): Nothing

    Throws TestFailedException to indicate a test failed.

  30. def feature(description: String)(f: ⇒ Unit): Unit

    Describe a “subject” being specified and tested by the passed function value.

  31. def finalize(): Unit

    This method is called by the garbage collector on the receiver object when garbage collection determines that there are no more references to the object.

  32. def getClass(): java.lang.Class[_]

    Returns a representation that corresponds to the dynamic class of the receiver object.

  33. def groups: Map[String, Set[String]]

    The groups methods has been deprecated and will be removed in a future version of ScalaTest.

  34. def hashCode(): Int

    Returns a hash code value for the object.

  35. def ignore(specText: String)(testFun: (Map[String, Any]) ⇒ Any): Unit

    Register a test to ignore, which has the given spec text and test function value that takes no arguments.

  36. def ignore(specText: String, testTags: Tag*)(testFun: (Map[String, Any]) ⇒ Any): Unit

    Register a test to ignore, which has the given spec text, optional tags, and test function value that takes no arguments.

  37. implicit def info: Informer

    Returns an Informer that during test execution will forward strings (and other objects) passed to itsapply method to the current reporter.

  38. def intercept[T <: AnyRef](f: ⇒ Any)(implicit manifest: Manifest[T]): T

    Intercept and return an exception that's expected to be thrown by the passed function value.

  39. def isInstanceOf[T0]: Boolean

    This method is used to test whether the dynamic type of the receiver object is T0.

  40. def ne(arg0: AnyRef): Boolean

    o.ne(arg0) is the same as !(o.eq(arg0)).

  41. def nestedSuites: List[Suite]

    A List of this Suite object's nested Suites.

  42. def notify(): Unit

    Wakes up a single thread that is waiting on the receiver object's monitor.

  43. def notifyAll(): Unit

    Wakes up all threads that are waiting on the receiver object's monitor.

  44. def pending: PendingNothing

    Throws TestPendingException to indicate a test is pending.

  45. def pendingUntilFixed(f: ⇒ Unit): Unit

    Execute the passed block of code, and if it completes abruptly, throw TestPendingException, else throw TestFailedException.

  46. def run(testName: Option[String], reporter: Reporter, stopper: Stopper, filter: Filter, configMap: Map[String, Any], distributor: Option[Distributor], tracker: Tracker): Unit

    Runs this suite of tests.

  47. def runNestedSuites(reporter: Reporter, stopper: Stopper, filter: Filter, configMap: Map[String, Any], distributor: Option[Distributor], tracker: Tracker): Unit

    Run zero to many of this Suite's nested Suites.

  48. def runTest(testName: String, reporter: Reporter, stopper: Stopper, configMap: Map[String, Any], tracker: Tracker): Unit

    Run a test.

  49. def runTests(testName: Option[String], reporter: Reporter, stopper: Stopper, filter: Filter, configMap: Map[String, Any], distributor: Option[Distributor], tracker: Tracker): Unit

    Run zero to many of this Spec's tests.

  50. def scenario(specText: String)(testFun: (Map[String, Any]) ⇒ Any): Unit

    Register a test with the given spec text and test function value that takes no arguments.

  51. def scenario(specText: String, testTags: Tag*)(testFun: (Map[String, Any]) ⇒ Any): Unit

    Register a test with the given spec text, optional tags, and test function value that takes no arguments.

  52. def scenariosFor(unit: Unit): Unit

    Registers shared scenarios.

  53. def suiteName: String

    A user-friendly suite name for this Suite.

  54. def synchronized[T0](arg0: T0): T0

  55. def tags: Map[String, Set[String]]

    A Map whose keys are String tag names to which tests in this Spec belong, and values the Set of test names that belong to each tag.

  56. def testNames: Set[String]

    An immutable Set of test names.

  57. def toString(): String

    Returns a string representation of the object.

  58. def wait(): Unit

  59. def wait(arg0: Long, arg1: Int): Unit

  60. def wait(arg0: Long): Unit

  61. def withClue(clue: Any)(fun: ⇒ Unit): Unit

    Executes the block of code passed as the second parameter, and, if it completes abruptly with a ModifiableMessage exception, prepends the "clue" string passed as the first parameter to the beginning of the detail message of that thrown exception, then rethrows it.

  62. def withFixture(test: OneArgTest): Unit

    Invoke the test function, passing to the the test function the configMapobtained by invoking configMap on the passed OneArgTest.

  63. def withFixture(test: NoArgTest): Unit

    Run the passed test function in the context of a fixture established by this method.