UnitTest.h File Reference

Back to the index.

Classes | Macros
UnitTest.h File Reference
#include "misc.h"
#include <exception>
#include <iostream>

Go to the source code of this file.

Classes

class  UnitTestFailedException
 An exception thrown by unit test cases that fail. More...
 
class  UnitTestable
 Base class for unit testable classes. More...
 
class  UnitTest
 A collection of helper functions, for writing simple unit tests. More...
 

Macros

#define UNITTESTS(class)   void class::RunUnitTests(int& nSucceeded, int& nFailures)
 Helper for unit test case execution. More...
 
#define UNITTEST(functionname)
 Helper for unit test case execution. More...
 

Macro Definition Documentation

◆ UNITTEST

#define UNITTEST (   functionname)
Value:
try { \
std::cout << "### " #functionname "\n"; \
(functionname)(); \
++ (nSucceeded); \
} catch (UnitTestFailedException& ex) { \
std::cout.flush(); \
std::cerr << "\n### " #functionname " (" __FILE__ " line " \
<< __LINE__ << ") failed!\n" \
" > " << ex.GetMessage() << "\n"; \
std::cerr.flush(); \
++ (nFailures); \
}

Helper for unit test case execution.

For each test case that throws a UnitTestFailedException, the number of failures is increased. For test cases that don't fail, the number of successful test cases is increased instead.

Usage: (usually at the end of a class implementation file)

     #ifdef WITHUNITTESTS
     static void MyClass::Test_MyClass_SomeTest()
     {
        UnitTest::Assert("expected blah blah", bool_condition);
        ... more asserts here ...
     }
     ...
     UNITTESTS(MyClass)
     {
        UNITTEST(Test_MyClass_SomeTest);
        UNITTEST(Test_MyClass_AnotherTest);
        ... more test cases here ...
     }
     #endif // WITHUNITTESTS

Note that MyClass (in the example above) should inherit from the UnitTestable class.

Definition at line 218 of file UnitTest.h.

◆ UNITTESTS

#define UNITTESTS (   class)    void class::RunUnitTests(int& nSucceeded, int& nFailures)

Helper for unit test case execution.

The main purpose, appart from making the code look more compact, of having a macro for this is that the configure script finds all .cc files that use this macro, and generates lists of header files to include and lists of RunUnitTests functions to call. These are then included and run from UnitTest::RunTests.

See the comment for UNITTEST for details on how to use it.

Definition at line 185 of file UnitTest.h.

UnitTestFailedException::GetMessage
const string & GetMessage() const
Retrieves the error message associated with the exception.
Definition: UnitTest.h:57
UnitTestFailedException
An exception thrown by unit test cases that fail.
Definition: UnitTest.h:41

Generated on Tue Aug 25 2020 19:25:06 for GXemul by doxygen 1.8.18