Writing a Test

A test is represented by a class. The class has to have a public member function called run(); this function should take no arguments.

The run() function should perform a sequence of actions and do one or more assertions. For example, say that we are testing a stack class. Here is a test that we might write:

    class SinglePush {
    public:
      void run() {
        Stack stack;

        ASSERT(stack.empty());

        stack.push(3);
        ASSERT(!stack.empty());
        ASSERT_EQUALS(3, stack.top());
      }
    };

If one of the assertions fails, the run() function will immediately stop executing, and the test will be marked as having failed. If all of the assertions hold, the test will be marked as having passed.

The macros ASSERT and ASSERT_EQUALS are defined in the header file unittest/UnitTest.hpp.


david carlton <carlton@bactrian.org>