Persistent Fixture Management

About This Chapter

In Chapter 8 , Transient Fixture Management, we saw how we can go about building in-memory Fresh Fixtures (page 311 ). We noted in that chapter that managing Fresh Fixtures is more complicated when the fixture is persisted either by the SUT or by the test itself. This chapter introduces the additional patterns required to manage persistent fixtures, including both Persistent Fresh Fixtures (see Fresh Fixture ) and Shared Fixtures (page 317 ).

Managing Persistent Fresh Fixtures

The term Persistent Fresh Fixture might sound like an oxymoron but it is actually not as large a contradiction as it might first seem. The Fresh Fixture strategy means that each run of each Test Method (page 348 ) uses a newly created fixture. The name speaks to its intent: We do not reuse the fixture! It does not need to imply that the fixture is transient—only that it is not reused ( Figure 9.1 ). Persistent Fresh Fixtures present several challenges not encountered with Transient Fresh Fixtures. In this chapter, we focus on the challenge posed by Unrepeatable Tests (see Erratic Test on page 228 ) caused by leftover Persistent Fresh Fixtures and Slow Tests (page 253 ) caused by Shared Fixtures (page 317 ).

Figure 9.1. A Fresh Fixture can be either transient or persistent. We can apply a Fresh Fixture strategy even if the test fixture is naturally persistent but we must have a way to tear it down after each test.

image

What Makes Fixtures Persistent?

A fixture, fresh or otherwise, can become persistent for one of two reasons. The first reason is that the SUT is a stateful object and "remembers" how it was used in the past. This scenario most often occurs when the SUT includes a database, but it can occur simply because the SUT uses class variables to hold some of its data. The second reason is that the Testcase Class (page 373 ) holds a reference to an otherwise Transient Fresh Fixture in a way that makes it survive across Test Method invocations.

Some members of the xUnit family provide a mechanism to reload all classes at the beginning of each test run. This behavior may appear as an option—a check box labeled "Reload Classes"—or it may be automatic. Such a feature keeps the fixture from becoming persistent when it is referenced from a class variable; it does not prevent the Fresh Fixture from becoming persistent if either the SUT or the test puts the fixture into the file system or a database.

Issues Caused by Persistent Fresh Fixtures

When fixtures are persistent, we may find that subsequent runs of the same Test Method try to recreate a fixture that already exists. This behavior may cause conflicts between the preexisting and newly created resources. Although violating unique key constraints in the database is the most common example of this problem, the conflict could be as simple as trying to create a file with the same name as one that already exists. One way to avoid these Unrepeatable Tests is to tear down the fixture at the end of each test; another is to use Distinct Generated Values (see Generated Value on page 723 ) for any identifiers that might cause conflicts.

Tearing Down Persistent Fresh Fixtures

Unlike fixture setup code, which should help us understand the pre-conditions of the test, fixture teardown code is purely a matter of good housekeeping. It does not help us understand the behavior of the SUT but it has the potential to obscure the intent of the test or at least make it more difficult to understand. Therefore, the best kind of teardown code is the nonexistent kind. We should avoid writing teardown code whenever we can, which is why Garbage-Collected Teardown (page 500 ) is so preferable. Unfortunately, we cannot take advantage of Garbage-Collected Teardown if our Fresh Fixture is persistent.

Hand-Coded Teardown

One way to ensure that the fixture is destroyed after we are done with it is to include test-specific teardown code within our Test Methods . This teardown mechanism might seem simple, but it is actually more complex than immediately meets the eye. Consider the following example:

            public  void  testGetFlightsByOriginAirport_NoFlights()
                           throws  Exception  {
                  //  Fixture  Setup
                  BigDecimal  outboundAirport  =  createTestAirport("1OF");
                  //  Exercise  System
                  List  flightsAtDestination1  =
                             facade.getFlightsByOriginAirport(outboundAirport);
                  //  Verify  Outcome
                  assertEquals(0,flightsAtDestination1.size());
                  facade.removeAirport(outboundAirport);
              }

This Naive In-line Teardown (see In-line Teardown on page 509 ) will tear down the fixture when the test passes—but it won't tear down the fixture if the test fails or ends with an error. This is because the calls to the Assertion Methods (page 362 ) throw an exception; therefore, we may never make it to the teardown code. To ensure that the In-line Teardown code always executes, we must surround everything in the Test Method that might raise an exception with a try/catch control structure. Here's the same test suitably modified:

      public  void  testGetFlightsByOriginAirport_NoFlights_td()
                     throws  Exception  {
            //  Fixture  Setup
            BigDecimal  outboundAirport  =  createTestAirport("1OF");
            try  {
                  //  Exercise  System
                  List  flightsAtDestination1  =
                            facade.getFlightsByOriginAirport(outboundAirport);
                            //  Verify  Outcome
                            assertEquals(0,flightsAtDestination1.size());
                      }  finally  {
                              facade.removeAirport(outboundAirport);
                      }
              }

