diff --git a/wiki/GoogleTestFAQ.wiki b/wiki/GoogleTestFAQ.wiki index 6414d845..44caecb8 100644 --- a/wiki/GoogleTestFAQ.wiki +++ b/wiki/GoogleTestFAQ.wiki @@ -587,47 +587,10 @@ namespace internal { EXPECT_TRUE(internal::Func(12345)); }}} -== I have an existing C++ Google Test test that I now would like to run twice with two values of an external variable. Is there a good way to test it without defining the test twice? == - -Yes! You can use a feature in Google Test called value-parameterized tests -which lets you repeat your tests with a number of different parameters. - -First, define your fixture. Derive it from `testing::TestWithParam`, -where `T` is your parameter type. In `SetUp()` or the fixture constructor, you -initialize your flag with the parameter value obtained from the -`GetParam()` method: - -{{{ -class FooTest : public testing::TestWithParam { - protected: - virtual void SetUp() { external_parameter = GetParam(); } -}; -}}} - -Now, define your tests as usual. The only difference is you'll be -using the `TEST_P` macro instead of `TEST_F` (P stands for -"parameterized".): - -{{{ -TEST_P(FooTest, TestBlah) { - ... // Tested code that depends on external_parameter goes here. -} -}}} - -And finally, instantiate tests in the `FooTest` test case with the -values you want: - -{{{ -INSTANTIATE_TEST_CASE_P(InstantiationName, FooTest, testing::Values(1, 2, 3)); -}}} - -`InstantiationName` is used to identify this particular instantiation -(you can instantiate a test case many times with different value lists). - -Each test in your test case will be run with each parameter value you -have specified, and the results will be reported separately for each -test/parameter combination. +== I would like to run a test several times with different parameters. Do I need to write several similar copies of it? == +No. You can use a feature called [GoogleTestAdvancedGuide#Value_Parameterized_Tests value-parameterized tests] which +lets you repeat your tests with a number of different parameters. == How do I test a file that defines main()? == @@ -661,6 +624,7 @@ test. For example: ... }}} + However, please remember this is a hack and should only be used as the last resort.