如何通过在运行时选择单元测试来运行 CPPUnit 中的单元测试子集?
我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可能在 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.
如果您使用 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).
另一种方法:
An alternative approach: