Behavior Smells

Smells in This Chapter

       Assertion Roulette 224

       Erratic Test 228

       Fragile Test 239

       Frequent Debugging 248

       Manual Intervention 250

       Slow Tests 253

Assertion Roulette

It is hard to tell which of several assertions within the same test method caused a test failure.

Symptoms

A test fails. Upon examining the output of the Test Runner (page 377 ), we cannot determine exactly which assertion failed.

Impact

When a test fails during an automated Integration Build [SCM], it may be hard to tell exactly which assertion failed. If the problem cannot be reproduced on a developer's machine (as may be the case if the problem is caused by environmental issues or Resource Optimism; see Erratic Test on page 228 ) fixing the problem may be difficult and time-consuming.

Causes

Cause: Eager Test

A single test verifies too much functionality.

Symptoms

A test exercises several methods of the SUT or calls the same method several times interspersed with fixture setup logic and assertions.

public  void  testFlightMileage_asKm2()  throws  Exception  {
      //  set  up  fixture
      //  exercise  constructor
      Flight  newFlight  =  new  Flight(validFlightNumber);
      //  verify  constructed  object
      assertEquals(validFlightNumber,  newFlight.number);
      assertEquals("",  newFlight.airlineCode);
      assertNull(newFlight.airline);
      //  set  up  mileage
      newFlight.setMileage(1122);
      //  exercise  mileage  translator
      int  actualKilometres  =  newFlight.getMileageAsKm();
      //  verify  results
      int  expectedKilometres  =  1810;
      assertEquals(  expectedKilometres,  actualKilometres);
      //  now  try  it  with  a  canceled  flight
      newFlight.cancel();
      try  {
          newFlight.getMileageAsKm();
          fail("Expected  exception");
      }  catch  (InvalidRequestException  e)  {
          assertEquals(  "Cannot  get  cancelled  flight  mileage",
                              e.getMessage());
      }
}

Another possible symptom is that the test automater wants to modify the Test Automation Framework (page 298 ) to keep going after an assertion has failed so that the rest of the assertions can be executed.

Root Cause

An Eager Test is often caused by trying to minimize the number of unit tests (whether consciously or unconsciously) by verifying many test conditions in a single Test Method (page 348 ). While this is a good practice for manually executed tests that have "liveware" interpreting the results and adjusting the tests in real time, it just doesn't work very well for Fully Automated Tests (see page 26 ).

Another common cause of Eager Tests is using xUnit to automate customer tests that require many steps, thereby verifying many aspects of the SUT in each test. These tests are necessarily longer than unit tests but care should be taken to keep them as short as possible (but no shorter!).

Possible Solution

For unit tests, we break up the test into a suite of Single-Condition Tests (see page 45 ) by teasing apart the Eager Test . It may be possible to do so by using one or more Extract Method [Fowler] refactorings to pull out independent pieces into their own Test Methods . Sometimes it is easier to clone the test once for each test condition and then clean up each Test Method by removing any code that is not required for that particular test conditions. Any code required to set up the fixture or put the SUT into the correct starting state can be extracted into a Creation Method (page 415 ). A good IDE or compiler will then help us determine which variables are no longer being used.

If we are automating customer tests using xUnit, and this effort has resulted in many steps in each test because the work flows require complex fixture setup, we could consider using some other way to set up the fixture for the latter parts of the test. If we can use Back Door Setup (see Back Door Manipulation on page 327 ) to create the fixture for the last part of the test independently of the first part, we can break one test into two, thereby improving our Defect Localization (see Goals of Test Automation ). We should repeat this process as many times as it takes to make the tests short enough to be readable at a single glance and to Communicate Intent (see page 41 ) clearly.

Cause: Missing Assertion Message
Symptoms

A test fails. Upon examining the output of the Test Runner , we cannot determine exactly which assertion failed.

Root Cause

This problem is caused by the use of Assertion Method (page 362 ) calls with identical or missing Assertion Messages (page 370 ). It is most commonly encountered when running tests using a Command-Line Test Runner (see Test Runner ) or a Test Runner that is not integrated with the program text editor or development environment.

In the following test, we have a number of Equality Assertions (see Assertion Method ):

public  void  testInvoice_addLineItem7()  {
      LineItem  expItem  =  new  LineItem(inv,  product,  QUANTITY);
      //  Exercise
      inv.addItemQuantity(product,  QUANTITY);
      //  Verify
      List  lineItems  =  inv.getLineItems();
      LineItem  actual  =  (LineItem)lineItems.get(0);
      assertEquals(expItem.getInv(),  actual.getInv());
      assertEquals(expItem.getProd(),  actual.getProd());
      assertEquals(expItem.getQuantity(),  actual.getQuantity());
}

When an assertion fails, will we know which one it was? An Equality Assertion typically prints out both the expected and the actual values—but it may prove difficult to tell which assertion failed if the expected values are similar or print out cryptically. A good rule of thumb is to include at least a minimal Assertion Message whenever we have more than one call to the same kind of Assertion Method .

Possible Solution

If the problem occurred while we were running a test using a Graphical Test Runner (see Test Runner ) with IDE integration, we should be able to click on the appropriate line in the stack traceback to have the IDE highlight the failed assertion. Failing this, we can turn on the debugger and single-step through the test to see which assertion statement fails.

If the problem occurred while we were running a test using a Command-Line Test Runner, we can try running the test from a Graphical Test Runner with IDE integration to determine the offending assertion. If that doesn't work, we may have to resort to using line numbers (if available) or apply a process of elimination to deduce which of the assertions it couldn't be to narrow down the possibilities. Of course, we could just bite the bullet and add a unique Assertion Message (even just a number!) to each call to an Assertion Method .

Further Reading

Assertion Roulette and Eager Test were first described in a paper presented at XP2001 called "Refactoring Test Code" [RTC] .

Erratic Test

One or more tests behave erratically; sometimes they pass and sometimes they fail.

Symptoms

We have one or more tests that run but give different results depending on when they are run and who is running them. In some cases, the Erratic Test will consistently give the same results when run by one developer but fail when run by someone else or in a different environment. In other cases, the Erratic Test will give different results when run from the same Test Runner (page 377 ).

Impact

We may be tempted to remove the failing test from the suite to "keep the bar green" but this would result in an (intentional) Lost Test (see Production Bugs on page 268 ). If we choose to keep the Erratic Test in the test suite despite the failures, the known failure may obscure other problems, such as another issue detected by the same tests. Just having a test fail can cause us to miss additional failures because it is much easier to see the change from a green bar to a red bar than to notice that two tests are failing instead of just the one we expected.

Troubleshooting Advice

Erratic Tests can be challenging to troubleshoot because so many potential causes exist. If the cause cannot be easily determined, it may be necessary to collect data systematically over a period of time. Where (in which environments) did the tests pass, and where did they fail? Were all the tests being run or just a subset of them? Did any change in behavior occur when the test suite was run several times in a row? Did any change in behavior occur when it was run from several Test Runners at the same time?

Once we have some data, it should be easier to match up the observed symptoms with those listed for each of the potential causes and to narrow the list of possibilities to a handful of candidates. Then we can collect some more data focusing on differences in symptoms between the possible causes. Figure 16.1 summarizes the process for determining which cause of an Erratic Test we are dealing with.

Figure 16.1. Troubleshooting an Erratic Test.

image

Causes

Tests may behave erratically for a number of reasons. The underlying cause can usually be determined through some persistent sleuthing by paying attention to patterns regarding how and when the tests fail. Some of the causes are common enough to warrant giving them names and specific advice for rectifying them.

Cause: Interacting Tests

Tests depend on other tests in some way. Note that Interacting Test Suites and Lonely Test are specific variations of Interacting Tests .

Symptoms

A test that works by itself suddenly fails in the following circumstances:

  • Another test is added to (or removed from) the suite.
  • Another test in the suite fails (or starts to pass).
  • The test (or another test) is renamed or moved in the source file.
  • A new version of the Test Runner is installed.
Root Cause

Interacting Tests usually arise when tests use a Shared Fixture (page 317 ), with one test depending in some way on the outcome of another test. The cause of Interacting Tests can be described from two perspectives:

  • The mechanism of interaction
  • The reason for interaction

The mechanism for interaction could be something blatantly obvious—for example, testing an SUT that includes a database—or it could be more subtle. Anything that outlives the lifetime of the test can lead to interactions; static variables can be depended on to cause Interacting Tests and, therefore, should be avoided in both the SUT and the Test Automation Framework (page 298 )! See the sidebar " There's Always an Exception " on page 384 for an example of the latter problem. Singletons [GOF] and Registries [PEAA] are good examples of things to avoid in the SUT if at all possible. If we must use them, it is best to include a mechanism to reinitialize their static variables at the beginning of each test.

Tests may interact for a number of reasons, either by design or by accident:

  • Depending on the fixture constructed by the fixture setup phase of another test
  • Depending on the changes made to the SUT during the exercise SUT phase of another test
  • A collision caused by some mutually exclusive action (which may be either of the problems mentioned above) between two tests run in the same test run

The dependencies may suddenly cease to be satisfied if the depended-on test

  • Is removed from the suite,
  • Is modified to no longer change the state of the SUT,
  • Fails in its attempt to change the state of the SUT, or
  • Is run after the test in question (because it was renamed or moved to a different Testcase Class ; see page 373 ).

Similarly, collisions may start occurring when the colliding test is

  • Added to the suite,
  • Passes for the first time, or
  • Runs before the dependent test.

In many of these cases, multiple tests will fail. Some of the tests may fail for a good reason—namely, the SUT is not doing what it is supposed to do. Dependent tests may fail for the wrong reason—because they were coded to depend on other tests' success. As a result, they may be giving a "false-positive" (false-failure) indication.

In general, depending on the order of test execution is not a wise approach because of the problems described above. Most variants of the xUnit framework do not make any guarantees about the order of test execution within a test suite. ( TestNG, however, promotes interdependencies between tests by providing features to manage the dependencies.)

Possible Solution

Using a Fresh Fixture (page 311 ) is the preferred solution for Interacting Tests ; it is almost guaranteed to solve the problem. If we must use a Shared Fixture, we should consider using an Immutable Shared Fixture (see Shared Fixture ) to prevent the tests from interacting with one another through changes in the fixture by creating from scratch those parts of the fixture that they intend to modify.

If an unsatisfied dependency arises because another test does not create the expected objects or database data, we should consider using Lazy Setup (page 435 ) to create the objects or data in both tests. This approach ensures that the first test to execute creates the objects or data for both tests. We can put the fixture setup code into a Creation Method (page 415 ) to avoid Test Code Duplication (page 213 ). If the tests are on different Testcase Classes, we can move the fixture setup code to a Test Helper (page 643 ).

Sometimes the collision may be caused by objects or database data that are created in our test but not cleaned up afterward. In such a case, we should consider implementing Automated Fixture Teardown (see Automated Teardown on page 503 ) to remove them safely and efficiently.

A quick way to find out whether any tests depend on one another is to run the tests in a different order than the normal order. Running the entire test suite in reverse order, for example, would do the trick nicely. Doing so regularly would help avoid accidental introduction of Interacting Tests .

Cause: Interacting Test Suites

In this special case of Interacting Tests, the tests are in different test suites.

Symptoms

A test passes when it is run in its own test suite but fails when it is run within a Suite of Suites (see Test Suite Object on page 387 ).

Suite1.run()-->  Green
Suite2.run()-->  Green
Suite(Suite1,Suite2).run()-->  Test  C  in  Suite2  fails

Root Cause

Interacting Test Suites usually occur when tests in separate test suites try to create the same resource. When they are run in the same suite, the first one succeeds but the second one fails while trying to create the resource.

The nature of the problem may be obvious just by looking at the test failure or by reading the failed Test Method (page 348 ). If it is not, we can try removing other tests from the (nonfailing) test suite, one by one. When the failure stops occurring, we simply examine the last test we removed for behaviors that might cause the interactions with the other (failing) test. In particular, we need to look at anything that might involve a Shared Fixture, including all places where class variables are initialized. These locations may be within the Test Method itself, within a setUp method, or in any Test Utility Methods (page 599 ) that are called.

