
232
Chapter 16 Behavior Smells
Root Cause
Interacting Test Suites usually occur when tests in separate test suites try to cre-
ate the same resource. When they are run in the same suite, the fi rst 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 remov-
ing 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 confl ict 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 confl ict with the fi xture being created by a later test, we should con-
sider 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 fi xture 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.
Erratic Test