Parametrized test case
- In the implementation of the test case, the callback for setUp(), run() and tearDown() are provided through generic functors. Those functors can be bound to a struct functor, a C function or a C++ method. Parameters can also be bound by the functors so that when the call is made, specific values are passed as parameters. Here is an excerpt from examples/parametrized_test_case/:
1 #include <cpput/testcase.h>
2 #include <cpput/testsuite.h>
3 #include <cpput/testrunner.h> // cppunit2 testrunner for opentest
4 #include <cpput/assert.h>
5 #include <opentest/texttestdriver.h>
6 #include <opentest/properties.h>
7
8
9 static void testMultiply( int x, int y, int expected_result )
10 {
11 int result = x * y;
12 CPPUT_ASSERT_EQUAL( expected_result, result );
13 }
14
15
16
17 int main( int argc, const char *argv[] )
18 {
19 CppUT::TestSuitePtr allSuite = CppUT::makeTestSuite( "All tests" );
20 allSuite->add( CppUT::makeTestCase(
21 CppTL::bind_cfn( testMultiply, 7, 2, 7*2 ),
22 "multiply 7*2" ) );
23 allSuite->add( CppUT::makeTestCase(
24 CppTL::bind_cfn( testMultiply, 7, 5, 7*4 ),
25 "multiply 7*5 (failing demo)" ) );
26
27 CppUT::TestRunner runner;
28 CppUT::AbstractTestSuitePtr rootSuite =
29 CppTL::staticPointerCast<CppUT::AbstractTestSuite>( allSuite );
30 runner.setRootSuite( rootSuite );
31
32 OpenTest::TextTestDriver driver( runner );
33 bool sucessful = driver.run();
34 return sucessful ? 0 : 1;
35 }
- While this example is trivial, the parameters bound to the test case could easily be read from some input files.