Warning: There may be more than one pair of tests interacting in the same test suite! The interaction may also be caused by the Suite Fixture Setup (page 441 ) or Setup Decorator (page 447 ) of several Testcase Classes clashing rather than by a conflict between the actual Test Methods!

Variants of xUnit that use Testcase Class Discovery (see Test Discovery on page 393 ), such as NUnit, may appear to not use test suites. In reality, they do—they just don't expect the test automaters to use a Test Suite Factory (see Test Enumeration on page 399 ) to identify the Test Suite Object to the Test Runner .

Possible Solution

We could, of course, eliminate this problem entirely by using a Fresh Fixture. If this solution isn't within our scope, we could try using an Immutable Shared Fixture to prevent the tests' interaction.

If the problem is caused by leftover objects or database rows created by one test that conflict with the fixture being created by a later test, we should consider using Automated Teardown to eliminate the need to write error-prone cleanup code.

Cause: Lonely Test

A Lonely Test is a special case of Interacting Tests. In this case, a test can be run as part of a suite but cannot be run by itself because it depends on something in a Shared Fixture that was created by another test (e.g., Chained Tests ; see page 454 ) or by suite-level fixture setup logic (e.g., a Setup Decorator ).

We can address this problem by converting the test to use a Fresh Fixture or by adding Lazy Setup logic to the Lonely Test to allow it to run by itself.

Cause: Resource Leakage

Tests or the SUT consume finite resources.

Symptoms

Tests run more and more slowly or start to fail suddenly. Reinitializing the Test Runner, SUT, or Database Sandbox (page 650 ) clears up the problem—only to have it reappear over time.

Root Cause

Tests or the SUT consume finite resources by allocating those resources and failing to free them afterward. This practice may make the tests run more slowly. Over time, all the resources are used up and tests that depend on them start to fail.

This problem can be caused by one of two types of bugs:

  • The SUT fails to clean up the resources properly. The sooner we detect this behavior, the sooner we can track it down and fix it.
  • The tests themselves cause the resource leakage by allocating resources as part of fixture setup and failing to clean them up during fixture teardown.
Possible Solution

If the problem lies in the SUT, then the tests have done their job and we can fix the bug. If the tests are causing the resource leakage , then we must eliminate the source of the leaks. If the leaks are caused by failure to clean up properly when tests fail, we may need to ensure that all tests do Guaranteed In-line Teardown (see In-line Teardown on page 509 ) or convert them to use Automated Teardown .

In general, it is a good idea to set the size of all resource pools to 1. This choice will cause the tests to fail much sooner, allowing us to more quickly determine which tests are causing the leak(s).

Cause: Resource Optimism

A test that depends on external resources has nondeterministic results depending on when or where it is run.

Symptoms

A test passes when it is run in one environment and fails when it is run in another environment.

Root Cause

A resource that is available in one environment is not available in another environment.

Possible Solution

If possible, we should convert the test to use a Fresh Fixture by creating the resource as part of the test's fixture setup phase. This approach ensures that the resource exists wherever it is run. It may necessitate the use of relative addressing of files to ensure that the specific location in the file system exists regardless of where the SUT is executed.

If an external resource must be used, the resources should be stored in the source code repository [SCM] so that all Test Runners run in the same environment.

Cause: Unrepeatable Test

A test behaves differently the first time it is run compared with how it behaves on subsequent test runs. In effect, it is interacting with itself across test runs.

Symptoms

Either a test passes the first time it is run and fails on all subsequent runs, or it fails the first time and passes on all subsequent runs. Here's an example of what "Pass-Fail-Fail" might look like:

Suite.run()-->  Green
Suite.run()-->  Test  C  fails
Suite.run()-->  Test  C  fails
User  resets  something
Suite.run()-->  Green
Suite.run()-->  Test  C  fails

Here's an example of what "Fail-Pass-Pass" might look like:

Suite.run()-->  Test  C  fails
Suite.run()-->  Green
Suite.run()-->  Green
User  resets  something
Suite.run()-->  Test  C  fails
Suite.run()-->  Green

Be forewarned that if our test suite contains several Unrepeatable Tests , we may see results that look more like this:

Suite.run()-->  Test  C  fails
Suite.run()-->  Test  X  fails
Suite.run()-->  Test  X  fails
User  resets  something
Suite.run()-->  Test  C  fails
Suite.run()-->  Test  X  fails

Test C exhibits the "Fail-Pass-Pass" behavior, while test X exhibits the "Pass-Fail-Fail" behavior at the same time. It is easy to miss this problem because we see a red bar in each case; we notice the difference only if we look closely to see which tests fail each time we run them.

Root Cause

The most common cause of an Unrepeatable Test is the use—either deliberate or accidental—of a Shared Fixture . A test may be modifying the test fixture such that, during a subsequent run of the test suite, the fixture is in a different state. Although this problem most commonly occurs with a Prebuilt Fixture (see Shared Fixture ), the only true prerequisite is that the fixture outlasts the test run.

The use of a Database Sandbox may isolate our tests from other developers' tests but it won't prevent the tests we run from colliding with themselves or with other tests we run from the same Test Runner .

The use of Lazy Setup to initialize a fixture holding class variable can result in the test fixture not being reinitialized on subsequent runs of the same test suite. In effect, we are sharing the test fixture between all runs started from the same Test Runner .

Possible Solution

Because a persistent Shared Fixture is a prerequisite for an Unrepeatable Test , we can eliminate the problem by using a Fresh Fixture for each test. To fully isolate the tests, we must make sure that no shared resource, such as a Database Sandbox, outlasts the lifetimes of the individual tests. One option is to replace a database with a Fake Database (see Fake Object on page 551 ). If we must work with a persistent data store, we should use Distinct Generated Values (see Generated Value on page 723 ) for all database keys to ensure that we create different objects for each test and test run. The other alternative is to implement Automated Teardown to remove all newly created objects and rows safely and efficiently.

Cause: Test Run War

Test failures occur at random when several people are running tests simultaneously.

Symptoms

We are running tests that depend on some shared external resource such as a database. From the perspective of a single person running tests, we might see something like this:

