Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/124.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 单元测试类层次结构的良好实践_C++_Unit Testing_Testing_Class Hierarchy - Fatal编程技术网

C++ 单元测试类层次结构的良好实践

C++ 单元测试类层次结构的良好实践,c++,unit-testing,testing,class-hierarchy,C++,Unit Testing,Testing,Class Hierarchy,单元测试类层次结构的常见/广泛接受的实践是什么 我试图寻找与此问题相关的不同问题,但找不到全面的问题 假设有一个层次结构正在测试中 class Base { int base_internal() { return 10;} public: int virtual do_f() { return 0; } int execute() { return do_f()*base_in

单元测试类层次结构的常见/广泛接受的实践是什么

我试图寻找与此问题相关的不同问题,但找不到全面的问题

假设有一个层次结构正在测试中

class Base 
{
        int base_internal() { return 10;}                

    public:
        int virtual do_f() { return 0; }

        int execute() {
            return do_f()*base_internal();
        }
};

class DerrivedA : public Base
{
        int a_specefic() { return 4; }

    public:
        int virtual do_f()  { return a_specefic(); }
};


class DerrivedB : public Base
{
        int b_specefic() { return 8; }

    public:
        int virtual do_f()  { return b_specefic(); }
};

现在,对于单元测试基础设施,我在这里考虑两种策略

策略1。为每种类型设置“tester”类,从适当的类派生并进行测试

class TestForBase : public Base {...};
class TestForDerrivedA : public DerrivedA {...};
class TestForDerrivedB : public DerrivedB {....};
策略2。使用visitor设计模式的“协调化”版本(将有一个TestVisitor),然后使用accept()方法修改测试中的每个类,并在Tester类的visit()内部进行测试

class Base 
{
        void accept(Tester& t);
        int base_internal();

    public:
        int virtual do_f();
        int execute();
};


class Derrived : public Base
{
        void accept(Tester& t);
        int a_specefic();

    public:
        int virtual do_f();
};

struct Tester
{
        void visit(Base* b)  { ... };
        void visit(Derrived* d) { ... } ;
};


还有其他选择吗?

我不明白您想解决什么问题。为什么需要动态调度?只需编写代码来测试各种派生类就可以了。这没有问题,问题是关于良好实践的问题。我不认为仅仅用代码测试派生类是一个好的解决方案。好吧,你必须编写测试代码,无论是在访问者、派生类还是其他地方。我的问题是,为什么要在测试中添加动态调度,或者为什么您认为这样做是一件好事?您使用的是这样工作的特定测试框架吗?不要为测试类创建“一对一”单元测试。很糟糕,它将测试与实现细节结合在一起,因此很难进行重构。我建议阅读罗伯特·C·马丁的《干净代码》,或者在互联网上搜索他的《鲍勃叔叔》演讲。“UT的良好实践是一个非常宽泛的话题。”昆汀:好的,现在我有你的问题了。显然,有一个假设,即“框架”表示测试运行器类。如果我们在测试中不提供任何关系/模式,那么我们在测试运行程序中就没有灵活性。以一个简单的解决方案为例,将所有“分离”的测试实例保持为runner类的成员。每次有新类被测试时,我们将被迫修改运行程序并添加新实例。所以,应该有一种通用的方法来为测试层次结构构建单元测试类。希望这有帮助。我将更新问题,使其更具解释性。