views:

84

answers:

2

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?

+1  A: 

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.

Billy ONeal
+2  A: 

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.

Michael Aaron Safyan
Ha! I win by 10 seconds.... +1 :)
Billy ONeal
Okay, if that's how it's supposed to work. I'll admit to a little disappointment; I don't see TEST_FIXTURE being as useful to me this way, but I guess I'll have to live with it. Thanks for clearing it up, anyway :)
Peter