Suite.run()  -->  Test  3  fails
Suite.run()  -->  Test  2  fails
Suite.run()  -->  All  tests  pass
Suite.run()  -->  Test  1  fails

Upon describing our problem to our teammates, we discover that they are having the same problem at the same time. When only one of us runs tests, all of the tests pass.

Impact

A Test Run War can be very frustrating because the probability of it occurring increases the closer we get to a code cutoff deadline. This isn't just Murphy's law kicking in: It really does happen more often at this point! We tend to commit smaller changes at more frequent intervals as the deadline approaches (think "last-minute bug fixing"!). This, in turn, increases the likelihood that someone else will be running the test suite at the same time, which itself increases the likelihood of test collisions between test runs occurring at the same time.

Root Cause

A Test Run War can happen only when we have a globally Shared Fixture that various tests access and sometimes modify. This shared fixture could be a file that must be opened or read by either a test or the SUT, or it could consist of the records in a test database.

Database contention can be caused by the following activities:

  • Trying to update or delete a record while another test is also updating the same record
  • Trying to update or delete a record while another test has a read lock (pessimistic locking) on the same record

File contention can be caused by an attempt to access a file that has already been opened by another instance of the test running from a different Test Runner .

Possible Solution

Using a Fresh Fixture is the preferred solution for a Test Run War . An even simpler solution is to give each Test Runner his or her own Database Sandbox . This should not involve making any changes to the tests but will completely eliminate the possibility of a Test Run War . It will not, however, eliminate other sources of Erratic Tests because the tests can still interact through the Shared Fixture (the Database Sandbox ). Another option is to switch to an Immutable Shared Fixture by having each test create new objects whenever it plans to change those objects. This approach does require changes to the Test Methods .

If the problem is caused by leftover objects or database rows created by one test that pollutes the fixture of a later test, another solution is using Automated Teardown to clean up after each test safely and efficiently. This measure, by itself, is unlikely to completely eliminate a Test Run War but it might reduce its frequency.

Cause: Nondeterministic Test

Test failures occur at random, even when only a single Test Runner is running tests.

Symptoms

We are running tests and the results vary each time we run them, as shown here:

Suite.run()  -->  Test  3  fails
Suite.run()  -->  Test  3  crashes
Suite.run()  -->  All  tests  pass
Suite.run()  -->  Test  3  fails

After comparing notes with our teammates, we rule out a Test Run War either because we are the only person running tests or because the test fixture is not shared between users or computers.

As with an Unrepeatable Test , having multiple Nondeterministic Tests in the same test suite can make it more difficult to detect the failure/error pattern: It looks like different tests are failing rather than a single test producing different results.

Impact

Debugging Nondeterministic Tests can be very time-consuming and frustrating because the code executes differently each time. Reproducing the failure can be problematic, and characterizing exactly what causes the failure may require many attempts. (Once the cause has been characterized, it is often a straightforward process to replace the random value with a value known to cause the problem.)

Root Cause

Nondeterministic Tests are caused by using different values each time a test is run. Sometimes, of course, it is a good idea to use different values each time the same test is run. For example, Distinct Generated Values may legitimately be used as unique keys for objects stored in a database. Use of generated values as input to an algorithm where the behavior of the SUT is expected to differ for different values can cause Nondeterministic Tests , however, as in the following examples:

  • Integer values where negative (or even zero) values are treated differently by the system, or where there is a maximum allowable value. If we generate a value at random, the test could fail in some test runs and pass on others.
  • String values where the length of a string has minimum or maximum allowed values. This problem often occurs accidentally when we generate a random or unique numeric value and then convert it to a string representation without using an explicit format that guarantees the length is constant.

It might seem like a good idea to use random values because they would improve our test coverage. Unfortunately, this tactic decreases our understanding of the test coverage and the repeatability of our tests (which violates the Repeatable Test principle; see page 26 ).

Another potential cause of Nondeterministic Tests is the use of Conditional Test Logic (page 200 ) in our tests. Its inclusion can result in different code paths being executed on different test runs, which in turn makes our tests nondeterministic. A common "reason" cited for doing so is the Flexible Test (see Conditional Test Logic ). Anything that makes the tests less than completely deterministic is a bad idea!

Possible Solution

The first step is to make our tests repeatable by ensuring that they execute in a completely linear fashion by removing any Conditional Test Logic . Then we can go about replacing any random values with deterministic values. If this results in poor test coverage, we can add more tests for the interesting cases we aren't covering. A good way to determine the best set of input values is to use the boundary values of the equivalence classes . If their use results in a lot of Test Code Duplication , we can extract a Parameterized Test (page 607 ) or put the input values and the expected results into a file read by a Data-Driven Test (page 288 ).

Fragile Test

A test fails to compile or run when the SUT is changed in ways that do not affect the part the test is exercising.

Symptoms

We have one or more tests that used to run and pass but now either fail to compile and run or fail when they are run. When we have changed the behavior of the SUT in question, such a change in test results is expected. When we don't think the change should have affected the tests that are failing or we haven't changed any production code or tests, we have a case of Fragile Tests .

Past efforts at automated testing have often run afoul of the "four sensitivities" of automated tests. These sensitivities are what cause Fully Automated Tests (see page 26 ) that previously passed to suddenly start failing. The root cause for tests failing can be loosely classified into one of these four sensitivities. Although each sensitivity may be caused by a variety of specific test coding behaviors, it is useful to understand the sensitivities in their own right.

Impact

Fragile Tests increase the cost of test maintenance by forcing us to visit many more tests each time we modify the functionality of the system or the fixture. They are particularly deadly when projects rely on highly incremental delivery, as in agile development (such as eXtreme Programming ).

Troubleshooting Advice

We need to look for patterns in how the tests fail. We ask ourselves, "What do all of the broken tests have in common?" The answer to this question should help us understand how the tests are coupled to the SUT. Then we look for ways to minimize this coupling.

Figure 16.2 summarizes the process for determining which sensitivity we are dealing with.

Figure 16.2. Troubleshooting a Fragile Test.

image

The general sequence is to first ask ourselves whether the tests are failing to compile; if so, Interface Sensitivity is likely to blame. With dynamic languages we may see type incompatibility test errors at runtime—another sign of Interface Sensitivity .

