CppTest home page | CppTest project page |
This tutorial is intended to quickly get you started.
CppTest is released under the GNU Lesser General Public License.
void
and take no parameters. For example:
class ExampleTestSuite : public Test::Suite { public: ExampleTestSuite() { TEST_ADD(ExampleTestSuite::first_test) TEST_ADD(ExampleTestSuite::second_test) } private: void first_test(); void second_test(); };
For example, the functions declared within ExampleTestSuite
may be declared as:
void ExampleTestSuite::first_test() { // Will succeed since the expression evaluates to true // TEST_ASSERT(1 + 1 == 2) // Will fail since the expression evaluates to false // TEST_ASSERT(0 == 1); } void ExampleTestSuite::second_test() { // Will succeed since the expression evaluates to true // TEST_ASSERT_DELTA(0.5, 0.7, 0.3); // Will fail since the expression evaluates to false // TEST_ASSERT_DELTA(0.5, 0.7, 0.1); }
New output handlers should be derived from Test::Output.
ExampleTestSuite
:
Test::TextOutput output(Test::TextOutput::Verbose); ExampleTestSuite ets; return ets.run(output) ? EXIT_SUCCESS : EXIT_FAILURE;
Note that a single test normally continues after an assert. However, this behavior may be changed with an optional parameter to Test::Suite::run(). For example:
class SomeTestSuite: public Test::Suite { public: SomeTestSuite() { TEST_ADD(SomeTestSuite::test) } private: void test() { TEST_FAIL("this will always fail") TEST_FAIL("this assert will never be executed") } }; bool run_tests() { SomeTestSuite sts; Test::TextOutput output(Test::TextOutput::Verbose); return sts.run(output, false); // Note the 'false' parameter }
class TestSuite1: public Test::Suite { }; // ... with many tests class TestSuite2: public Test::Suite { }; // ... with many tests class TestSuite3: public Test::Suite { }; // ... with many tests bool run_tests() { Test::Suite ts; ts.add(auto_ptr<Test::Suite>(new TestSuite1)); ts.add(auto_ptr<Test::Suite>(new TestSuite2)); ts.add(auto_ptr<Test::Suite>(new TestSuite3)); Test::TextOutput output(Test::TextOutput::Verbose); return ts.run(output); }
class SomeTestSuite: public Test::Suite { public: SomeTestSuite() { TEST_ADD(SomeTestSuite::test1) TEST_ADD(SomeTestSuite::test2) } protected: virtual void setup() {} // setup resources... virtual void tear_down() {} // remove resources... private: void test1() {} // use common resources... void test2() {} // use common resources... };