summaryrefslogtreecommitdiff
path: root/include/cppunit/TestCase.h
blob: dc85e7a9a5c4fedb6f0ff339810c9232c04c2fa5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#ifndef CPPUNIT_TESTCASE_H
#define CPPUNIT_TESTCASE_H

#include <string>
#include <cppunit/Test.h>
#include <cppunit/TestAssert.h>

namespace CppUnit {

  class TestResult;

  /**
   * A test case defines the fixture to run multiple tests. 
   * To define a test case
   * do the following:
   * - implement a subclass of TestCase 
   * - the fixture is defined by instance variables 
   * - initialize the fixture state by overriding setUp
   *   (i.e. construct the instance variables of the fixture)
   * - clean-up after a test by overriding tearDown.
   *
   * Each test runs in its own fixture so there
   * can be no side effects among test runs.
   * Here is an example:
   * 
   * \code
   * class MathTest : public TestCase {
   *     protected: int m_value1;
   *     protected: int m_value2;
   *
   *     public: MathTest (string name)
   *                 : TestCase (name) {
   *     }
   *
   *     protected: void setUp () {
   *         m_value1 = 2;
   *         m_value2 = 3;
   *     }
   * }
   * \endcode
   *
   * For each test implement a method which interacts
   * with the fixture. Verify the expected results with assertions specified
   * by calling CPPUNIT_ASSERT on the expression you want to test:
   * 
   * \code
   *    protected: void testAdd () {
   *        int result = value1 + value2;
   *        CPPUNIT_ASSERT (result == 5);
   *    }
   * \endcode
   * 
   * Once the methods are defined you can run them. To do this, use
   * a TestCaller.
   *
   * \code
   * Test *test = new TestCaller<MathTest>("testAdd", MathTest::testAdd);
   * test->run ();
   * \endcode
   *
   *
   * The tests to be run can be collected into a TestSuite. 
   * 
   * \code
   * public: static TestSuite *MathTest::suite () {
   *      TestSuite *suiteOfTests = new TestSuite;
   *      suiteOfTests->addTest(new TestCaller<MathTest>(
   *                              "testAdd", testAdd));
   *      suiteOfTests->addTest(new TestCaller<MathTest>(
   *                              "testDivideByZero", testDivideByZero));
   *      return suiteOfTests;
   *  }
   * \endcode
   * 
   *
   * \see TestResult
   * \see TestSuite 
   * \see TestCaller
   *
   */

  class TestCase : public Test
  {
    public:
      TestCase         ();
      TestCase         (std::string Name);
      ~TestCase        ();

      virtual void        run              (TestResult *result);
      virtual TestResult  *run             ();
      virtual int         countTestCases   () const;
      std::string         getName          () const;
      std::string         toString         () const;

      virtual void        setUp            ();
      virtual void        tearDown         ();

    protected:
      virtual void        runTest          ();
    
      TestResult          *defaultResult   ();
    
    private:
      TestCase (const TestCase& other); 
      TestCase& operator= (const TestCase& other); 

    private:
      const std::string   m_name;
  };

} // namespace CppUnit

#endif // CPPUNIT_TESTCASE_H