What is the difference between TEST, TEST_F and TEST_P?

Nguyễn Đức Tâm picture Nguyễn Đức Tâm · Oct 29, 2019 · Viewed 11.8k times · Source

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.

Answer

Himanshu Jaju picture Himanshu Jaju · Nov 3, 2019

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