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

发布于 2024-07-28 22:46:44 字数 147 浏览 6 评论 0原文

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

在 Java 中,我要么在与被测类相同的包中创建测试类,要么创建一个匿名子类来公开我的测试类中所需的方法,但这两种方法在 C++ 中都不可用。

我正在使用 NUnit 测试非托管 C++ 类。

How do I unit test a protected method in C++?

In Java, I'd either create the test class in the same package as the class under test or create an anonymous subclass that exposes the method I need in my test class, but neither of those methods are available to me in C++.

I am testing an unmanaged C++ class using NUnit.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

抽个烟儿 2024-08-04 22:46:44

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

在测试代码中,定义被测类的派生类(直接定义或从其派生类之一定义)。 为受保护的成员添加访问器,或在派生类中执行测试。 在 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);

Assuming you mean a protected method of a publicly-accessible class:

In the test code, define a derived class of the class under test (either directly, or from one of its derived classes). Add accessors for the protected members, or perform tests within your derived class . "protected" access control really isn't very scary in C++: it requires no co-operation from the base class to "crack into" it. So it's best not to introduce any "test code" into the base class, not even a friend declaration:

// 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);
梦毁影碎の 2024-08-04 22:46:44

您还可以使用 using 关键字来公开公共块(使用 。

// in realclass.h
class RealClass {
    protected:
    int foo(int a) { return a+1; }
    int foo(string a) { return a.length(); } // Overload works too
    template<class T> int foo(const T& a) { return 1; } // Templates work too
};

// in test code
#include "realclass.h"
class RealClassExposed : public RealClass {
    public:
        using RealClass::foo;
};

RealClassExposed blah;
assert(blah.foo(1) == 2);
assert(blah.foo("test") == 4);
assert(blah.foo(blah) == 1);

请参阅:http://en. cppreference.com/w/cpp/language/using_declaration

You can also use using keyword to expose public block (using .

// in realclass.h
class RealClass {
    protected:
    int foo(int a) { return a+1; }
    int foo(string a) { return a.length(); } // Overload works too
    template<class T> int foo(const T& a) { return 1; } // Templates work too
};

// in test code
#include "realclass.h"
class RealClassExposed : public RealClass {
    public:
        using RealClass::foo;
};

RealClassExposed blah;
assert(blah.foo(1) == 2);
assert(blah.foo("test") == 4);
assert(blah.foo(blah) == 1);

See: http://en.cppreference.com/w/cpp/language/using_declaration

薔薇婲 2024-08-04 22:46:44

我使用 CxxTest 并让 CxxTest 从包含受保护成员函数的类派生。 如果您仍在寻找自己喜欢的 C++ 单元测试框架,请查看这篇文章

I use CxxTest and have the CxxTest derive from the class that contains the protected member function. If you're still searching around for your favorite C++ Unit Testing framework, take a look at this article.

メ斷腸人バ 2024-08-04 22:46:44

声明一个友元类MyClass_UnitTest; 在你的 MyClass 中。 然后,您可以在单元测试程序中的其他位置定义 MyClass_UnitTest,该程序可以完全访问 MyClass 内部,但不必在发布应用程序中提供实现。
有关如何完成此操作的一个很好的示例,请参阅 CppUnit 文档。

Declare a friend class MyClass_UnitTest; in your MyClass. You can then define MyClass_UnitTest elsewhere in your unit test program that has full access to MyClass internals, but you don't have to provide an implementation in your release application.
See CppUnit documentation for a good example of how this is done.

只涨不跌 2024-08-04 22:46:44

C++ 中有一个使用 #define 的简单解决方案。 只需像这样包含“ClassUnderTest”:

#define protected public
 #define private   public
    #include <ClassUnderTest.hpp>
 #undef protected
#undef private

感谢本文和 RonFox

There's a simple solution in C++ using #define. Just wrap the include of your "ClassUnderTest" like this:

#define protected public
 #define private   public
    #include <ClassUnderTest.hpp>
 #undef protected
#undef private

Credit goes to this article and RonFox

唐婉 2024-08-04 22:46:44

考虑一个公共的、可能是静态的“单元测试”函数。

丑陋,但比我能想到的使用宏或朋友之类的替代方案更好。

Consider a public, possibly static 'unit test' function.

Ugly, but better than the alternatives I can think of using macros or friends or such.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文