CppUnit:运行单个测试用例

发布于 2024-12-21 08:44:47 字数 341 浏览 2 评论 0原文

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 技术交流群。

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

发布评论

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

评论(4

彩虹直至黑白 2024-12-28 08:44:47

我猜你是在要求 CppUnit 的 SSCCE。由于 CppUnit 是一个框架,因此一个最小的示例必须放置最小的测试结构 - 就像 TestFixture,因为否则可以不需要整个 CppUnit,而只使用 std::assert。所有这些都可以在一个文件中完成,例如以下形式的 Main.cpp

//Declaration file: MTest.h
#ifndef MTEST_H
#define MTEST_H
#include <cppunit/extensions/HelperMacros.h>

class MTest : public CPPUNIT_NS::TestFixture
{
  CPPUNIT_TEST_SUITE(MTest);
  CPPUNIT_TEST(simpleTest);
  CPPUNIT_TEST_SUITE_END();
public:
  void simpleTest();
};
#endif  // MTEST_H

//////////////////////////////////////
// Implementation file, e.g. MTest.cpp
#include <cppunit/config/SourcePrefix.h>
//#include "MTest.h"

// Registers the fixture into the 'registry'
CPPUNIT_TEST_SUITE_REGISTRATION(MTest);

// Some code to be tested.
void MTest::simpleTest() {
  CPPUNIT_ASSERT_EQUAL(1, 2);
}

/////////////////////////////////////
// Main file, Main.cpp
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <cppunit/ui/text/TestRunner.h>

int main(int argc, char* argv[])
{
  CPPUNIT_NS::TextUi::TestRunner runner;   //the runner
  // Get the top level suite from the registry
  CPPUNIT_NS::Test* suite = 
      CPPUNIT_NS::TestFactoryRegistry::getRegistry().makeTest();
  // Adds the test to the list of test to run
  runner.addTest(suite);  
  // Run the test.
  bool wasSucessful = runner.run();
  // Return error code 1 if the one of test failed.
  return wasSucessful ? 0 : 1;
}

需要使用 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, say Main.cpp of the following form:

//Declaration file: MTest.h
#ifndef MTEST_H
#define MTEST_H
#include <cppunit/extensions/HelperMacros.h>

class MTest : public CPPUNIT_NS::TestFixture
{
  CPPUNIT_TEST_SUITE(MTest);
  CPPUNIT_TEST(simpleTest);
  CPPUNIT_TEST_SUITE_END();
public:
  void simpleTest();
};
#endif  // MTEST_H

//////////////////////////////////////
// Implementation file, e.g. MTest.cpp
#include <cppunit/config/SourcePrefix.h>
//#include "MTest.h"

// Registers the fixture into the 'registry'
CPPUNIT_TEST_SUITE_REGISTRATION(MTest);

// Some code to be tested.
void MTest::simpleTest() {
  CPPUNIT_ASSERT_EQUAL(1, 2);
}

/////////////////////////////////////
// Main file, Main.cpp
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <cppunit/ui/text/TestRunner.h>

int main(int argc, char* argv[])
{
  CPPUNIT_NS::TextUi::TestRunner runner;   //the runner
  // Get the top level suite from the registry
  CPPUNIT_NS::Test* suite = 
      CPPUNIT_NS::TestFactoryRegistry::getRegistry().makeTest();
  // Adds the test to the list of test to run
  runner.addTest(suite);  
  // Run the test.
  bool wasSucessful = runner.run();
  // Return error code 1 if the one of test failed.
  return wasSucessful ? 0 : 1;
}

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 of libcppunit library as is required by your environment]).

A "cleaner" examply would split the code into separate MTest (.h and .cpp, as indicated) and Main.cpp. In this case CppUnit methods from Main.cpp call methods provided by CppUnit helper macros in MTest files. They should therefore be linked together, e.g. by g++ MTest.o Main.o ../../src/cppunit/.libs/libcppunit.a.

何以笙箫默 2024-12-28 08:44:47

所有测试类的基类是CppUnit::TestFixture,您可以重写一些函数,例如setUptearDown来初始化测试对象并删除它们。

