org.scalatest.fixture

FixtureFunSuite

trait FixtureFunSuite extends FixtureSuite

A sister trait to org.scalatest.FunSuite that can pass a fixture object into its tests.

This trait behaves similarly to trait org.scalatest.FunSuite, except that tests may take a fixture object. The type of the fixture object passed is defined by the abstract Fixture type, which is declared as a member of this trait (inherited from supertrait FixtureSuite). This trait also inherits the abstract method withFixture from supertrait FixtureSuite. The withFixture method takes a OneArgTest, which is a nested trait defined as a member of supertrait FixtureSuite.OneArgTest has an apply method that takes a Fixture. This apply method is responsible for running a test. This trait's runTest method delegates the actual running of each test to withFixture, passing in the test code to run via the OneArgTest argument. The withFixture 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.FunSuite:

Here's an example:

import org.scalatest.fixture.FixtureFunSuite
import java.io.FileReader
import java.io.FileWriter
import java.io.File

class MyFunSuite extends FixtureFunSuite {

// 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 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') }

// (You can also write tests that don't take a fixture parameter.) test("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.FixtureFunSuite
import scala.collection.mutable.ListBuffer

class MyFunSuite extends FixtureFunSuite {

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) }

test("easy") { fixture => val (builder, buffer) = fixture builder.append("easy!") assert(builder.toString === "ScalaTest is easy!") assert(buffer.isEmpty) buffer += "sweet" }

test("fun") { 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.FixtureFunSuite
import scala.collection.mutable.ListBuffer

class MyFunSuite extends FixtureFunSuite {

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)) }

test("easy") { fixture => import fixture._ builder.append("easy!") assert(builder.toString === "ScalaTest is easy!") assert(buffer.isEmpty) buffer += "sweet" }

test("fun") { 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:

test("easy") { 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:

test("fun") { 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 a configMapmethod, which will return a Map[String, Any] from which configuration information may be obtained. The runTest method of this trait will pass a OneArgTest to withFixturewhose configMap method returns the configMap passed to runTest. Here's an example in which the name of a temp file is taken from the passed configMap:

import org.scalatest.fixture.FixtureFunSuite
import java.io.FileReader
import java.io.FileWriter
import java.io.File

class MyFunSuite extends FixtureFunSuite {

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 to runTest, you can mix in trait ConfigMapFixture. See the documentation for ConfigMapFixture for the details, but here's a quick example of how it looks:

 import org.scalatest.fixture.FixtureFunSuite
 import org.scalatest.fixture.ConfigMapFixture

class MyFunSuite extends FixtureFunSuite with ConfigMapFixture {

test("hello") { configMap => // Use the configMap passed to runTest in the test assert(configMap.contains("hello") }

test("world") { configMap => assert(configMap.contains("world") } }

ConfigMapFixture can also be used to facilitate writing FixtureFunSuites that include tests that take different fixture types. See the documentation for MultipleFixtureFunSuite for more information.

known subclasses: MultipleFixtureFunSuite
    authors:
  1. Bill Venners

Inherited
  1. Hide All
  2. Show all
  1. FixtureSuite
  2. Suite
  3. AbstractSuite
  4. Assertions
  5. AnyRef
  6. 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

    The type of the fixture parameter that can be passed into tests in this suite.

  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): (FixtureParam) ⇒ 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): (FixtureParam) ⇒ 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 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.

  31. def getClass(): java.lang.Class[_ <: java.lang.Object]

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

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

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

  33. def hashCode(): Int

    Returns a hash code value for the object.

  34. def ignore(testName: String, testTags: Tag*)(f: (FixtureParam) ⇒ Any): Unit

    Register a test to ignore, which has the specified name, optional tags, and function value that takes no arguments.

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

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

  37. def isInstanceOf[T0]: Boolean

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

  38. def ne(arg0: AnyRef): Boolean

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

  39. def nestedSuites: List[Suite]

    A List of this Suite object's nested Suites.

  40. def notify(): Unit

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

  41. def notifyAll(): Unit

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

  42. def pending: PendingNothing

    Throws TestPendingException to indicate a test is pending.

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

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

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

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

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

    Run a test.

  47. 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 Suite's tests.

  48. def suiteName: String

    A user-friendly suite name for this Suite.

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

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

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

  51. def test(testName: String, testTags: Tag*)(f: (FixtureParam) ⇒ Any): Unit

    Register a test with the specified name, optional tags, and function value that takes no arguments.

  52. def testNames: Set[String]

    An immutable Set of test names.

  53. def testsFor(unit: Unit): Unit

    Registers shared tests.

  54. def toString(): String

    Returns a string representation of the object.

  55. def wait(): Unit

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

  57. def wait(arg0: Long): Unit

  58. def withFixture(test: NoArgTest): Unit

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

  59. def withFixture(test: OneArgTest): Unit

    Run the passed test function with a fixture created by this method.