3 // $Id: cpptest-suite.h,v 1.4 2010/03/26 04:45:14 hartwork Exp $
5 // CppTest - A C++ Unit Testing Framework
6 // Copyright (c) 2003 Niklas Lundell
10 // This library is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU Lesser General Public
12 // License as published by the Free Software Foundation; either
13 // version 2 of the License, or (at your option) any later version.
15 // This library is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // Lesser General Public License for more details.
20 // You should have received a copy of the GNU Lesser General Public
21 // License along with this library; if not, write to the
22 // Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 // Boston, MA 02111-1307, USA.
29 #ifndef CPPTEST_SUITE_H
30 #define CPPTEST_SUITE_H
36 #include "cpptest-time.h"
37 #include "cpptest-source.h"
43 /// \brief Unit testing suite.
45 /// Base class for all suites. Derive from this class to create own
48 /// %Test functions in derived classes are registered as tests using the
49 /// TEST_ADD(func). Testing is started by run(). Note that suites may be
50 /// embedded in other suites using add().
58 void add(std::auto_ptr
<Suite
> suite
);
60 bool run(Output
& output
, bool cont_after_fail
= true);
63 /// Pointer to a test function.
65 typedef void (Suite::*Func
)();
67 bool continue_after_failure() const { return _continue
; }
69 virtual void setup() {}
70 virtual void tear_down() {}
72 void register_test(Func func
, const std::string
& name
);
73 void assertment(Source s
);
83 friend struct ExecTests
;
84 friend struct SubSuiteTests
;
85 friend struct SubSuiteTime
;
93 Data(Func func
, const std::string
& name
)
94 : _func(func
), _name(name
) {}
97 typedef std::list
<Data
> Tests
;
98 typedef std::list
<Suite
*> Suites
;
100 std::string _name
; // Suite name
101 const std::string
* _cur_test
; // Current test func name
102 Suites _suites
; // External test suites
103 Tests _tests
; // All tests
104 Output
* _output
; // Output handler
105 bool _result
: 1; // Test result
106 bool _success
: 1; // Set if no test failed
107 bool _continue
: 1; // Continue func after failures
109 void do_run(Output
* os
, bool cont_after_fail
);
110 int total_tests() const;
111 Time
total_time(bool recursive
) const;
116 Suite
& operator=(const Suite
&);
119 /// Adds a test function to the enclosing suite. Note that test functions
120 /// should be added in the suites constructor.
122 /// \param func Function to add, must be of type Suite::Func.
126 /// MySuite::MySuite()
128 /// TEST_ADD(&MySuite::test_1)
129 /// TEST_ADD(&MySuite::test_2)
134 #define TEST_ADD(func)\
135 register_test(static_cast<Func>(&func), #func);
139 #endif // #ifndef CPPTEST_SUITE_H