Testing on the Toilet

September 4, 2008

Data-Driven Traps! When writing a unit test, it is tempting to exercise many scenarios by writing a data-driven test. For example, to test the function IsWord, you could write (ARRAYSIZE is a macro that returns the number of elements in an array): const struct {const char* word; bool is_word;} test_data[] = { {"milk", true}, {"centre", false}, {"jklm", false}, }; TEST(IsWordTest, TestEverything) { for (int i = 0; i < ARRAYSIZE(test_data); i++) EXPECT_EQ(test_data[i].is_word, IsWord(test_data[i].word)); }

This keeps the test code short and makes it easy to add new tests but makes it hard to identify a failing test assertion (and to get the debugger to stop in the right place). As your code grows the test data tends to grow faster than linearly. For example, if you add a parameter called locale to IsWord, the test could become: Locale LOCALES[] = { Word::US, Word::UK, Word::France, ... }; const struct {const char* word; bool is_word[NUM_LOCALES];} test_data[] = { {"milk", {true, true, false, ...}}, // one bool per language {"centre", {false, true, true, ...}}, {"jklm", {false, false, false, ...}} }; TEST(IsWordTest, TestEverything) { for (int i = 0; i < ARRAYSIZE(test_data); i++) for (int j = 0; j < ARRAYSIZE(LOCALES); j++) EXPECT_EQ(test_data[i].is_word[j], IsWord(test_data[i].word, LOCALES[i])); }

The change was relatively easy to make: change the data structure, fill in the boolean values for other locales and add a loop to the test code. But even this small changed has made the test harder to read and slower as it repeats potentially unnecessary checks. In addition, both the test AND the code have changed. How do you know the test is not broken? (Actually, it is broken. Can you see the bug?) By contrast, keeping the data in the test gives us: TEST(IsWordTest, IsWordInMultipleLocales) { EXPECT_TRUE(IsWord("milk", Word::UK)); EXPECT_TRUE(IsWord("milk", Word::US)); EXPECT_FALSE(IsWord("milk", Word::France)); } TEST(IsWordTest, IsWordWithNonExistentWord) { }

EXPECT_FALSE(IsWord("jklm", Word::US));

// 'jklm' test is not repeated // as it uses the same code path

The difference between these two code snippets is minor but real-life data-driven tests quickly become unmanageable. A complete example would not fit on this page but if you look at your code base, you will find a few specimens lurking in some (not-so) forgotten test classes. They can be identified by their large size, vague names and the fact that they provide little to no information about why they fail.

More information, discussion, and archives: http://googletesting.blogspot.com Copyright © 2007 Google, Inc. Licensed under a Creative Commons Attribution–ShareAlike 2.5 License (http://creativecommons.org/licenses/by-sa/2.5/).

Data-Driven Traps!

Sep 4, 2008 - This keeps the test code short and makes it easy to add new tests but makes it hard to ... As your code grows the test data tends to grow faster.

142KB Sizes 1 Downloads 308 Views

Recommend Documents

Uncertainty Traps
We develop a business cycles theory of endogenous uncertainty. ◮ Large evidence of heightened uncertainty in 2007-2012 (Bloom et al.,2012; Ludvigson et al.

Conservatism and Liquidity Traps
1 λ. Note: The figure displays how the output gap, the inflation rate, and the nominal interest rate in both states vary with λ. The dash-dotted vertical lines indicate ...

Institutional Traps
atively simple goods and services by low-skilled workers in vertically integrated firms and product chains. On the .... in-house, i.e., within one vertically integrated firm making up the whole product chain, then the expected ..... In the main text,

Uncertainty Traps
May 10, 2016 - We develop a theory of endogenous uncertainty and business cycles in which short- ..... We define a recursive competitive equilibrium as follows.10 ..... The top panel presents three different series of shocks to the mean of beliefs µ

Status Traps
Apr 25, 2016 - adult child's percentile rank exceeds that of the parents.4 ...... with higher levels of community engagement and stronger social networks help ...

Data-Driven Traps!
Sep 4, 2008 - This keeps the test code short and makes it easy to add new tests but makes it hard to identify a failing test assertion (and to get the debugger to ...

Traps of Satan.pdf
Page 2 of 2. Page 2 of 2. Traps of Satan.pdf. Traps of Satan.pdf. Open. Extract. Open with. Sign In. Main menu. Displaying Traps of Satan.pdf.

Tax Tips & Traps
tax credit) notes that if a taxpayer or a child uses an intensive insulin management system (IMS) or an insulin pump with respect to Diabetes, they may qualify for the DTC. Suite 300. 422 Richards Street. Vancouver, British Columbia. V6B 2Z4. (604) 8

Data-Driven Traps! Code
Sep 4, 2008 - This keeps the test code short and makes it easy to add new tests but makes it hard to identify a failing test assertion (and to get the debugger to stop in the right place). As your code grows the test data tends to grow faster than li

Conservatism and Liquidity Traps
The effect of conservatism on output in the high state is ambiguous. ... In Appendix D, we provide a numerical illustration of the aforementioned model properties. ..... with a length of 1050 periods each, where the first 50 periods are discarded.

Gradualism and Liquidity Traps
†Board of Governors of the Federal Reserve System, Division of Research and Statistics, 20th Street and Consti- ... series of small or moderate steps in the same direction. .... None of these studies, however, accounts for the ZLB on nominal intere

Gradualism and Liquidity Traps
smoothing arising from its ability to steer private-sector expectations by .... (9) and the private-sector equilibrium conditions (1) and (2) previously described.

Uncertainty Traps - UCLA Department of Economics
May 10, 2016 - Firms are heterogeneous in the cost of undertaking this investment and hold common beliefs about the fundamental. Beliefs are regularly ...

Reputation and Liquidity Traps
rate is kept at the lower bound while the crisis shock lasts but returns to the steady state as soon as the ... of the commitment and delay the recovery. [Carney ...

Fake Alphas, Tail Risk and Reputation Traps - ITAM Finance Conference
Dec 23, 2013 - choose to get over-exposed to tail risk due to their reputation concerns in advance of the crisis. Then market ...... Reputational cheap talk. The Rand .... m≥n is bounded and equicontinuous over the domain. [1/n,1 − 1/n] .

101 chess opening traps pdf download
There was a problem previewing this document. Retrying... Download. Connect more apps... Try one of the apps below to open or edit this item. 101 chess ...

Karl Wiegers Describes 10 Requirements Traps ... -
This article describes ten common traps that software projects ..... Even better, store your requirements in the database of a.