Unit Testing with JrDebugLogger |
Top Previous Next |
This example shows how to use the basic unit testing support functions. You will find this demo project in the DebugLibrary source code directory, along with project files for ms .net2003,vc6, and dev-c++.
//--------------------------------------------------------------------------- // JrDebug demo that demonstrates various features //---------------------------------------------------------------------------
//--------------------------------------------------------------------------- // System includes #include <iostream> //---------------------------------------------------------------------------
//--------------------------------------------------------------------------- // Include the Debugging Library files // Note that only our main .cpp needs to include the JRutils_debugout_main.h file // alternatively, you could add this cpp to your project or makefile. #include "../../jrdebug_main.cpp" //---------------------------------------------------------------------------
//--------------------------------------------------------------------------- // code for the unit tests void MyUnitTest() { dbprintf("my unit test!"); } void MyUnitTest2() { dbprintf("my unit test that should throw an exception."); // two tests, one that shouldnt fail and will abort further testing if it does dbtestex(10<100); // and another test that will fail and generate a message dbtest(100<10); // now throw an exception to show how it will be caught by the testing system throw "mystrexception"; }
class DummyClass { // this demonstrates that you can register static class functions as unit test functions public: static void ClassStaticTest() {dbprintf("in static class member test");}; };
// register the unit tests - this is all you have to do to tell the system about your test functions JrDebugRegisterTestFunction("simpletst","Simple Unit Test",MyUnitTest,"simple"); // note how we can register a (static) member class function as a test function JrDebugRegisterTestFunction("statictest","Static Class Member Test",DummyClass::ClassStaticTest,"advanced"); //---------------------------------------------------------------------------
//--------------------------------------------------------------------------- int main(int argc, char *argv[]) { // announce printf("Demo program, to run unit tests call on commandline with -dbt");
// if you really want to you can register test functions from within code instead of globally // JrDebugRegisterTestFunction("simpletest","Simple Unit Test #2",MyUnitTest2,"advanced");
// normal commandline parsing, and will turn on tests if appropriates JrDebug::GrabCommandlineDebugFile(argc,argv,true); // exit right away if tests run - you might want to add this to your code if you want -dbt to run tests and then exit if (JrDebug::RanTests()) return 0;
// force tests to run manually in this particular demo? if (!JrDebug::RanTests()) { printf("Running tests manually since they weren't invoked on commandline."); // let the debugger run all registered tests JrDebug::RunTests("all"); // or we could manually invoke functions without even registering them // JrDebugRunTestFunction("Manual Test#2",MyUnitTest2); }
// all done return 0; } //---------------------------------------------------------------------------
|