在 CppUnit 中指定测试依赖项?

发布于 2024-09-05 22:26:21 字数 701 浏览 8 评论 0原文

我想指定 CppUnit 中的测试顺序。根据我的研究,测试顺序取决于编译器或链接器以及它们如何遇到文件。

如何在 CppUnit 中指定依赖关系?

例如,让我们考虑一个有四条线的矩形类。每条线包含两个点类。假设每个类位于单独的模块或翻译单元中。

struct Point
{
  int x;
  int y;
};

struct Line
{
  Point a;
  Point b;
};

struct Rectangle
{
  Line top;
  Line left;
  Line right;
  Line bottom;
};

在上面的代码中,应该首先测试Point类,然后测试Line类,最后测试Rectangle类。如果 Line 或 Point 类有问题,则没有理由测试 Rectangle 类。 这是一个非常简化的示例。

对于复合类,应首先测试内部类或成员数据类型类。

让我们假设每个类都有一个关联的测试类。每个测试类都有自己发布的测试方法(注册到 CppUnit 列表),位于单独的文件中。用于测试 Lines 的类不知道用于测试点的类;矩形也类似。当这些测试用例类被编译时,它们的顺序取决于编译器和链接器。

那么,如何对测试用例进行排序呢?

仅供参考,我正在使用 CppUnit、wxTestRunner 和 Visual Studio 2008

I would like to specify the order of testing in CppUnit. According to my research, the testing order depends on either the compiler or linker and how they came across the files.

How does one specify dependencies in CppUnit?

For example, let us consider a rectangle class that has four lines. Each line contains two point classes. Assume that each class is in a separate module or translation unit.

struct Point
{
  int x;
  int y;
};

struct Line
{
  Point a;
  Point b;
};

struct Rectangle
{
  Line top;
  Line left;
  Line right;
  Line bottom;
};

In the above code, the Point class should be tested first, then the Line class and finally the Rectangle class. There is no reason to test the Rectangle class if the Line or Point classes have problems. This is a very simplified example.

For composite classes, the inner classes or member data type classes should be test first.

Let us assume that each class has an associated testing class. Each test class has its own published test methods (which are registered to the CppUnit list), in separate files. The class for testing Lines has no knowledge of the testing class for points; and similar for the rectangle. When these test case classes are compiled, their order is dependent on the compiler and linker.

So, how does one order the test cases?

FYI, I am using CppUnit, wxTestRunner and Visual Studio 2008

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

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

发布评论

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

评论(1

美人如玉 2024-09-12 22:26:21

您想要做的并不是真正的单元测试。 “纯”单元测试旨在测试单个单元(单个类),使用模拟或假对象代替真正的依赖项;一旦您测试类之间的依赖关系,那就是集成测试,而不是单元测试。

有了该免责声明......

看起来您也许可以使用 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION 创建多个套件,然后按顺序运行每个套件,前提是所有先前的套件都已通过,但您可能需要破解或替换 wxTestRunner 测试运行程序才能执行此操作。

CppUnit 的 创建 TestSuite 页面还有其他用于注册测试套件的选项;例如,CPPUNIT_REGISTRY_ADD 允许您创建套件层次结构,其中应该让您对顺序有一定的控制,但我看不出有任何方法可以使一个套件中的失败中止后续测试。

最后,作为一个建议,CppUnit 可能不是当今最好的 C++ 单元测试框架。我个人是 Google 测试 的粉丝,但是 Boost.TestUnitTest++ 也不错。 (这个答案介绍了一个名为 Saru 的个人项目,听起来它可能会给你您想要订购测试的灵活性。)

What you're trying to do isn't really unit testing. "Pure" unit testing is intended to test individual units (individual classes), using mocks or fake objects in the place of real dependencies; once you're testing classes' dependencies on each other, that's integration testing, not unit testing.

With that disclaimer out of the way...

It looks like you might be able to use CPPUNIT_TEST_SUITE_NAMED_REGISTRATION to create multiple suites then run each suite in order, only if all previous suites have passed, but you might need to hack up or replace wxTestRunner test runner to do this.

CppUnit's page on Creating TestSuite has other options for registering test suites; CPPUNIT_REGISTRY_ADD, for example, lets you create a hierarchy of suites, which should give you some control over the ordering, but I don't see any way for a failure in one suite to abort subsequent tests.

Finally, just as a suggestion, CppUnit is probably not the best C++ unit testing framework these days. I'm personally a fan of Google Test, but Boost.Test and UnitTest++ are also good. (This answer introduces a personal project called Saru that sounds like it might give you the flexibility you want of ordering tests.)

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