What is the difference between TEST, TEST_F and TEST_P?
Asked Answered
O

2

70

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.

Ofris answered 29/10, 2019 at 2:29 Comment(0)
F
115

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

Figge answered 3/11, 2019 at 13:58 Comment(4)
Thanks for your answer, it's really help the community.Ofris
updated link to googletest primer: github.com/google/googletest/blob/master/docs/primer.mdGentleman
Here you can read the "Googletest Primer" in the official documentation, which is better formatted.Epstein
Advanced googletest Topics | GoogleTest introduces TEST_P() .Ingurgitate
W
10

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.

Westlund answered 9/5, 2023 at 6:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.