Unfortunately, the mechanism to ensure that the teardown code always runs introduces a fair bit of complication into the Test Method . Matters become even more complicated when we must tear down several resources: Even if our attempt to clean up one resource fails, we want to ensure that the other resources are still cleaned up. We can address part of this problem by using Extract Method [Fowler] refactoring to move the teardown code into a Test Utility Method (page 599 ) that we call from inside the error-handling construct. Although this Delegated Teardown (see In-line Teardown ) hides the complexity of dealing with teardown errors, we still need to ensure that the method gets called even when test errors or test failures occur.

Most members of the xUnit family solve this problem by supporting Implicit Teardown (page 516 ). The Test Automation Framework (page 298 ) calls a special tearDown method after each Test Method regardless of whether the test passed or failed. This approach avoids placing the error-handling code within the Test Method but imposes two requirements on our tests. First, the fixture must be accessible from the tearDown method, so we must use instance variables (preferred), class variables, or global variables to hold the fixture. Second, we must ensure that the tearDown method works properly with each of the Test Methods regardless of which fixture it sets up. 1

Matching Setup with Teardown Code Organization

Given the three ways of organizing our setup code— In-line Setup (page 408 ), Delegated Setup (page 411 ), and Implicit Setup (page 424 )—and the three ways of organizing our teardown code— In-line Teardown, Delegated Teardown, and Implicit Teardown —nine different combinations are available to us. Choosing the right one turns out to be an easy decision because it is not important for the teardown code to be visible to the test reader. We simply choose the most appropriate setup code organization and either the equivalent or more hidden version of teardown ( Table 9.1 ). For example, it is appropriate to use Implicit Teardown even with In-line Setup or Delegated Setup ; it is almost never a good idea to use In-line Teardown with anything other than In-line Setup, and even then it should probably be avoided!

Table 9.1. The Compatibility of Various Fixture Setup and Teardown Strategies for Persistent Test Fixtures

image

Automated Teardown

Hand-coded teardown is associated with two problems: Extra work is required to write the tests, and the teardown code is hard to get right and even harder to test. When the teardown goes wrong, it may lead to Erratic Tests caused by Resource Leakage because the test that fails as a result is often different from the one that didn't clean up properly.

In languages that support garbage collection, tearing down a Transient Fresh Fixture should be pretty much automatic. As long as our fixtures are referenced only by instance variables that go out of scope when our Testcase Object (page 382 ) is destroyed, garbage collection will clean them up. Garbage collection won't work, however, if we use class variables or if our fixtures include persistent objects such as files or database rows. In those cases, we need to perform our own cleanup.

Not surprisingly, this situation may inspire the lazy but creative programmer to come up with a way to automate the teardown logic. The important thing to note is that teardown code doesn't help us understand the test so it is better for it to remain hidden. 2 We can eliminate the need to write hand-crafted teardown code for each Test Method or Testcase Class by building an Automated Teardown (page 503 ) mechanism. It consists of three parts:

  1. A well-tested mechanism to iterate over a list of objects that need to be deleted and catch/report any errors it encounters while ensuring that all the deletions are attempted.
  2. A dispatching mechanism that invokes the deletion code appropriate to the kind of object to be deleted. This mechanism is often implemented as a Command [GOF] object that wraps each object to be deleted, but could be as simple as calling a delete method on the object itself or using a switch statement based on the object's class.
  3. A registration mechanism to add newly created objects (suitably wrapped if necessary) to the list of objects to be deleted.

Once we have built our Automated Teardown mechanism, we can simply invoke the registration method from our Creation Methods (page 415 ) and the cleanup method from the tearDown method. The latter operation can be specified in a Testcase Superclass (page 638 ) that all of our Testcase Classes inherit from. We can even extend this mechanism to delete objects created by the SUT as it is exercised. To do so, we use an observable Object Factory (see Dependency Lookup on page 686 ) inside the SUT and have our Testcase Superclass register itself as an Observer [GOF] of object creation.

Database Teardown

When our persistent Fresh Fixture has been built entirely in a relational database, we can take advantage of certain features of the database to implement its teardown. Table Truncation Teardown (page 661 ) is a brute-force way to blow away the entire contents of a table with a single database command. Of course, it is appropriate only when each Test Runner (page 377 ) has its own Database Sandbox (page 650 ). A somewhat less drastic approach is to use Transaction Rollback Teardown (page 668 ) to undo all changes made within the context of the current test. This mechanism relies on the SUT having been designed using the Humble Transaction Controller pattern (see Humble Object on page 695 ) so that we can invoke the business logic from the test without having the SUT commit the transaction automatically. Both of these database-specific teardown patterns are most commonly implemented using Implicit Teardown to keep the teardown logic out of the Test Methods.

Avoiding the Need for Teardown

So far, we have looked at ways to do fixture teardown. Now, let us look at ways to avoid fixture teardown.

Avoiding Fixture Collisions

We need to do fixture teardown for three reasons:

  1. The accumulation of leftover fixture objects can cause tests to run slowly.
  2. The leftover fixture objects can cause the SUT to behave differently or our assertions to report incorrect results.
  3. The leftover fixture objects can prevent us from creating the Fresh Fixture required by our test.

The issue that is easiest to address is the first one: We can schedule a periodic cleansing of the persistence mechanism back to a known, minimalist state. Unfortunately, this tactic is useful only if we can get the tests to run correctly in the presence of accumulated test detritus.

The second issue can be addressed by using Delta Assertions (page 485 ) rather than "absolute" assertions. Delta Assertions work by taking a snapshot of the fixture before the test is run and verifying that the expected differences have appeared after we exercise the SUT.

The third issue can be addressed by ensuring that each test generates a different set of fixture objects each time it is run. Thus any objects that the test needs to create must be given totally unique identifiers—that is, unique filenames, unique keys, and so on. To do so, we can build a simple unique ID generator and create a new ID at the beginning of each test. We can then use that unique ID as part of the identity of each newly created fixture object. If the fixture is shared beyond a single Test Runner , we may need to include something about the user in the unique identifiers we create; the currently logged-in user ID is usually sufficient. Using Distinct Generated Values as keys offers another benefit: It allows us to implement a Database Partitioning Scheme (see Database Sandbox ) in which we can use absolute assertions despite the presence of leftover fixture objects.

Avoiding Fixture Persistence

We seem to be going to a lot of trouble to undo the side effects caused by a persistent Fresh Fixture . Wouldn't it be nice if we could avoid all of this work? The good news is that we can. The bad news is that we need to make our Fresh Fixture nonpersistent to do so. When the SUT is to blame for the persistence of the fixture, one possibility is to replace the persistence mechanism with a Test Double (page 522 ) that the test can wipe out at will. A good example of this approach is the use of a Fake Database (see Fake Object on page 551 ). When the test is to blame for fixture persistence, the solution is even easier: Just use a less persistent fixture reference mechanism.

Dealing with Slow Tests

A major drawback of using a Persistent Fresh Fixture is speed or, more precisely, the lack thereof. File systems and databases are much slower than the processors used in modern computers. As a consequence, tests that interact with databases tend to run much more slowly than tests that run entirely in memory. Part of this difference arises because the SUT is accessing the fixture from disk—but this issue turns out to be only a small part of the reason for the slowdown. Setting up the Fresh Fixture at the beginning of each test and tearing it down at the end of each test typically takes many more disk accesses than those used by the SUT to access the fixture. As a result, tests that access the database often take 50 to 100 times 3 longer to run than tests that run entirely in memory, all other things being equal.

The typical reaction to slow tests caused by Persistent Fresh Fixtures is to eliminate the fixture setup and teardown overhead by reusing the fixture across many tests. Assuming we have five disk accesses to set up and tear down the fixture for every disk access performed by the SUT, the absolute best 4 we can do by switching to a Shared Fixture is somewhere around ten times as slow. Of course, this outcome is still too slow in most situations and it comes with a hefty price: The tests are no longer independent. That means we will likely have Interacting Tests (see Erratic Test ), Lonely Tests (see Erratic Test ), and Unrepeatable Tests on top of our Slow Tests !

A much better solution is to eliminate the need to have a disk-based database under the application. With a small amount of effort, we should be able to replace the disk-based database with an In-Memory Database (see Fake Object ) or a Fake Database . This decision is best made early in the project while the effort is still low. Yes, there are some challenges, such as dealing with stored procedures, but they are all surmountable.

This tactic isn't the only way to deal with Slow Tests, of course. The sidebar " Faster Tests Without Shared Fixtures " (page 319 ) explores some other strategies.

Managing Shared Fixtures

Managing Shared Fixtures has a lot in common with managing Persistent Fresh Fixtures, except that we deliberately choose not to tear the fixture down after every test so that we can reuse it in subsequent tests ( Figure 9.2 ). This implies two things. First, we must be able to access the fixture in the other tests. Second, we must have a way of triggering both the construction and the teardown of the fixture.

Figure 9.2. A Shared Fixture with two Test Methods that share it. A Shared Fixture is set up once and used by two or more tests that may interact, either deliberately or accidentally, as a result. Note the lack of a fixture setup phase for the second test.

image

Accessing Shared Fixtures

