Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between TEST, TEST_F and TEST_P?

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 image 747
Nguyễn Đức Tâm Avatar asked Oct 29 '19 02:10

Nguyễn Đức Tâm


People also ask

What is the difference between test and Test_f?

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.

What is Test_p?

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.

What is test suite in Gtest?

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.

What is Instantiate_test_case_p?

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.


1 Answers

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

like image 164
Himanshu Jaju Avatar answered Oct 08 '22 13:10

Himanshu Jaju