Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a gtest equivalent for fixture-level setup/teardown?

So, I know there's "literally" fixtures for gtest, but the constructor/destructor and setup/teardown functions will execute after each test rather than after the entire set of tests in the fixture.

I can think of ways of hacking around this, but is there some built-in support that I'm not finding?

like image 249
user3338893 Avatar asked Dec 05 '14 18:12

user3338893


1 Answers

Google Test supports set-up and tear-down on test (test method) level, suite (class) level and also on program level. The later is the one you seam to look for:

https://github.com/google/googletest/blob/master/docs/advanced.md#global-set-up-and-tear-down describes how you can derive and register you own environment-fixture class implementing set-up and tear-down methods, which are called only once during the execution of your test runner.

In short you can do something similar to that:

class SetupEnvironment : public ::testing::Environment
{
public:
  virtual ~SetupEnvironment();
  void SetUp() override { ... }
  void TearDown() override { ... }
};

int main(int argc, char* argv[])
{
  testing::AddGlobalTestEnvironment(new SetupEnvironment());
  return RUN_ALL_TESTS();
}
like image 59
salchint Avatar answered Nov 12 '22 20:11

salchint