user997112
user997112

Reputation: 30615

Boost unit test fixture inheriting test class, to have access to protected methods?

I have worked with Google Test/GTest previously, which I am sure allowed your text fixture class to inherit the class you were testing, so it could have access to the protected methods of the class being tested (without them needing to be exposed as public).

I am trying to achieve the same thing with Boost unit test, but it won't allow me to access a protected method (even though my test fixture class inherits from the class being tested).

Is there a way for test fixture classes to access protected methods of classes being tested in Boost Test? If not, what is the best way to expose private/protected methods for unit testing?

Upvotes: 1

Views: 1136

Answers (1)

Mark Storer
Mark Storer

Reputation: 15868

The test function from BOOST_FIXTURE_TEST_CASE is part of a struct that inherits from the fixture. It's public and protected members can be treated like local variables. Throw in a fixture that inherits from the class you want to test, and you're off to the races:

class TestClass {
public:
    TestClass() {}
protected:
    bool Foo() { return true; }
};

class MyFixture : public TestClass {
public:
    MyFixture() { bar = 1; }
protected:
    int bar;
};

BOOST_FIXTURE_TEST_CASE(MyTest, MyFixture) {
    BOOST_TEST(bar == 1);
    BOOST_TEST(Foo());
}

Upvotes: 2

Related Questions