Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/140.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++中对受保护的方法进行单元测试?_C++_Unit Testing_Nunit_Protected - Fatal编程技术网

如何在C++;? 如何在C++中对受保护的方法进行单元测试?

如何在C++;? 如何在C++中对受保护的方法进行单元测试?,c++,unit-testing,nunit,protected,C++,Unit Testing,Nunit,Protected,在爪哇中,我要么在与测试类相同的包中创建测试类,要么创建一个匿名子类,公开我在测试类中需要的方法,但是在C++中,这些方法都不适用。 我使用NUng.< P>测试非托管C++类。考虑一个公共的,可能是静态的“单元测试”函数。 丑陋,但比我能想到的使用宏或friends之类的替代方法要好。声明friend类MyClass\u UnitTest;在你的班上。然后,您可以在单元测试程序的其他地方定义MyClass_UnitTest,该程序可以完全访问MyClass内部,但不必在发布应用程序中提供实现。

在爪哇中,我要么在与测试类相同的包中创建测试类,要么创建一个匿名子类,公开我在测试类中需要的方法,但是在C++中,这些方法都不适用。

我使用NUng.

< P>测试非托管C++类。考虑一个公共的,可能是静态的“单元测试”函数。
丑陋,但比我能想到的使用宏或friends之类的替代方法要好。

声明friend类MyClass\u UnitTest;在你的班上。然后,您可以在单元测试程序的其他地方定义MyClass_UnitTest,该程序可以完全访问MyClass内部,但不必在发布应用程序中提供实现。
请参阅文档,了解如何执行此操作的示例。

假设您指的是公共可访问类的受保护方法:

在测试代码中,定义被测试类的派生类(直接或从其派生类之一)。为受保护的成员添加访问器,或在派生类中执行测试。“受保护”的访问控制在C++中并不可怕:它不需要基类的合作就可以“破解”它。因此,最好不要在基类中引入任何“测试代码”,甚至不要引入友元声明:

// in realclass.h
class RealClass {
    protected:
    int foo(int a) { return a+1; }
};

// in test code
#include "realclass.h"
class Test : public RealClass {
    public:
    int wrapfoo(int a) { return foo(a); }
    void testfoo(int input, int expected) {
        assert(foo(input) == expected);
    }
};

Test blah;
assert(blah.wrapfoo(1) == 2);
blah.testfoo(E_TO_THE_I_PI, 0);

我使用并使CxxTest派生自包含受保护成员函数的类。如果你还在搜索你喜欢的C++单元测试框架,看看.

你也可以使用关键字来公开公共块(使用.< /p>)
//在realclass.h中
阶级{
受保护的:
intfoo(inta){返回a+1;}
int foo(string a){return a.length();}//重载也可以工作
模板int foo(const T&a){return 1;}//模板也可以工作
};
//在测试代码中
#包括“realclass.h”
类RealClassExposed:public RealClass{
公众:
使用RealClass::foo;
};
真实的谎言;
断言(blah.foo(1)==2);
断言(blah.foo(“test”)==4);
断言(blah.foo(blah)==1);

<>:

P>使用C++定义有一个简单的C++解决方案。只需包上“类下级测试”的包含:

#define protected public
 #define private   public
    #include <ClassUnderTest.hpp>
 #undef protected
#undef private
#定义受保护的公共
#定义私人和公共
#包括
#未定义保护
#未定义私有

您使用的是GestTestASM?还是您从NUnter(.NET)中运行非托管C++代码??Friend类不是你的朋友。那么什么都不是你的朋友。每种语言的每一项功能都可能被误用和滥用。仅仅因为白痴误用了该功能并不意味着我永远不应该将其用于正确的目的。同意,但有些功能比其他功能更容易被滥用。我经常看到Friend函数被用来规避封装这是解决这个问题的真正的面向对象解决方案。继承的存在是有原因的!作为对-1的回应…我在“理论”中知道你应该测试公共接口。然而,在开发周期的实践中,这可能是一个非常宝贵的技巧。我的建议…明智地使用它。
#define protected public
 #define private   public
    #include <ClassUnderTest.hpp>
 #undef protected
#undef private