简体   繁体   中英

C++ Google Test Where to Put Test Fixture Constructor Definition

I'm using Google Test for my C++ code recently. When I read how to setup the test fixture for tests, I get a little bit confused. The Writing the main() Function session showed an example about what the test fixture class looks like. However, when it comes tothe constructor definitions, should we put it just inside the test fixture class? For example, like the following code snippet given by google test doc:

class FooTest : public ::testing::Test {
 protected:
  // You can remove any or all of the following functions if its body
  // is empty.

  FooTest() {
    // You can do set-up work for each test here.
  }

  virtual ~FooTest() {
    // You can do clean-up work that doesn't throw exceptions here.
  }
}

I also looked at the definition of the macro TEST_F(test_fixture_name, test_name) , it seems like for each test associated with the same test fixture, the macro will create a new subclass of the test fixture class.

Given the above fact,

  1. if the constructor's work is heavy, does that mean the implicitly inline of the test fixture's constructor will let the compiler expand the constructor's large pieces of code everywhere? (or this doesn't matter in the same translation unit?)

  2. Does it make more sense to define the constructor outside of the test fixture class in this situation? But this will make the test code less readable, I don't really know what to do..

Could anybody give me some suggestions on this? Thanks!

On question 1, that's entirely up to the compiler - it has broad latitude to decide if and how to inline your functions. Even if you explicitly declare a function as inline , it's still just a suggestion as far as the compiler is concerned, and it's free to ignore that suggestion if it decides the resulting machine code would be too bloated or inefficient.

The C++ FAQ has some more details on the topic:

There are several ways to designate that a function is inline, some of which involve the inline keyword, others do not. No matter how you designate a function as inline, it is a request that the compiler is allowed to ignore: the compiler might inline-expand some, all, or none of the places where you call a function designated as inline. (Don't get discouraged if that seems hopelessly vague. The flexibility of the above is actually a huge advantage: it lets the compiler treat large functions differently from small ones, plus it lets the compiler generate code that is easy to debug if you select the right compiler options.)

On question 2, I'd recommend just going with whatever you find most readable. When I use Google Tests, I personally put all the "shared" code inside the test fixture definition, followed immediately by TEST_F declarations for all of the unit tests that run inside that fixture.

class MyTestCase : public ::testing::Test
{
    virtual void SetUp() override
    {
        // ...
    }
}

TEST_F(MyTestCase, UnitTestNumber1)
{
    // testing stuff here
}

// ...more tests...

That's just a suggestion, though. Pick a standard that works for you and use it consistently.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM