summaryrefslogtreecommitdiffstats
blob: 930370ad970a68293c224619eb0f3426ee6eb889 (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#ifndef CPPUNIT_TESTRUNNER_H
#define CPPUNIT_TESTRUNNER_H

#include <cppunit/TestSuite.h>
#include <string>

CPPUNIT_NS_BEGIN


class Test;
class TestResult;


/*! \brief Generic test runner.
 * \ingroup ExecutingTest
 *
 * The TestRunner assumes ownership of all added tests: you can not add test
 * or suite that are local variable since they can't be deleted.
 *
 * Example of usage:
 * \code
 * #include <cppunit/extensions/TestFactoryRegistry.h>
 * #include <cppunit/CompilerOutputter.h>
 * #include <cppunit/TestResult.h>
 * #include <cppunit/TestResultCollector.h>
 * #include <cppunit/TestRunner.h>
 * #include <cppunit/TextTestProgressListener.h>
 * 
 * 
 * int 
 * main( int argc, char* argv[] )
 * {
 *   std::string testPath = (argc > 1) ? std::string(argv[1]) : "";
 * 
 *   // Create the event manager and test controller
 *   CppUnit::TestResult controller;
 * 
 *   // Add a listener that colllects test result
 *   CppUnit::TestResultCollector result;
 *   controller.addListener( &result );        
 * 
 *   // Add a listener that print dots as test run.
 *   CppUnit::TextTestProgressListener progress;
 *   controller.addListener( &progress );      
 * 
 *   // Add the top suite to the test runner
 *   CppUnit::TestRunner runner;
 *   runner.addTest( CppUnit::TestFactoryRegistry::getRegistry().makeTest() );   
 *   try
 *   {
 *     std::cout << "Running "  <<  testPath;
 *     runner.run( controller, testPath );
 * 
 *     std::cerr << std::endl;
 * 
 *     // Print test in a compiler compatible format.
 *     CppUnit::CompilerOutputter outputter( &result, std::cerr );
 *     outputter.write();                      
 *   }
 *   catch ( std::invalid_argument &e )  // Test path not resolved
 *   {
 *     std::cerr  <<  std::endl  
 *                <<  "ERROR: "  <<  e.what()
 *                << std::endl;
 *     return 0;
 *   }
 * 
 *   return result.wasSuccessful() ? 0 : 1;
 * }
 * \endcode
 */
class CPPUNIT_API TestRunner
{
public:
  /*! \brief Constructs a TestRunner object.
   */
  TestRunner(  );

  /// Destructor.
  virtual ~TestRunner();

  /*! \brief Adds the specified test.
   * \param test Test to add. The TestRunner takes ownership of the test.
   */
  virtual void addTest( Test *test );

  /*! \brief Runs a test using the specified controller.
   * \param controller Event manager and controller used for testing
   * \param testPath Test path string. See Test::resolveTestPath() for detail.
   * \exception std::invalid_argument if no test matching \a testPath is found.
   *                                  see TestPath::TestPath( Test*, const std::string &)
   *                                  for detail.
   */
  virtual void run( TestResult &controller,
                    const std::string &testPath = "" );

protected:
  /*! \brief (INTERNAL) Mutating test suite.
   */
  class CPPUNIT_API WrappingSuite : public TestSuite
  {
  public:
    WrappingSuite( const std::string &name = "All Tests" );

    int getChildTestCount() const;

    std::string getName() const;

    void run( TestResult *result );

  protected:
    Test *doGetChildTestAt( int index ) const;

    bool hasOnlyOneTest() const;

    Test *getUniqueChildTest() const;
  };

protected:
  WrappingSuite *m_suite;

private:
  /// Prevents the use of the copy constructor.
  TestRunner( const TestRunner &copy );

  /// Prevents the use of the copy operator.
  void operator =( const TestRunner &copy );

private:
};


CPPUNIT_NS_END

#endif  // CPPUNIT_TESTRUNNER_H