mirror of
https://github.com/google/googletest.git
synced 2025-04-06 05:55:04 +00:00
Edited wiki page through web user interface.
This commit is contained in:
parent
401938fdbe
commit
622b7a7084
1 changed files with 29 additions and 3 deletions
|
@ -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!
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue