org.scalatest.fixture

MultipleFixtureFlatSpec

trait MultipleFixtureFlatSpec extends FixtureFlatSpec with ConfigMapFixture

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

This trait behaves similarly to trait org.scalatest.FlatSpec, except that tests may take a fixture object, and unlike a FixtureFlatSpec, different tests may take different types of fixtures. This trait extends FixtureFlatSpecand 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.FlatSpec:

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

import org.scalatest.fixture.MultipleFixtureFlatSpec

class MyFlatSpec extends MultipleFixtureFlatSpec {

// 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 it should "take a string fixture" in { (s: String) => assert(s === "howdy") }

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

// A test that takes no fixture it should "take no fixture" in { () => 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 MultipleFixtureFlatSpec inherits an implicit conversion from a by-name parameter toFixtureParam => Unit from supertrait FixtureFlatSpec. 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:

it should "take a string fixture" in { (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
it should "take a string fixture" in {
  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:

it should "take a list fixture" in { (list: List[Int]) =>
  assert(list.size === 1)
}

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

it should "take a list fixture" in {
  withListFixture { (list: List[Int]) =>
    assert(list.size === 1)
  }
}

Note that in a FixtureFlatSpec, 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:

it should "take a list fixture" in { (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
it should "take a list fixture" in { list =>
  assert(list.size === 1)
}

    authors:
  1. Bill Venners

Inherited
  1. Hide All
  2. Show all
  1. ConfigMapFixture
  2. FixtureFlatSpec
  3. CanVerb
  4. MustVerb
  5. ShouldVerb
  6. FixtureSuite
  7. Suite
  8. AbstractSuite
  9. Assertions
  10. AnyRef
  11. Any
Visibility
  1. Public
  2. All

Type Members

  1. class BehaviorWord extends AnyRef

    Class that supports the registration of a “subject” being specified and tested via the instance referenced from FixtureFlatSpec's behavior field.

  2. class Equalizer extends AnyRef

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

  3. type FixtureParam = Map[String, Any]

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

  4. class IgnoreVerbString extends AnyRef

    Class that supports registration of ignored tests via the IgnoreWord instance referenced from FixtureFlatSpec's ignore field.

  5. class IgnoreVerbStringTaggedAs extends AnyRef

    Class that supports registration of ignored, tagged tests via the IgnoreWord instance referenced from FixtureFlatSpec's ignore field.

  6. class IgnoreWord extends AnyRef

    Class that supports registration of ignored tests via the instance referenced from FixtureFlatSpec's ignore field.

  7. class InAndIgnoreMethods extends AnyRef

    Class that supports test registration in shorthand form.

  8. class InAndIgnoreMethodsAfterTaggedAs extends AnyRef

    Class that supports tagged test registration in shorthand form.

  9. class ItVerbString extends AnyRef

    Class that supports test registration via the instance referenced from FixtureFlatSpec's it field.

  10. class ItVerbStringTaggedAs extends AnyRef

    Class that supports the registration of tagged tests via the ItWord instance referenced from FixtureFlatSpec's it field.

  11. class ItWord extends AnyRef

    Class that supports test (and shared test) registration via the instance referenced from FixtureFlatSpec's it field.

  12. trait NoArgTest extends () ⇒ Unit

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

  13. trait OneArgTest extends (FixtureParam) ⇒ Unit

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

  14. class StringCanWrapperForVerb extends AnyRef

    This class supports the syntax of FlatSpec, WordSpec, FixtureFlatSpec, and FixtureWordSpec.

  15. class StringMustWrapperForVerb extends AnyRef

    This class supports the syntax of FlatSpec, WordSpec, FixtureFlatSpec, and FixtureWordSpec.

  16. class StringShouldWrapperForVerb extends AnyRef

    This class supports the syntax of FlatSpec, WordSpec, FixtureFlatSpec, and FixtureWordSpec.

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. val behave: BehaveWord

    Supports shared test registration in FixtureFlatSpecs.

  14. val behavior: BehaviorWord

    Supports the registration of a “subject” being specified and tested.

  15. def clone(): AnyRef

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

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

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

  17. implicit def convertToInAndIgnoreMethods(resultOfStringPassedToVerb: ResultOfStringPassedToVerb): InAndIgnoreMethods

    Implicitly converts an object of type ResultOfStringPassedToVerb to anInAndIgnoreMethods, to enable in and ignoremethods to be invokable on that object.

  18. implicit def convertToInAndIgnoreMethodsAfterTaggedAs(resultOfTaggedAsInvocation: ResultOfTaggedAsInvocation): InAndIgnoreMethodsAfterTaggedAs

    Implicitly converts an object of type ResultOfTaggedAsInvocation to anInAndIgnoreMethodsAfterTaggedAs, to enable in and ignoremethods to be invokable on that object.

  19. implicit def convertToStringCanWrapper(o: String): StringCanWrapperForVerb

    Implicitly converts an object of type String to a StringCanWrapper, to enable can methods to be invokable on that object.

  20. implicit def convertToStringMustWrapper(o: String): StringMustWrapperForVerb

    Implicitly converts an object of type String to a StringMustWrapper, to enable must methods to be invokable on that object.

  21. implicit def convertToStringShouldWrapper(o: String): StringShouldWrapperForVerb

    Implicitly converts an object of type String to a StringShouldWrapperForVerb, to enable should methods to be invokable on that object.

  22. def eq(arg0: AnyRef): Boolean

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

  23. def equals(arg0: Any): Boolean

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

  24. 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.

  25. def execute(testName: String): Unit

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

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

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

  27. def execute(): Unit

    Executes this Suite, printing results to the standard output.

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

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

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

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

  30. def expectedTestCount(filter: Filter): Int

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

  31. def fail(cause: Throwable): Nothing

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

  32. 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.

  33. def fail(message: String): Nothing

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

  34. def fail(): Nothing

    Throws TestFailedException to indicate a test failed.

  35. 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.

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

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

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

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

  38. def hashCode(): Int

    Returns a hash code value for the object.

  39. val ignore: IgnoreWord

    Supports registration of ignored tests in FixtureFlatSpecs.

  40. 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.

  41. 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.

  42. def isInstanceOf[T0]: Boolean

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

  43. val it: ItWord

    Supports test (and shared test) registration in FixtureFlatSpecs.

  44. def ne(arg0: AnyRef): Boolean

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

  45. def nestedSuites: List[Suite]

    A List of this Suite object's nested Suites.

  46. def notify(): Unit

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

  47. def notifyAll(): Unit

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

  48. def pending: PendingNothing

    Throws TestPendingException to indicate a test is pending.

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

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

  50. 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.

  51. 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.

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

    Run a test.

  53. 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.

  54. implicit val shorthandSharedTestRegistrationFunction: (String) ⇒ BehaveWord

    Supports the shorthand form of shared test registration.

  55. implicit val shorthandTestRegistrationFunction: (String, String, String) ⇒ ResultOfStringPassedToVerb

    Supports the shorthand form of test registration.

  56. def suiteName: String

    A user-friendly suite name for this Suite.

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

  58. 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.

  59. def testNames: Set[String]

    An immutable Set of test names.

  60. def toString(): String

    Returns a string representation of the object.

  61. def wait(): Unit

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

  63. def wait(arg0: Long): Unit

  64. 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.

  65. def withFixture(test: OneArgTest): Unit

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

  66. def withFixture(test: NoArgTest): Unit

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