If the tests are running but the SUT is providing incorrect results, we must ask ourselves whether we have changed the code. If so, we can try backing out of the latest code changes to see if that fixes the problem. If that tactic stops the failing tests, 1 then we had Behavior Sensitivity .

If the tests still fail with the latest code changes backed out, then something else must have changed and we must be dealing with either Data Sensitivity or Context Sensitivity . The former occurs only when we use a Shared Fixture (page 317 ) or we have modified fixture setup code; otherwise, we must have a case of Context Sensitivity .

While this sequence of asking questions isn't foolproof, it will give the right answer probably nine times out of ten. Caveat emptor!

Causes

Fragile Tests may be the result of several different root causes. They may be a sign of Indirect Testing (see Obscure Test on page 186 )—that is, using the objects we modified to access other objects—or they may be a sign that we have Eager Tests (see Assertion Roulette on page 224 ) that are verifying too much functionality. Fragile Tests may also be symptoms of overcoupled software that is hard to test in small pieces ( Hard-to-Test Code ; see page 209 ) or our lack of experience with unit testing using Test Doubles (page 522 ) to test pieces in isolation ( Overspecified Software ).

Regardless of their root cause, Fragile Tests usually show up as one of the four sensitivities. Let's start by looking at them in a bit more detail; we'll then examine some more detailed examples of how specific causes change test output.

Cause: Interface Sensitivity

Interface Sensitivity occurs when a test fails to compile or run because some part of the interface of the SUT that the test uses has changed.

Symptoms

In statically typed languages, Interface Sensitivity usually shows up as a failure to compile. In dynamically typed languages, it shows up only when we run the tests. A test written in a dynamically typed language may experience a test error when it invokes an application programming interface (API) that has been modified (via a method name change or method signature change). Alternatively, the test may fail to find a user interface element it needs to interact with the SUT via a user interface. Recorded Tests (page 278 ) that interact with the SUT through a user interface 2 are particularly prone to this problem.

Possible Solution

The cause of the failures is usually reasonably apparent. The point at which the test fails (to compile or execute) will usually point out the location of the problem. It is rare for the test to continue to run beyond the point of change—after all, it is the change itself that causes the test error.

When the interface is used only internally (within the organization or application) and by automated tests, SUT API Encapsulation (see Test Utility Method on page 599 ) is the best solution for Interface Sensitivity. It reduces the cost and impact of changes to the API and, therefore, does not discourage necessary changes from being made. A common way to implement SUT API Encapsulation is through the definition of a Higher-Level Language (see page 41 ) that is used to express the tests. The verbs in the test language are translated into the appropriate method calls by the encapsulation layer, which is then the only software that needs to be modified when the interface is altered in somewhat backward-compatible ways. The "test language" can be implemented in the form of Test Utility Methods such as Creation Methods (page 415 ) and Verification Methods (see Custom Assertion on page 474 ) that hide the API of the SUT from the test.

The only other way to avoid Interface Sensitivity is to put the interface under strict change control. When the clients of the interface are external and anonymous (such as the clients of Windows DLLs), this tactic may be the only viable alternative. In these cases, a protocol usually applies to making changes to interfaces. That is, all changes must be backward compatible; before older versions of methods can be removed, they must be deprecated, and deprecated methods must exist for a minimum number of releases or elapsed time.

Cause: Behavior Sensitivity

Behavior Sensitivity occurs when changes to the SUT cause other tests to fail.

Symptoms

A test that once passed suddenly starts failing when a new feature is added to the SUT or a bug is fixed.

Root Cause

Tests may fail because the functionality they are verifying has been modified. This outcome does not necessarily signal a case of Behavior Sensitivity because it is the whole reason for having regression tests. It is a case of Behavior Sensitivity in any of the following circumstances:

  • The functionality the regression tests use to set up the pre-test state of the SUT has been modified.
  • The functionality the regression tests use to verify the post-test state of the SUT has been modified.
  • The code the regression tests use to tear down the fixture has been changed.

If the code that changed is not part of the SUT we are verifying, then we are dealing with Context Sensitivity . That is, we may be testing too large a SUT. In such a case, what we really need to do is to separate the SUT into the part we are verifying and the components on which that part depends.

Possible Solution

Any newly incorrect assumptions about the behavior of the SUT used during fixture setup may be encapsulated behind Creation Methods . Similarly, assumptions about the details of post-test state of the SUT can be encapsulated in Custom Assertions or Verification Methods . While these measures won't eliminate the need to update test code when the assumptions change, they certainly do reduce the amount of test code that needs to be changed.

Cause: Data Sensitivity

Data Sensitivity occurs when a test fails because the data being used to test the SUT has been modified. This sensitivity most commonly arises when the contents of the test database change.

Symptoms

A test that once passed suddenly starts failing in any of the following circumstances:

  • Data is added to the database that holds the pre-test state of the SUT.
  • Records in the database are modified or deleted.
  • The code that sets up a Standard Fixture (page 305 ) is modified.
  • A Shared Fixture is modified before the first test that uses it.

In all of these cases, we must be using a Standard Fixture, which may be either a Fresh Fixture (page 311 ) or a Shared Fixture such as a Prebuilt Fixture (see Shared Fixture ).

Root Cause

Tests may fail because the result verification logic in the test looks for data that no longer exists in the database or uses search criteria that accidentally include newly added records. Another potential cause of failure is that the SUT is being exercised with inputs that reference missing or modified data and, therefore, the SUT behaves differently.

In all cases, the tests make assumptions about which data exist in the database—and those assumptions are violated.

Possible Solution

In those cases where the failures occur during the exercise SUT phase of the test, we need to look at the pre-conditions of the logic we are exercising and make sure they have not been affected by recent changes to the database.

In most cases, the failures occur during result verification. We need to examine the result verification logic to ensure that it does not make any unreasonable assumptions about which data exists. If it does, we can modify the verification logic.


Why Do We Need 100 Customers?

A software development coworker of mine was working on a project as an analyst. One day, the manager she was working for came into her office and asked, "Why have you requested 100 unique customers be created in the test database instance?"