假设您有一个名为 MyFirstTest 的测试类,要向 Cpp 框架注册测试函数,您必须执行以下操作:

CPPUNIT_TEST_SUITE(MyFirstTest);
CPPUNIT_TEST(myTestFunction);
... //any other function you want to register with appropriate macros
CPPUNIT_TEST_SUITE_END();

此外,您还必须注册每个测试类(在其各自的头文件或 cpp 文件中)

CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(MyFirstTest, "YouTestName");

。测试类已设置,您可以运行它。主要功能将如下所示:

bool wasSuccessful = false;

    try
    {
        CppUnit::TextUi::TestRunner runner;
        runner.setOutputter( new CppUnit::CompilerOutputter(&runner.result(), std::cerr));
        CppUnit::TestFactoryRegistry ®istry = CppUnit::TestFactoryRegistry::getRegistry("YouTestName");
        runner.addTest(registry.makeTest());
        wasSuccessful = runner.run("", false);
    }
    catch(const std::exception& e)
    {
        std::cerr << e.what() << std::endl;
        wasSuccessful = false;
    }

如果您希望添加更多测试类,主要功能将保持不变。您只需创建测试类(从 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 like setUp and tearDown 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:

CPPUNIT_TEST_SUITE(MyFirstTest);
CPPUNIT_TEST(myTestFunction);
... //any other function you want to register with appropriate macros
CPPUNIT_TEST_SUITE_END();

Also you will have to register each test class (in their respective header or cpp file)

CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(MyFirstTest, "YouTestName");

Once your test class is setup, you can run it. Main function will look like:

bool wasSuccessful = false;

    try
    {
        CppUnit::TextUi::TestRunner runner;
        runner.setOutputter( new CppUnit::CompilerOutputter(&runner.result(), std::cerr));
        CppUnit::TestFactoryRegistry ®istry = CppUnit::TestFactoryRegistry::getRegistry("YouTestName");
        runner.addTest(registry.makeTest());
        wasSuccessful = runner.run("", false);
    }
    catch(const std::exception& e)
    {
        std::cerr << e.what() << std::endl;
        wasSuccessful = false;
    }

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 using CPPUNIT_TEST_SUITE_NAMED_REGISTRATION. The getRegistry method that is used in main 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 using CPPUNIT_TEST or any other appropriate macro.

溺渁∝ 2024-12-28 08:44:47

您所指的页面描述了整个过程,包括许多额外的内容,包括如何在 TestFixtures 中手动编写代码,然后如何在 TestSuites 中注册这些代码,然后如何使用宏来编写并注册它们,这很罗嗦。有时,向人们展示一个简单的例子会更好。他们在页面的最底部有一个:

#include <cppunit/extensions/TestFactoryRegistry.h>
#include <cppunit/ui/text/TestRunner.h>

int main( int argc, char **argv)
{
  CppUnit::TextUi::TestRunner runner;
  CppUnit::TestFactoryRegistry ®istry = CppUnit::TestFactoryRegistry::getRegistry();
  runner.addTest( registry.makeTest() );
  bool wasSuccessful = runner.run( "", false );
  return wasSuccessful;
}

基础设施非常简单,真的。您创建一个测试运行程序,然后检索已注册测试的列表,将它们添加到运行程序中,让运行程序运行测试,并向您报告。但是,是的,让事情变得简单总是最好的。人们不想做困难的事情。

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:

#include <cppunit/extensions/TestFactoryRegistry.h>
#include <cppunit/ui/text/TestRunner.h>

int main( int argc, char **argv)
{
  CppUnit::TextUi::TestRunner runner;
  CppUnit::TestFactoryRegistry ®istry = CppUnit::TestFactoryRegistry::getRegistry();
  runner.addTest( registry.makeTest() );
  bool wasSuccessful = runner.run( "", false );
  return wasSuccessful;
}

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.

ˉ厌 2024-12-28 08:44:47

简单但愚蠢的解决方案:注释掉您不喜欢的 CPPUNIT_TEST 行。

Simple but dumb solution: comment out the CPPUNIT_TEST lines you do not like.

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