QxCppUnit Library Version 0.9.2
QxCppUnit Library: Examples

Examples

The demo program shipped with the QxCppUnit library can be used as the starting point for writing a GUI test runner for CppUnit. It has some CppUnit test fixtures with simple test cases which succeed or fail and uses a TestRunner instance in the main program to launch the GUI.

A CppUnit Test Example

This is one of the test fixtures of the demo program. Of course this is a very simple test and only used for demonstration purposes. It isn't intended to show how to write CppUnit tests.

00001 // This class implements a set of CppUnit example test cases which
00002 // succeed or fail. It isn't intended to show how to write CppUnit tests.
00003 
00004 #include <cppunit/TestFixture.h>
00005 #include <cppunit/extensions/HelperMacros.h>
00006 
00007 class TestExamples2;
00008 
00009 CPPUNIT_TEST_SUITE_REGISTRATION(TestExamples2);
00010 
00011 class TestExamples2 : public CPPUNIT_NS::TestFixture
00012 {
00013     CPPUNIT_TEST_SUITE(TestExamples2);
00014 
00015     CPPUNIT_TEST(testFail);
00016     CPPUNIT_TEST(testAssertAssertionPass);
00017     CPPUNIT_TEST(testAssertAssertionFail);
00018     CPPUNIT_TEST(testUnhandledError);
00019 
00020     CPPUNIT_TEST_SUITE_END();
00021 
00022     void testFail()
00023     {
00024         CPPUNIT_FAIL("Intentionally failed");
00025     }
00026 
00027     void testAssertAssertionPass()
00028     {
00029         CPPUNIT_ASSERT_ASSERTION_PASS(CPPUNIT_ASSERT(1 == 1));
00030     }
00031 
00032     void testAssertAssertionFail()
00033     {
00034         CPPUNIT_ASSERT_ASSERTION_FAIL(CPPUNIT_ASSERT(1 == 2));
00035     }
00036 
00037     void testUnhandledError()
00038     {
00039         throw "Unhandled error";
00040     }
00041 };

The Demo Main Program

The main program is straightforward:

00001 #include <QApplication>
00002 #include <qxcppunit/testrunner.h>
00003 #include <cppunit/extensions/TestFactoryRegistry.h>
00004 
00005 int main(int argc, char *argv[])
00006 {
00007     QApplication app(argc, argv);
00008 
00009     QxCppUnit::TestRunner runner;
00010 
00011     runner.addTest(CPPUNIT_NS::TestFactoryRegistry::getRegistry().makeTest());
00012     runner.run();
00013 
00014     return 0;
00015 }