Hosted by:
Unit testing expertise at your fingertips!
Home | Discuss | Lists

Setup External Resource

The book has now been published and the content of this chapter has likely changed substanstially.

The system under test (SUT) depends on the contents of an external resource which is acting as a Mystery Guest in our test.

Create an external resource within the fixture setup logic of the test rather than using a predefined resource.

From [RTC]:

If it is necessary for a test to rely on external resources, such as directories, databases, or files, make sure the test that uses them explicitly creates or allocates these resources before testing, and releases them when done (take precautions to ensure the resource is also released when tests fail).

Implementation Notes

When our SUTmust use an external resources such as a file and we absolutely, positively cannot replace the access mechanism with a Test Stub (page X) or Fake Object (page X), we may need to live with the fact that we have to use an external resource. The problem with external resources is that the test reader cannot tell what they contain, they may disappear unexpectedly causing tests to fail because of Resource Optimism (see Erratic Test on page X) and they may result in Interacting Testss (see Erratic Test) and Test Run Wars (see Erratic Test). Setup External Resource does not help us with the last problem but it does avoid the problems of Mystery Guest (see Obscure Test on page X) and Resource Optimism.

To implement the Setup External Resource refactoring we simply pull the contents of the external resource into our Test Method (page X), setUp method or a Test Utility Method (page X) called by them. Using the contents we construct the external resource within our test code thereby making it evident to the test reader exactly what the test depends on. This also guarantees that the resource exists because we create it in every test run.

Page generated at Wed Feb 09 16:39:52 +1100 2011

Copyright © 2003-2008 Gerard Meszaros all rights reserved

All Categories
Introductory Narratives
Web Site Instructions
Code Refactorings
Database Patterns
DfT Patterns
External Patterns
Fixture Setup Patterns
Fixture Teardown Patterns
Front Matter
Result Verification Patterns
Test Double Patterns
Test Organization
Test Refactorings
Test Smells
Test Strategy
Value Patterns
XUnit Basics
xUnit Members
All "Test Refactorings"
Extract Testable Component
Inline Resource
Make Resource Unique
Minimize Data
Replace Dependency with Test Double
Setup External Resource