As a systems analyst, my coworker was responsible for helping the business analysts define the requirements and the acceptance tests for a large, complex project. She wanted to automate the tests but had to overcome several hurdles. One of the biggest hurdles was the fact that the SUT got much of its data from an upstream system—it was too complex to try to generate this data manually.

The systems analyst came up with a way to generate XML from tests captured in spreadsheets. For the fixture setup part of the tests, she transformed the XML into QaRun (a Record and Playback Test tool— see Recorded Test on page 278 ) scripts that would load the data into the upstream system via the user interface. Because it took a while to run these scripts and for the data to make its way downstream to the SUT, the systems analyst had to run these scripts ahead of time. This meant that a Fresh Fixture (page 311 ) strategy was unachievable; a Prebuilt Fixture (page 429 ) was the best she could do. In an attempt to avoid the Interacting Tests (see Erratic Test on page 228 ) that were sure to result from a Shared Fixture (page 317 ), the systems analyst decided to implement a virtual Database Sandbox (page 650 ) using a Database Partitioning Scheme based on a unique customer number for each test. This way, any side effects of one test couldn't affect any other tests.

Given that she had about 100 tests to automate, the systems analyst needed about 100 test customers defined in the database. And that's what she told her manager.


The failure can show up in the result verification logic even if the problem is that the inputs of the SUT refer to nonexistent or modified data. This may require examining the "after" state of the SUT (which differs from the expected post-test state) and tracing it back to discover why it does not match our expectations. This should expose the mismatch between SUT inputs and the data that existed before the test started executing.

The best solution to Data Sensitivity is to make the tests independent of the existing contents of the database—that is, to use a Fresh Fixture . If this is not possible, we can try using some sort of Database Partitioning Scheme (see Database Sandbox on page 650 ) to ensure that the data modified for one test does not overlap with the data used by other tests. (See the sidebar " Why Do We Need 100 Customers? " on page 244 for an example.)

Another solution is to verify that the right changes have been made to the data. Delta Assertions (page 485 ) compare before and after "snapshots" of the data, thereby ignoring data that hasn't changed. They eliminate the need to hard-code knowledge about the entire fixture into the result verification phase of the test.

Cause: Context Sensitivity

Context Sensitivity occurs when a test fails because the state or behavior of the context in which the SUT executes has changed in some way.

Symptoms

A test that once passed suddenly starts failing for mysterious reasons. Unlike with an Erratic Test (page 228 ), the test produces consistent results when run repeatedly over a short period of time. What is different is that it consistently fails regardless of how it is run.

Root Cause

Tests may fail for two reasons:

  • The functionality they are verifying depends in some way on the time or date.
  • The behavior of some other code or system(s) on which the SUT depends has changed.

A major source of Context Sensitivity is confusion about which SUT we are intending to verify. Recall that the SUT is whatever piece of software we are intending to verify. When unit testing, it should be a very small part of the overall system or application. Failure to isolate the specific unit (e.g., class or method) is bound to lead to Context Sensitivity because we end up testing too much software all at once. Indirect inputs that should be controlled by the test are then left to chance. If someone then modifies a depended-on component (DOC), our tests fail.

To eliminate Context Sensitivity, we must track down which indirect input to the SUT has changed and why. If the system contains any date- or time-related logic, we should examine this logic to see whether the length of the month or other similar factors could be the cause of the problem.

If the SUT depends on input from any other systems, we should examine these inputs to see if anything has changed recently. Logs of previous interactions with these other systems are very useful for comparison with logs of the failure scenarios.

If the problem comes and goes, we should look for patterns related to when it passes and when it fails. See Erratic Test for a more detailed discussion of possible causes of Context Sensitivity .

Possible Solution

We need to control all the inputs of the SUT if our tests are to be deterministic. If we depend on inputs from other systems, we may need to control these inputs by using a Test Stub (page 529 ) that is configured and installed by the test. If the system contains any time- or date-specific logic, we need to be able to control the system clock as part of our testing. This may necessitate stubbing out the system clock with a Virtual Clock [VCTP] that gives the test a way to set the starting time or date and possibly to simulate the passage of time.

Cause: Overspecified Software

Also known as

Overcoupled Test

A test says too much about how the software should be structured or behave. This form of Behavior Sensitivity (see Fragile Test on page 239 ) is associated with the style of testing called Behavior Verification (page 468 ). It is characterized by extensive use of Mock Objects (page 544 ) to build layer-crossing tests. The main issue is that the tests describe how the software should do something, not what it should achieve. That is, the tests will pass only if the software is implemented in a particular way. This problem can be avoided by applying the principle Use the Front Door First (see page 40 ) whenever possible to avoid encoding too much knowledge about the implementation of the SUT into the tests.

Cause: Sensitive Equality

Objects to be verified are converted to strings and compared with an expected string. This is an example of Behavior Sensitivity in that the test is sensitive to behavior that it is not in the business of verifying. We could also think of it as a case of Interface Sensitivity where the semantics of the interface have changed. Either way, the problem arises from the way the test was coded; using the string representations of objects for verifying them against expected values is just asking for trouble.

Cause: Fragile Fixture

When a Standard Fixture is modified to accommodate a new test, several other tests fail. This is an alias for either Data Sensitivity or Context Sensitivity depending on the nature of the fixture in question.

Further Reading

Sensitive Equality and Fragile Fixture were first described in [RTC], which was the first paper published on test smells and refactoring test code. The four sensitivities were first described in [ARTRP], which also described several ways to avoid Fragile Tests in Recorded Tests .

Frequent Debugging

Manual debugging is required to determine the cause of most test failures.

Symptoms

Also known as

Manual Debugging

A test run results in a test failure or a test error. The output of the Test Runner (page 377 ) is insufficient for us to determine the problem. Thus we have to use an interactive debugger (or sprinkle print statements throughout the code) to determine where things are going wrong.

If this case is an exception, we needn't worry about it. If most test failures require this kind of debugging, however, we have a case of Frequent Debugging .

Causes

