I have researched a lot about gtest/gmock but none of them gave me the right answer. I'm new to C++ so any help would be really appreciated.
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
TEST_P()
. –
Ingurgitate I think my answer to this is:
In C++ testing frameworks, TEST, TEST_F, and TEST_P are macros used to define test cases. Here's the difference between them:
TEST: This macro is used to define a standalone test case. It is typically used when you have a single test case that does not need any setup or teardown code.
TEST_F: This macro is used to define a test case that needs to set up some fixtures before running the test and tear them down afterward. A fixture is a set of data or objects that you want to use across multiple tests. TEST_F is short for "test fixture".
TEST_P: This macro is used to define a test case that takes parameters. It is typically used when you want to run the same test with different inputs. The TEST_P macro is followed by a set of parameters that are passed to the test case.
In summary, TEST is for standalone test cases, TEST_F is for test cases with fixtures, and TEST_P is for test cases with parameters. The choice of which macro to use depends on the specific testing needs and requirements of your project.
© 2022 - 2024 — McMap. All rights reserved.