Peter
Peter

Reputation: 7324

UnitTest++ constructing fixtures multiple times?

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?

Upvotes: 1

Views: 1583

Answers (2)

Michael Aaron Safyan
Michael Aaron Safyan

Reputation: 95499

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.

Upvotes: 6

Billy ONeal
Billy ONeal

Reputation: 106540

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.

Upvotes: 2

Related Questions