Test Fixtures: Using the Same Data Configuration for Multiple Tests
- Derive a class from
::testing::Test
. Start its body withprotected:
, as we’ll want to access fixture members from sub-classes. - Inside the class, declare any objects you plan to use.
- If necessary, write a default constructor or
SetUp()
function to prepare the objects for each test. A common mistake is to spellSetUp()
asSetup()
with a smallu
- Useoverride
in C++11 to make sure you spelled it correctly. - If necessary, write a destructor or
TearDown()
function to release any resources you allocated inSetUp()
. To learn when you should use the constructor/destructor and when you should useSetUp()/TearDown()
, read the FAQ. - If needed, define subroutines for your tests to share.
When using a fixture, use TEST_F()
instead of TEST()
as it allows you to access objects and subroutines in the test fixture: