Minimize Coupling

One of the primary arguments against extensive testing is that the maintenance of the tests creates an overhead that grows faster than the size of the source base. Most commonly, you will hear statements such as, “It’s too hard to maintain all those tests.”

In my experience, this effect can be almost eliminated by controlling the coupling2 of the tests. The means to control the coupling varies depending on the type of test under discussion.

2. The concept of coupling was introduced into the computer science lexicon by Edward Yourdon and Larry Constantine as part of structured design [SD]. It describes the degree of dependency between modules in a software system.

Coupling in system and integration tests generally occurs when the tests incorporate knowledge of the implementation in a white- or gray-box fashion. Sometimes overzealous testers reach beyond the necessary behavioral guarantees of the system, but often coupling is a symptom of software that was not built for testability by not providing access to verify all effects of an operation.

For unit testing, coupling often takes more insidious forms. For example, object-oriented design principles suggest that all attributes should be encapsulated in getters and setters. While this encapsulation has several benefits, including the implementation of logical and lazy-loading attributes, it is a very thin encapsulation in the majority of cases, in which it wraps a value or reference. The return of a reference to a complex internal attribute is often an implementation detail of convenience rather than a requirement of the interface. When this happens, the interface of the class under test is coupled to the definition of the internally referenced object. The unit test in turn exercises the interface, resulting in transitive coupling between the test and the internal object.

These transitive coupling relationships resemble the increase in communication relationships as the number of people on a team increases. If each person in a team of n people communicates directly with one another, the result is (n2-n)/2 lines of communication. For four people, there are only six lines of communication, a relatively manageable number. For ten people, the resulting 45 lines of communication introduce inefficiencies. Twenty people have 240 lines of communication, an already unmanageable number.

The techniques of this book are almost all focused in one way or another on reducing the coupling3 between your test and its corresponding implementation. By reducing the coupling between your components, you reduce the coupling in your tests. Applying this principle results in much more scalable testing efforts.

3. Coupling can be measured. See http://en.wikipedia.org/wiki/Coupling__computer_programming_#Module_coupling for one approach.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.93.12