简体   繁体   English

编写可以访问私有/受保护状态的单元测试

[英]Writing unit tests which may access private/protectedstate

I use Boost Test for unit tests.我使用 Boost Test 进行单元测试。 I typically have a fixture struct:我通常有一个夹具结构:

class ClassBeingTested
{
protected:
    int _x;             // Want to access this directly in my unit tests
};

struct TestFixture : public ClassBeingTested
{
    // I can access ClassBeingTested::_x here but it means i have to add getters for each test to call
    ClassBeingTested _bla;
};

However, even if my fixture inherits from ClassBeingTested or uses a friend relationship I cannot access private/protected methods/state from each individual test:但是,即使我的夹具继承自ClassBeingTested或使用朋友关系,我也无法从每个单独的测试中访问私有/受保护的方法/状态:

BOOST_FIXTURE_TEST_CASE(test1, TestFixture)
{
    _bla.doSomething();
    BOOST_REQUIRE_EQUAL(_bla.x, 10);    // Compiler error. I cannot access ClassBeingTested::_x here
}

only the fixture, which means I have to add a new getter (or test) for each access I wish to make.只有fixture,这意味着我必须为我希望进行的每次访问添加一个新的getter(或测试)。

Is there any way to achieve this?有什么办法可以做到这一点? I have to add public getter methods to ClassBeingTested which are only used by tests, which isn't ideal.我必须向ClassBeingTested添加公共 getter 方法,这些方法仅供测试使用,这并不理想。

(Please no replies of 'test using the public interface', that's not always possible). (请不要回复“使用公共接口进行测试”,这并不总是可能的)。

You can make a friend test buddy, struct ClassBeingTestedBuddy;可以交朋友测试好友, struct ClassBeingTestedBuddy; with friend struct ClassBeingTestedBuddy;friend struct ClassBeingTestedBuddy; in the class ClassBeingTested .class ClassBeingTested

Have the test buddy class expose all the protected or private variables or methods that you need for all your tests.让测试伙伴 class 公开所有测试所需的所有受保护或私有变量或方法。

It'd look like...它看起来像...

struct ClassBeingTestedBuddy {
    ClassBeingTested* obj;
    ClassBeingTestedBuddy(ClassBeingTested* obj_)
      : obj{obj_} {}
};

...plus anything else you'd want to expose. ...加上你想暴露的任何其他东西。

This is but one way of allowing the tests a bridge to the protected and private data and methods.这只是让测试成为连接受保护和私有数据和方法的桥梁的一种方式。 But since it is all code in your project, for your testing use, it is a reasonable way of getting the test code access without too much instrumenting your actual code.但是由于它是您项目中的所有代码,因此对于您的测试使用,这是一种合理的方式来获取测试代码访问权限,而无需过多地检测您的实际代码。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在单元测试中访问私有成员? - How to access private members in unit tests? 成员空间可以访问父类的私有成员 - Memberspaces may access private members of parent class 在函数/类声明附近编写C ++单元测试 - Writing C++ unit tests near function/class declarations 编写Makefile.am来调用googletest单元测试 - Writing a Makefile.am to invoke googletest unit tests 有没有一种方法可以检测哪些测试在Boost单元测试二进制文件中 - Is there a way to detect which tests are inside a boost unit-test binary 对更改私有成员变量的函数进行单元测试 - Unit testing a function which changes private member variables 如何在MVVM Windows应用商店应用中为私有C ++模型层编写Visual Studio单元测试? - How to write Visual Studio unit tests for private C++model layer in MVVM Windows Store app? 是否可以将c ++单元测试集成到maven构建过程中,如果可以,则使用哪种单元测试框架 - Is it possible to integrate c++ unit tests into a maven build process and if yes with which unit testing framework 在单元测试中,如何在不使用可能错过新成员的 operator== 的情况下比较两个对象? - In unit testing, how to compare two objects without using operator== which may miss new members? 当一个线程正在编写另一个线程可能同时执行的代码时,如何在ARM上同步? - How to synchronize on ARM when one thread is writing code which the other thread may be executing concurrently?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM