CppUnit:运行单个测试用例
在 http://cppunit.sourceforge.net/doc/lastest/cppunit_cookbook.html
他们给出了一个简单的测试用例,但没有展示如何运行它(没有 main
函数)。我浏览了他们的文档,但找不到如何运行测试并获取有关测试是否成功的文本输出。我不想组装固定装置或使用注册表或任何东西。
我如何运行该单个测试用例? IE 与之相关的 main
函数是什么?
In http://cppunit.sourceforge.net/doc/lastest/cppunit_cookbook.html
They give a simple TestCase but do not show how to run it (There is no main
function). I've looked through their documentation and can't find how to just run a test and get text output about whether or not it succeeded. I don't want to put together a fixture or use a registry or anything.
How do I run that single test case? I.E. What is the main
function that would go along with that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我猜你是在要求 CppUnit 的 SSCCE。由于 CppUnit 是一个框架,因此一个最小的示例必须放置最小的测试结构 - 就像 TestFixture,因为否则可以不需要整个 CppUnit,而只使用 std::assert。所有这些都可以在一个文件中完成,例如以下形式的
Main.cpp
:需要使用
cppunit
库进行编译/链接,例如g++ Main。 cpp ../../src/cppunit/.libs/libcppunit.a
(如果您碰巧在库主目录以下的 2 级启动[插入libcppunit您的环境需要的库])。
例如,“清理器”会将代码拆分为单独的
MTest
(.h
和.cpp
,如所示)和Main.cpp
。在本例中,Main.cpp
中的 CppUnit 方法调用由MTest
文件中的 CppUnit 辅助宏提供的方法。因此,它们应该链接在一起,例如通过g++ MTest.o Main.o ../../src/cppunit/.libs/libcppunit.a
。I gather you were asking for a SSCCE of CppUnit. As CppUnit is a framework, so a minimal example must put minimal test structure in place -- like a TestFixture, because otherwise one could do without the whole CppUnit and just use
std::assert
. All this can be done in one file, sayMain.cpp
of the following form:which would need to be compiled/linked with
cppunit
library e.g.g++ Main.cpp ../../src/cppunit/.libs/libcppunit.a
(if you happen to start 2 levels below the main directory of the library [insert the static or dynamic version oflibcppunit
library as is required by your environment]).A "cleaner" examply would split the code into separate
MTest
(.h
and.cpp
, as indicated) andMain.cpp
. In this case CppUnit methods fromMain.cpp
call methods provided by CppUnit helper macros inMTest
files. They should therefore be linked together, e.g. byg++ MTest.o Main.o ../../src/cppunit/.libs/libcppunit.a
.所有测试类的基类是
CppUnit::TestFixture
,您可以重写一些函数,例如setUp
和tearDown
来初始化测试对象并删除它们。假设您有一个名为
MyFirstTest
的测试类,要向 Cpp 框架注册测试函数,您必须执行以下操作:此外,您还必须注册每个测试类(在其各自的头文件或 cpp 文件中)
。测试类已设置,您可以运行它。主要功能将如下所示:
如果您希望添加更多测试类,主要功能将保持不变。您只需创建测试类(从
CppUnit::TestFixture
类派生),注册您的方法,重要的一步是使用CPPUNIT_TEST_SUITE_NAMED_REGISTRATION
向框架注册您的类。main
函数中使用的getRegistry
方法将获取您在框架中注册的所有测试类,并执行您已注册的这些类的所有方法CPPUNIT_TEST
或任何其他适当的宏。The base class for all your test class is
CppUnit::TestFixture
, you can override some function likesetUp
andtearDown
to initialize you test objects and delete them.Consider you have a test class called
MyFirstTest
, to register the test functions with Cpp framework you will have to do:Also you will have to register each test class (in their respective header or cpp file)
Once your test class is setup, you can run it. Main function will look like:
If you wish to add more test classes, the main function will remain the same. You just create test class (deriving from that
CppUnit::TestFixture
class), register your methods and the the important step is to register you class with framework usingCPPUNIT_TEST_SUITE_NAMED_REGISTRATION
. ThegetRegistry
method that is used inmain
function, will get all the test classes that you have registered with the framwork and executes all the methods of those classes that you have registered usingCPPUNIT_TEST
or any other appropriate macro.您所指的页面描述了整个过程,包括许多额外的内容,包括如何在 TestFixtures 中手动编写代码,然后如何在 TestSuites 中注册这些代码,然后如何使用宏来编写并注册它们,这很罗嗦。有时,向人们展示一个简单的例子会更好。他们在页面的最底部有一个:
基础设施非常简单,真的。您创建一个测试运行程序,然后检索已注册测试的列表,将它们添加到运行程序中,让运行程序运行测试,并向您报告。但是,是的,让事情变得简单总是最好的。人们不想做困难的事情。
The page you're referring to describes the whole process, including a lot of extra stuff on how you'd manually write the code in TestFixtures, then how you'd register those in TestSuites, then how you'd use the macros to write and register them, and it's very wordy. Sometimes it's better just to show people an easy example. They have this one at the very bottom of the page:
The infrastructure is pretty simple, really. You create a test runner, then retrieve the list of registered tests, add them to the runner, have the runner run the tests, and report back to you. But yeah, it's always best to make things easy. People don't want to do hard things.
简单但愚蠢的解决方案:注释掉您不喜欢的 CPPUNIT_TEST 行。
Simple but dumb solution: comment out the CPPUNIT_TEST lines you do not like.