From 622b7a708479998bf52458fd010f552c9d5c03ff Mon Sep 17 00:00:00 2001 From: vladlosev Date: Thu, 20 Nov 2008 17:55:40 +0000 Subject: [PATCH] Edited wiki page through web user interface. --- wiki/GoogleTestAdvancedGuide.wiki | 32 ++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/wiki/GoogleTestAdvancedGuide.wiki b/wiki/GoogleTestAdvancedGuide.wiki index 7d5f08cc..d272dc6c 100644 --- a/wiki/GoogleTestAdvancedGuide.wiki +++ b/wiki/GoogleTestAdvancedGuide.wiki @@ -810,10 +810,36 @@ _Availability:_ Linux, Windows, Mac. = Value Parameterized Tests = _Value-parameterized tests_ allow you to test your code with different -parameters without writing multiple copies of the same test. This is -useful in a number of situations, for example: +parameters without writing multiple copies of the same test. + +Suppose you write a test for your code and then realize that your code is affected by a presence of a Boolean command line flag. + +{{{ +TEST(MyCodeTest, TestFoo) { + // A code to test foo(). +} +}}} + +Usually people factor their test code into a function with a Boolean parameter in such situations. The function sets the flag, then executes the testing code. + +{{{ +void TestFooHelper(bool flag_value) { + flag = flag_value; + // A code to test foo(). +} + +TEST(MyCodeTest, TestFooo) { + TestFooHelper(false); + TestFooHelper(true); +} +}}} + +But this setup has serious drawbacks. First, when a test assertion fails in your tests, it becomes unclear what value of the parameter caused it to fail. You can stream a clarifying message into your `EXPECT`/`ASSERT` statements, but it you'll have to do it with all of them. Second, you have to add one such helper function per test. What if you have ten tests? Twenty? A hundred? + +Value-parameterized tests will let you write your test only once and then easily instantiate and run it with an arbitrary number of parameter values. + +Here are some other situations when value-parameterized tests come handy: - * You have a piece of code whose behavior is affected by one or more command-line flags. You want to make sure your code performs correctly for various values of those flags. * You wan to test different implementations of an OO interface. * You want to test your code over various inputs (a.k.a. data-driven testing). This feature is easy to abuse, so please exercise your good sense when doing it!