I have researched a lot about gtest/gmock but none of them gave me the right answer. I new to C++ so any help would be really appreciated.
Like TEST() , the first argument is the test case name, but for TEST_F() this must be the name of the test fixture class. You've probably guessed: _F is for fixture. Unfortunately, the C++ macro system does not allow us to create a single macro that can handle both types of tests.
TEST_P() is useful when you want to write tests with a parameter. Instead of writing multiple tests with different values of the parameter, you can write one test using TEST_P() which uses GetParam() and can be instantiated using INSTANTIATE_TEST_SUITE_P() . Example test. Follow this answer to receive notifications.
The test suite name is TestFixtureName . Both arguments TestFixtureName and TestName must be valid C++ identifiers and must not contain underscores ( _ ). TestFixtureName must be the name of a test fixture class—see Test Fixtures. The statements within the test body can be any code under test.
INSTANTIATE_TEST_CASE_P( LeapYearTests, LeapYearParameterizedTestFixture, ::testing::Values( 1, 711, 1989, 2013 )); Here we call the INSTANTIATE_TEST_CASE_P macro with first with a unique name for the instantiation of the test suite. This name can distinguish between multiple instantiations.
All documentation is covered in the official github repo. The primer documentation also covers a lot of information regarding the test macros. You could use the following summary and the examples linked to choose what you want to use.
TEST()
is useful when you want to write unit tests for static or global functions or simple classes. Example test
TEST_F()
is useful when you need access to objects and subroutines in the unit test. Example test
TEST_P()
is useful when you want to write tests with a parameter. Instead of writing multiple tests with different values of the parameter, you can write one test using TEST_P()
which uses GetParam()
and can be instantiated using INSTANTIATE_TEST_SUITE_P()
. Example test
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With