c++ - boost 单元测试夹具继承测试类,可以访问 protected 方法?

标签 c++ boost googletest boost-test

我以前使用过 Google Test/GTest,我确信它允许你的文本夹具类继承你正在测试的类,所以它可以访问被测试类的 protected 方法(不需要它们暴露为公开)。

我试图通过 Boost 单元测试实现相同的目的,但它不允许我访问 protected 方法(即使我的测试夹具类继承自被测试的类)。

有没有办法让测试夹具类访问在 Boost 测试中被测试的类的 protected 方法?如果不是,公开单元测试的私有(private)/ protected 方法的最佳方法是什么?

最佳答案

来自 BOOST_FIXTURE_TEST_CASE 的测试函数是从夹具继承的结构的一部分。它的公共(public)和 protected 成员可以像局部变量一样对待。放入一个继承自您要测试的类的夹具,然后您就可以开始比赛了:

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());
}

关于c++ - boost 单元测试夹具继承测试类,可以访问 protected 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54134664/

相关文章:

c++ - 在 C++ 中的大 vector 中搜索

c++ - 开发-C++/TDM-GCC : Linkage Problems with Boost Libaries Downloaded from boost. 组织

c++ - Google Test中的测试类的构造方法

c++ - 谷歌测试 : can not create string from char array?

cmake - 使用 CMake,如何在 gtest_discover_tests --gtest_list_tests 调用上设置环境属性?

c++ - deleteNode 函数出现中断连接列表

c++ - 转发声明结构时出错 : "has a previous declaration here"

c++ - 我如何检测我的应用程序何时最小化?

c++ - 如何修复 "Invalid read of size 8 - 40 bytes inside a block of size 64 free' d"

c++ - 使用 cmake for arm 与静态链接的 boost 库交叉编译