简体   繁体   中英

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?

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.

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.

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