Fixtures and Setup
Not only do we want to look at production code for refactoring opportunities, we want to look at the tests, too. Both our tests require the same line of code to create a Soundex instance. We’re not happy with even such seemingly trivial duplication. It adds up quickly and often turns into more complex duplication. It also clutters the tests, detracting from what’s important for a reader to understand.
It’s common for related tests to need common code. Google Mock lets us define a fixture class in which we can declare functions and member data for a related set of tests. (Technically, all Google Mock tests use a fixture that it generates behind the scenes.)
c2/10/SoundexTest.cpp | |
* | class SoundexEncoding: public testing::Test ... |
Get Modern C++ Programming with Test-Driven Development now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.