Frequent Debugging is caused by a lack of Defect Localization (see page 22 ) in our suite of automated tests. The failed tests should tell us what went wrong either through their individual failure messages (see Assertion Message on page 370 ) or through the pattern of test failures. If they don't:

  • We may be missing the detailed unit tests that would point out a logic error inside an individual class.
  • We may be missing the component tests for a cluster of classes (i.e., a component) that would point out an integration error between the individual classes. This can happen when we use Mock Objects (page 544 ) extensively to replace depended-on objects but the unit tests of the depended-on objects don't match the way the Mock Objects are programmed to behave.

I've encountered this problem most frequently when I wrote higher-level (functional or component) tests but failed to write all the unit tests for the individual methods. (Some people would call this approach storytest-driven development to distinguish it from unit test-driven development, in which every little bit of code is pulled into existence by a failing unit test.)

Frequent Debugging can also be caused by Infrequently Run Tests (see Production Bugs on page 268 ). If we run our tests after every little change we make to the software, we can easily remember what we changed since the last time we ran the tests. Thus, when a test fails, we don't have to spend a lot of time troubleshooting the software to discover where the bug is—we know where it is because we remember putting it there!

Impact

Manual debugging is a slow, tedious process. It is easy to overlook subtle indications of a bug and spend many hours tracking down a single logic error. Frequent Debugging reduces productivity and makes development schedules much less predictable because a single manual debugging session could extend the time required to develop the software by half a day or more.

Solution Patterns

If we are missing the customer tests for a piece of functionality and manual user testing has revealed a problem not exposed by any automated tests, we probably have a case of Untested Requirements (see Production Bugs ). We can ask ourselves, "What kind of automated test would have prevented the manual debugging session?" Better yet, once we have identified the problem, we can write a test that exposes it. Then we can use the failing test to do test-driven bug fixing . If we suspect this to be a widespread problem, we can create a development task to identify and write any additional tests that would be required to fill the gap we just exposed.

Doing true test-driven development is the best way to avoid the circumstances that lead to Frequent Debugging . We should start as close as possible to the skin of the application and do storytest-driven development—that is, we should write unit tests for individual classes as well as component tests for the collections of related classes to ensure we have good Defect Localization .

Manual Intervention

A test requires a person to perform some manual action each time it is run.

Symptoms

The person running the test must do something manually either before the test is run or partway through the test run; otherwise, the test fails. The Test Runner may need to verify the results of the test manually.

Impact

Automated tests are all about getting early feedback on problems introduced into the software. If the cost of getting that feedback is too high—that is, if it takes the form of Manual Intervention —we likely won't run the tests very often and we won't get the feedback very often. If we don't get that feedback very often, we'll probably introduce lots of problems between test runs, which will ultimately lead to Frequent Debugging (page 248 ) and High Test Maintenance Cost (page 265 ).

Manual Intervention also makes it impractical to have a fully automated Integration Build [SCM] and regression test process.

Causes

The causes of Manual Intervention are as varied as the kinds of things our software does or encounters. The following are some general categories of the kinds of issues that require Manual Intervention . This list is by no means exhaustive, though.

Cause: Manual Fixture Setup
Symptoms

A person has to set up the test environment manually before the automated tests can be run. This activity may take the form of configuring servers, starting server processes, or running scripts to set up a Prebuilt Fixture (page 429 ).

Root Cause

This problem is typically caused by a lack of attention to automating the fixture setup phase of the test. It may also be caused by excessive coupling between components in the SUT that prevents us from testing a majority of the code in the system inside the development environment.

Possible Solution

We need to make sure that we are writing Fully Automated Tests. This may require opening up test-specific APIs to allow tests to set up the fixture. Where the issue is related to an inability to run the software in the development environment, we may need to refactor the software to decouple the SUT from the steps that would otherwise need to be done manually.

Cause: Manual Result Verification
Symptoms

We can run the tests but they almost always pass—even when we know that the SUT is not returning the correct results.

Root Cause

If the tests we write are not Self-Checking Tests (see page 26 ), we can be given a false sense of security because tests will fail only if an error/exception is thrown.

Possible Solution

We can ensure that our tests are all self-checking by including result verification logic such as calls to Assertion Methods (page 362 ) within the Test Methods (page 348 ).

Cause: Manual Event Injection
Symptoms

A person must intervene during test execution to perform some manual action before the test can proceed.

Root Cause

Many events in a SUT are hard to generate under program control. Examples include unplugging network cables, bringing down database connections, and clicking buttons on a user interface.

Impact

If a person needs to do something manually, it both increases the effort to run the test and ensures that the test cannot be run unattended. This torpedoes any attempt to do a fully automated build-and-test cycle.

Possible Solution

The best solution is to find ways to test the software that do not require a real person to do the manual actions. If the events are reported to the SUT through asynchronous events, we can have the Test Method invoke the SUT directly, passing it a simulated event object. If the SUT experiences the situation as a synchronous response from some other part of the system, we can get control of the indirect inputs by replacing some part of the SUT with a Test Stub (page 529 ) that simulates the circumstances to which we want to expose the SUT.

Further Reading

Refer to Chapter 11 , Using Test Doubles, for a much more detailed description of how to get control of the indirect inputs of the SUT.

Slow Tests

The tests take too long to run.

Symptoms

The tests take long enough to run that developers don't run them every time they make a change to the SUT. Instead, the developers wait until the next coffee break or another interruption before running them. Or, whenever they run the tests, they walk around and chat with other team members (or play Doom or surf the Internet or . . .).

Impact

Slow Tests obviously have a direct cost: They reduce the productivity of the person running the test. When we are test driving the code, we'll waste precious seconds every time we run our tests; when it is time to run all the tests before we commit our changes, we'll have an even more significant wait time.

Slow Tests also have many indirect costs:

  • The bottleneck created by holding the "integration token" longer because we need to wait for the tests to run after merging all our changes.
  • The time during which other people are distracted by the person waiting for his or her test run to finish.
  • The time spent in debuggers finding a problem that was inserted sometime after the last time we ran the test. The longer it has been since the test was run, the less likely we are to remember exactly what we did to break the test. This cost is a result of the breakdown of the rapid feedback that automated unit tests provide.

