UnitTest++ constructing fixtures multiple times?

1.5k Views Asked by At

I'm writing some unit tests in UnitTest++ and want to write a bunch of tests which share some common resources. I thought that this should work via their TEST_FIXTURE setup, but it seems to be constructing a new fixture for every test. Sample code:

#include <UnitTest++.h>

struct SomeFixture {
    SomeFixture() {
        // this line is hit twice
    }
};

TEST_FIXTURE(SomeFixture, FirstTest) {
}

TEST_FIXTURE(SomeFixture, SecondTest) {
}

I feel like I must be doing something wrong; I had thought that the whole point of having the fixture was so that the setup/teardown code only happens once. Am I wrong on this? Is there something else I have to do to make it work that way?

2

There are 2 best solutions below

1
On BEST ANSWER

The point of a test fixture is to not have to write the same setup/teardown code in every single test, not to share state. If you want to share state, then you can simply reference a class with static fields and static functions in your tests, and then you can use the standard TEST macro instead of TEST_FIXTURE.

0
On

the whole point of having the fixture was so that the setup/teardown code only happens once

No, the whole point of fixtures is for the fixture to be repeated every test. What you are seeing is expected and correct behavior.