Regardless of how and when we choose to build the Shared Fixture , the tests need a way to find the test fixture they are to reuse. The choices available to us depend on the nature of the fixture. When the fixture is stored in a database (the most common usage of a Shared Fixture ), tests may access it directly without making direct references to the fixture objects as long as they know about the database. There may be a temptation to use Hard-Coded Values (see Literal Value on page 714 ) in database lookups to access the fixture objects. This is almost always a bad idea, however, because it leads to a close coupling between tests and the fixture implementation and because it has poor documentation value ( Obscure Test ; page 186 ). To avoid these potential problems, we can use Finder Methods (see Test Utility Method ) with Intent-Revealing Names [SBPP] to access the fixture. These Finder Methods may have names that are very similar to those of Creation Methods, but they return references to existing fixture objects rather than building brand new ones.

We have a range of possible solutions when the fixture is stored in memory. If all tests that need to share the fixture are in the same Testcase Class , we can use a fixture holding class variable to hold the reference to the fixture. As long as we give the variable an Intent-Revealing Name, the test reader should be able to understand the pre-conditions of the test. Another alternative is to use a Finder Method .

If we need to share the fixture across many Testcase Classes , we must use a more sophisticated technique. We could, of course, let one class declare the fixture holding class variable and have the other tests access the fixture via that variable. Unfortunately, this approach may create unnecessary coupling between the tests. Another alternative is to move the declaration to a well-known object—namely, a Test Fixture Registry (see Test Helper on page 643 ). This Registry [PEAA] object could be something like a test database or it could merely be a class. It can expose various parts of a fixture via discrete fixture holding class variables or via Finder Methods . When the Test Fixture Registry has only Finder Methods that know how to access the objects but don't hold references to them, we call it a Test Helper .

Triggering Shared Fixture Construction

For a test fixture to be shared, it must be built before any Test Method needs it. This construction could take place as late as right before the Test Method's logic is run, just before the entire test suite is run, or at some earlier time ( Figure 9.3 ). This leads us to the basic patterns of Shared Fixture creation.

Figure 9.3. The plethora of ways to manage a Shared Fixture. A Shared Fixture can be set up at a variety of times; the decision is based on how many tests need to reuse the fixture and how many times they need to do so.

image

If we are happy with the idea of creating the test fixture the first time any test needs it, we can use Lazy Setup (page 435 ) in the setUp method of the corresponding Testcase Class to create it as part of running the first test. Subsequent tests will then see that the fixture already exists and reuse it. Because there is no obvious signal that the last test in a test suite (or Suite of Suites ; see Test Suite Object on page 387 ) has been run, we won't know when to tear down the fixture after each test run. This can lead to Unrepeatable Tests because the fixture may survive across test runs (depending on how the various tests access it).

If we need to share the fixture more broadly, we could include a Fixture Setup Testcase at the beginning of the test suite. This is a special case of Chained Tests and suffers from the same problem as Lazy Setup —specifically, we don't know when it is time to tear down the fixture. It also depends on the ordering of tests within a suite, so it works best with Test Enumeration (page 399 ).

If we need to be able to tear down the test fixture after running a test suite, we must use a fixture management mechanism that tells us when the last test has been run. Several members of the xUnit family support the concept of a setUp method that runs just once for the test suite created from a single Testcase Class . This Suite Fixture Setup (page 441 ) method has a corresponding tearDown method that is called when the last Test Method has finished running. 5 We can then guarantee that a new fixture is built for each test run. The fixture is not left over to cause problems with subsequent test runs, which prevents Unrepeatable Tests ; it does not prevent Interacting Tests within the test run, however. This capability could be added as an extension to any member of the xUnit family. When it isn't supported or when we need to share the fixture beyond a single Testcase Class, we can resort to using a Setup Decorator (page 447 ) to bracket the running of a test suite with the execution of the fixture setUp and tearDown logic. The biggest drawback of Setup Decorator is that tests that depend on the decorator cannot be run by themselves; they are Lonely Tests .

The final option is to build the fixture well before the tests are run—that is, to employ a Prebuilt Fixture (page 429 ). This approach offers the most options regarding how the test fixture is actually constructed because the fixture setup need not be executable from within xUnit. For example, it could be set up manually, by using database scripts, by copying a "golden" database, or by running a data generation program. The major disadvantage with a Prebuilt Fixture is that if any tests are Unrepeatable Tests , we will need to perform a Manual Intervention (page 250 ) before each test run. As a result, a Prebuilt Fixture is often used in combination with a Fresh Fixture to construct an Immutable Shared Fixture (see Shared Fixture ).

What's Next?

Now that we've determined how we will set up and tear down our fixtures, we are ready to turn our attention to exercising the SUT and verifying that the expected outcome has occurred using calls to Assertion Methods . This process is described in more detail in Chapter 10 , Result Verification .