A common reaction to Slow Tests is to immediately go for a Shared Fixture (page 317 ). Unfortunately, this approach almost always results in other problems, including Erratic Tests (page 228 ). A better solution is to use a Fake Object (page 551 ) to replace slow components (such as the database) with faster ones. However, if all else fails and we must use some kind of Shared Fixture , we should make it immutable if at all possible.

Troubleshooting Advice

Slow Tests can be caused either by the way the SUT is built and tested or by the way the tests are designed. Sometimes the problem is obvious—we can just watch the green bar grow as we run the tests. There may be notable pauses in the execution; we may see explicit delays coded in a Test Method (page 348 ). If the cause is not obvious, however, we can run different subsets (or subsuites) of tests to see which ones run quickly and which ones take a long time to run.

A profiling tool can come in handy to see where we are spending the extra time in test execution. Of course, xUnit gives us a simple means to build our own mini-profiler: We can edit the setUp and tearDown methods of our Testcase Superclass (page 638 ). We then write out the start/end times or test duration into a log file, along with the name of the Testcase Class (page 373 ) and Test Method . Finally, we import this file into a spreadsheet, sort by duration, and voila—we have found the culprits. The tests with the longest execution times are the ones on which it will be most worthwhile to focus our efforts.

Causes

The specific cause of the Slow Tests could lie either in how we built the SUT or in how we coded the tests themselves. Sometimes, the way the SUT was built forces us to write our tests in a way that makes them slow. This is particularly a problem with legacy code or code that was built with a "test last" perspective.

Cause: Slow Component Usage

A component of the SUT has high latency.

Root Cause

The most common cause of Slow Tests is interacting with a database in many of the tests. Tests that have to write to a database to set up the fixture and read a database to verify the outcome (a form of Back Door Manipulation ; see page 327 ) take about 50 times longer to run than the same tests that run against in-memory data structures. This is an example of the more general problem of using slow components.

Possible Solution

We can make our tests run much faster by replacing the slow components with a Test Double (page 522 ) that provides near-instantaneous responses. When the slow component is the database, the use of a Fake Database (see Fake Object ) can make the tests run on average 50 times faster! See the sidebar " Faster Tests Without Shared Fixtures " on page 319 for other ways to skin this cat.

Cause: General Fixture
Symptoms

Tests are consistently slow because each test builds the same over-engineered fixture.

Root Cause

Each test constructs a large General Fixture each time a Fresh Fixture (page 311 ) is built. Because a General Fixture contains many more objects than a Minimal Fixture (page 302 ), it naturally takes longer to construct. Fresh Fixture involves setting up a brand-new instance of the fixture for each Testcase Object (page 382 ), so multiply "longer" by the number of tests to get an idea of the magnitude of the slowdown!

Possible Solution

Our first inclination is often to implement the General Fixture as a Shared Fixture to avoid rebuilding it for each test. Unless we can make this Shared Fixture immutable, however, this approach is likely to lead to Erratic Tests and should be avoided. A better solution is to reduce the amount of fixture setup performed by each test.

Cause: Asynchronous Test
Symptoms

A few tests take inordinately long to run; those tests contain explicit delays.

Root Cause

Delays included within a Test Method slow down test execution considerably. This slow execution may be necessary when the software we are testing spawns threads or processes ( Asynchronous Code; see Hard-to-Test Code on page 209 ) and the test needs to wait for them to launch, run, and verify whatever side effects they were expected to have. Because of the variability in how long it takes for these threads or processes to be started, the test usually needs to include a long delay "just in case"—that is, to ensure it passes consistently. Here's an example of a test with delays:

public  class  RequestHandlerThreadTest  extends  TestCase  {
      private  static  final  int  TWO_SECONDS  =  3000;
      public  void  testWasInitialized_Async()  =  throws  InterruptedException  {
          //  Setup
          RequestHandlerThread  sut  =  new  RequestHandlerThread();
          //  Exercise
          sut.start();
          //    Verify
          Thread.sleep(TWO_SECONDS);
          assertTrue(sut.initializedSuccessfully());
    }

    public  void  testHandleOneRequest_Async()
                throws  InterruptedException  {
          //  Setup
          RequestHandlerThread  sut  =  new  RequestHandlerThread();
          sut.start();
          //  Exercise
          enqueRequest(makeSimpleRequest());
          //  Verify
          Thread.sleep(TWO_SECONDS);
          assertEquals(1,  sut.getNumberOfRequestsCompleted());
          assertResponseEquals(makeSimpleResponse(),  getResponse());
    }
}

Impact

A two-second delay might not seem like a big deal. But consider what happens when we have a dozen such tests: It would take almost half a minute to run these tests. In contrast, we can run several hundred normal tests each second.

Possible Solution

The best way to address this problem is to avoid asynchronicity in tests by testing the logic synchronously. This may require us to do an Extract Testable Component (page 767 ) refactoring to implement a Humble Executable (see Humble Object on page 695 ).

Cause: Too Many Tests
Symptoms

There are so many tests that they are bound to take a long time to run regardless of how fast they execute.

Root Cause

The obvious cause of this problem is having so many tests. Perhaps we have such a large system that the large number of tests really is necessary, or perhaps we have too much overlap between tests.

The less obvious cause is that we are running too many of the tests too frequently!

Possible Solution

We don't have to run all the tests all the time! The key is to ensure that all tests are run regularly. If the entire suite is taking too long to run, consider creating a Subset Suite (see Named Test Suite on page 592 ) with a suitable cross section of tests; run this subsuite before every commit operation. The rest of the tests can be run regularly, albeit less often, by scheduling them to run overnight or at some other convenient time. Some people call this technique a "build pipeline." For more on this and other ideas, see the sidebar " Faster Tests Without Shared Fixtures " on page 319 .

If the system is large in size, it is a good idea to break it into a number of fairly independent subsystems or components. This allows teams working on each component to work independently and to run only those tests specific to their own component. Some of those tests should act as proxies for how the other components would use the component; they must be kept up-to-date if the interface contract changes. Hmmm, Tests as Documentation (see page 23 ); I like it! Some end-to-end tests that exercise all the components together (likely a form of storytests) would be essential, but they don't need to be included in the pre-commit suite.