2

I am using gtest as my testing framework. I am looking for a specific function that will be called only once.

So far, I have seen SetUp and the constructor of the class. But it seems like they are both called for each test. According to this example, the constructor is where,

You can do set-up work for each test here

leading me to believe that it's called once per test case. For the SetUp function,

Code here will be called immediately after the constructor (right before each test).

Does that mean both are called for each test? If so, where can I place code that will be called only once for the lifetime of the test class?

  • Possible duplicate of [SetUp vs Constructor in Test Fixture](https://stackoverflow.com/questions/13581738/setup-vs-constructor-in-test-fixture) – merlin2011 Dec 05 '17 at 00:16

1 Answers1

2

Use Test suite setup: SetUpTestSuite().

And yes, both the constructor and SetUp() method are called for each test method. In GTest, the test functions are implemented as subclasses of the Test Case class. That's why Test Case constructor is naturally called before execution of each of its children. See this FAQ question about what to use: constructor or SetUp().

Steve Broberg
  • 4,255
  • 3
  • 28
  • 40