A sister class to org.scalatest.Spec
that can pass a fixture object into its tests.
Implementation trait for class fixture.Spec
, which is
a sister class to org.scalatest.Spec
that can pass a
fixture object into its tests.
Implementation trait for class fixture.Spec
, which is
a sister class to org.scalatest.Spec
that can pass a
fixture object into its tests.
fixture.Spec
is a class,
not a trait, to minimize compile time given there is a slight compiler
overhead to mixing in traits compared to extending classes. If you need
to mix the behavior of fixture.Spec
into some other
class, you can use this trait instead, because class
fixture.Spec
does nothing more than extend this trait and add a nice toString
implementation.
See the documentation of the class for a detailed
overview of fixture.Spec
.
A sister class to
org.scalatest.Spec
that can pass a fixture object into its tests.fixture.Spec
in situations for whichSpec
would be a good choice, when all or most tests need the same fixture objects that must be cleaned up afterwards. Note:fixture.Spec
is intended for use in special situations, with classSpec
used for general needs. For more insight into wherefixture.Spec
fits in the big picture, see thewithFixture(OneArgTest)
subsection of the Shared fixtures section in the documentation for classSpec
.Class
fixture.Spec
behaves similarly to classorg.scalatest.Spec
, except that tests may have a fixture parameter. The type of the fixture parameter is defined by the abstractFixtureParam
type, which is a member of this class. This class also has an abstractwithFixture
method. ThiswithFixture
method takes aOneArgTest
, which is a nested trait defined as a member of this class.OneArgTest
has anapply
method that takes aFixtureParam
. Thisapply
method is responsible for running a test. This class'srunTest
method delegates the actual running of each test towithFixture(OneArgTest)
, passing in the test code to run via theOneArgTest
argument. ThewithFixture(OneArgTest)
method (abstract in this class) is responsible for creating the fixture argument and passing it to the test function.Subclasses of this class must, therefore, do three things differently from a plain old
org.scalatest.Spec
:FixtureParam
withFixture(OneArgTest)
methodIf 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 class. One good approach to passing multiple fixture objects is to encapsulate them in a case class. Here's an example:
To enable the stacking of traits that define
withFixture(NoArgTest)
, it is a good idea to letwithFixture(NoArgTest)
invoke the test function instead of invoking the test function directly. To do so, you'll need to convert theOneArgTest
to aNoArgTest
. You can do that by passing the fixture object to thetoNoArgTest
method ofOneArgTest
. In other words, instead of writing “test(theFixture)
”, you'd delegate responsibility for invoking the test function to thewithFixture(NoArgTest)
method of the same instance by writing:Here's a complete example:
If a test fails, the
OneArgTest
function will result in a Failed wrapping the exception describing the failure. To ensure clean up happens even if a test fails, you should invoke the test function from inside atry
block and do the cleanup in afinally
clause, as shown in the previous example.Sharing fixtures across classes
If multiple test classes need the same fixture, you can define the
FixtureParam
andwithFixture(OneArgTest)
implementations in a trait, then mix that trait into the test classes that need it. For example, if your application requires a database and your integration tests use that database, you will likely have many test classes that need a database fixture. You can create a "database fixture" trait that creates a database with a unique name, passes the connector into the test, then removes the database once the test completes. This is shown in the following example:Often when you create fixtures in a trait like
DbFixture
, you'll still need to enable individual test classes to "setup" a newly created fixture before it gets passed into the tests. A good way to accomplish this is to pass the newly created fixture into a setup method, likepopulateDb
in the previous example, before passing it to the test function. Classes that need to perform such setup can override the method, as doesExampleSpec
.If a test doesn't need the fixture, you can indicate that by leaving off the fixture parameter, as is done in the third test in the previous example, “
Test code should be clear
”. For such methods,runTest
will not invokewithFixture(OneArgTest)
. It will instead directly invokewithFixture(NoArgTest)
.Both examples shown above demonstrate the technique of giving each test its own "fixture sandbox" to play in. When your fixtures involve external side-effects, like creating files or databases, it is a good idea to give each file or database a unique name as is done in these examples. This keeps tests completely isolated, allowing you to run them in parallel if desired. You could mix
ParallelTestExecution
into either of theseExampleSpec
classes, and the tests would run in parallel just fine.