如何通过在运行时选择单元测试来运行 CPPUnit 中的单元测试子集?

发布于 2024-08-31 18:29:13 字数 84 浏览 4 评论 0原文

我使用 CppUnit 作为单元测试框架。是否可以选择测试用例的子集在运行时执行?

CppUnit 中是否提供了过滤选项来适应这种情况?

I am using CppUnit as a unit test framework. Is it possible to select a subset of testcases to execute at runtime?

Is there a filtering option provided within CppUnit to accomodate this?

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

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

发布评论

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

评论(3

执手闯天涯 2024-09-07 18:29:13

您可能在 main() 中调用的 TestRunner::run() 方法实际上具有可选参数: run(std::string testName = "", bool doWait = false, bool doPrintResult = true, bool doPrintProgress = true)。 testName 必须是测试的具体名称。如果需要,您可以按姓名请求特定测试。您还可以对特定测试调用 runTest(Test*) 或 runTestByName(testName)。

但听起来你想变得更复杂。假设您使用 CPPUNIT_TEST_SUITE_REGISTRATION() 宏注册了所有测试,静态 TestFactoryRegistry::makeTest() 方法将返回所有已注册测试的 TestSuite。

TestSuite 对象通过 getTests() 方法生成一个向量。您可以迭代这些,将它们的名称与正则表达式(或通过索引号或您想要的任何方式)进行匹配,而不是像大多数人那样在整个套件上调用 TestRunner::addTest(registry.makeTest()) ,您只需添加您要求的具体测试。

您必须编写一些东西来迭代测试并进行匹配,但除此之外它应该非常简单。可能有十几行代码,再加上解析命令行参数。使用正则表达式可以让自己变得更简单。

The TestRunner::run() method you are likely calling in your main() actually has optional parameters: run(std::string testName = "", bool doWait = false, bool doPrintResult = true, bool doPrintProgress = true). testName must be the specific name of a test. You could request a specific test by name if you wanted. You can also call runTest(Test*) on a specific test, or runTestByName(testName).

But it sounds like you want to get more sophisticated. Assuming you registered all your tests with the CPPUNIT_TEST_SUITE_REGISTRATION() macros, the static TestFactoryRegistry::makeTest() method will return a TestSuite of all registered tests.

The TestSuite object yields a vector via the getTests() method. You could iterate through those, matching their names against a regexp (or by index number or however you want) and instead of calling TestRunner::addTest(registry.makeTest()) on the whole suite like most people do, you just add the specific tests you're requesting.

You'll have to write something to iterate through the tests and do the matching, but other than that it should be dead simple. Probably a dozen lines of code, plus parsing the command line arguments. Use regex to keep it simpler for yourself.

箜明 2024-09-07 18:29:13

如果您使用 cppunit 的 GUI 测试运行器 您可以只检查要运行的测试。

如果您无法使用 GUI 测试运行程序,请查看 post - 它描述了一种“可配置”的方式来定义基于 xml 文档运行哪些测试(上一篇文章或多或少地描述了我最终的解决方案)。

If you are using the GUI test runner for cppunit you can just check the tests you want to run.

If you cannot use the GUI test runner, check out this post - it describes an "configurable" way of defining which tests to run based on a xml document (the last post describes more or less the solution I had in the end).

沉鱼一梦 2024-09-07 18:29:13

另一种方法:

// find the unit test as specified by the one argument to this program
CPPUNIT_NS::Test *suite = CPPUNIT_NS::TestFactoryRegistry::getRegistry().makeTest();
int iTestIndex = 0;
for (; iTestIndex < suite->getChildTestCount(); ++iTestIndex)
{
    fprintf(stderr, "INFO: Looking for a match between '%s' and '%s'\n",
            suite->getChildTestAt(iTestIndex)->getName().c_str(),
            argv[1]);
    if (suite->getChildTestAt(iTestIndex)->getName() == std::string(argv[1]))
    {
        fprintf(stderr, "INFO: Found a match for '%s' and '%s'\n",
                suite->getChildTestAt(iTestIndex)->getName().c_str(),
                argv[1]);
        break;
    }
}
if (iTestIndex >= suite->getChildTestCount())
{
    fprintf(stderr, "ERROR: Did NOT find test '%s'!\n", argv[1]);
    return -1;
}

An alternative approach:

// find the unit test as specified by the one argument to this program
CPPUNIT_NS::Test *suite = CPPUNIT_NS::TestFactoryRegistry::getRegistry().makeTest();
int iTestIndex = 0;
for (; iTestIndex < suite->getChildTestCount(); ++iTestIndex)
{
    fprintf(stderr, "INFO: Looking for a match between '%s' and '%s'\n",
            suite->getChildTestAt(iTestIndex)->getName().c_str(),
            argv[1]);
    if (suite->getChildTestAt(iTestIndex)->getName() == std::string(argv[1]))
    {
        fprintf(stderr, "INFO: Found a match for '%s' and '%s'\n",
                suite->getChildTestAt(iTestIndex)->getName().c_str(),
                argv[1]);
        break;
    }
}
if (iTestIndex >= suite->getChildTestCount())
{
    fprintf(stderr, "ERROR: Did NOT find test '%s'!\n", argv[1]);
    return -1;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文