问题
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?
回答1:
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
.
回答2:
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.
回答3:
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.
来源:https://stackoverflow.com/questions/8482729/cppunit-run-a-single-test-case