The Artima Developer Community
Sponsored Link

Test-Driven Development
A Conversation with Martin Fowler, Part V
by Bill Venners
December 2, 2002

<<  Page 4 of 4


Unit Testing

Bill Venners: What is the unit in unit test? Define unit test.

Martin Fowler: That's very difficult. To a first approximation, it's a class. But as you work with unit tests more, you begin to realize you're testing little areas of responsibility, and that could be a part of a class or it could be several classes together. I don't get too hung up about it, but I'd say if you're just starting out, think of unit tests as just writing a test case per class.

Bill Venners: And the reason you call it "unit" testing is that you're testing individual pieces that make up the program. To get a robust whole, you build it on robust parts, or robust units. To get the parts robust, you test them individually with unit tests.

Martin Fowler: That's the notion of where unit testing originally came from. Unit testing in XP is often unlike classical unit testing, because in XP you're usually not testing each unit in isolation. You're testing each class and its immediate connections to its neighbors.

Bill Venners: What's the difference between unit and functional testing?

Martin Fowler: The current name in XP circles for functional tests is acceptance tests. Acceptance tests view the system more as a black box and test more end to end across the whole system. You would have unit tests for individual classes in the domain and database mapping layers. In most of these unit tests, you might not even connect the database. You would stub the database out. But with the functional tests, which go end to end, you would want everything connected.

Bill Venners: When do you stop writing tests? You say in Refactoring, "There's a point of diminishing returns with testing, and there's a danger that by writing too many tests you become discouraged and end up not writing any. You should concentrate on where the risk is." How do you know where the risk is?

Martin Fowler: Ask yourself which bits of the program would you be scared to change? One test I've come up with since the Refactoring book is asking if there is any line of code that you could comment out and the tests wouldn't fail? If so, you are either missing a test or you've got an unnecessary line of code. Similarly, take any Boolean expression. Could you just reverse it? What test would fail? If there's not a test failing, then, you've obviously got some more tests to write or some code to remove.

Bill Venners: Sounds like you could actually automate that.

Martin Fowler: Actually, there is a program that does that. It's called JesTer. The problem is it takes a long time to run, because every time it makes one minor mutation it has to run the entire build test suite.

Talk Back!

Have an opinion about test-first development, incremental interface design, or unit testing? Discuss this article in the News & Ideas Forum topic, Test-Driven Development.


Refactoring: Improving the Design of Existing Code, by Martin Fowler with Kent Beck, John Brant, William Opdyke, and Don Roberts is at at:

To Be Explicit, an article by Martin Fowler first published in IEEE Software:

Public versus Published Interfaces, an article by Martin Fowler first published in IEEE Software:

JesTer, the tool mentioned by Martin Fowler that finds places in your code not covered by a unit test:

The Pragmatic Programmer: From Journeyman to Master, by Andrew Hunt and David Thomas, is at at:

IntelliJ IDEA, a Java IDE with refactoring support:

Eclipse, an open source IDE with refactoring support:

A catalog of summaries of refactorings mentioned in the book, Refactoring:

A refactoring portal maintained by Martin Fowler contains links to refactoring tools and other refactoring sites:

Martin Fowler's links to extreme programming resources:

Articles written by Martin Fowler about XP and agile methods:

Patterns of Enterprise Application Architecture, by Martin Fowler is at at:

UML Distilled: A Brief Guide to the Standard Object Modeling Language, by Martin Fowler and Kendall Scott is at at:

Planning Extreme Programming, by Kent Beck and Martin Fowler is at at:

Analysis Patterns: Reusable Object Models , by Martin Fowler is at at:

Martin Fowler's website contains many articles, book chapters, and other information from Martin:

<<  Page 4 of 4

Sponsored Links

Copyright © 1996-2018 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use