Re: [Cppunit-devel] Parameterizing tests by using XML
Brought to you by:
blep
From: FUKUDA F. <ff...@nt...> - 2002-07-18 23:26:27
|
Hi. --- "[Cppunit-devel] Parameterizing tests by using XML" / Steffen Huebner / 2002/07/18 16:07:43 +0200 --- >I have looked over CppUnit the last time to find a way making tests more >flexible by parameterizing them. >The idea is to place input parameters and corresponding expected values in a XML file. >That forces to parse the incoming XML file. >... I'm now trying this. - format of testdata(XML) is identical to JTestCase (http://jtestcase.sourceforge.net/) <tests> <class name="CalculatorTest"> <!-- Sample global params --> <params> <param name="gVar1" type="double">10</param> <param name="gVar2" type="int">30</param> </params> <!-- params & asserts for CalculatorTest::testCalculate() --> <method name="testCalculate" test-case="positive-add"> <!-- expected: 10 + 20 = 30 --> <params> <param name="var1" type="int">10</param> <param name="var2" type="int">20</param> <param name="opt">+</param> </params> <asserts> <assert name="result" type="int" action="EQUALS">30</assert> </asserts> </method> <!--- and so on --> </class> </tests> ... and I made class: TextContext/TestContextProxy. you can use as follows: class CalculatorTest : public CppUnit::TestFixture { CPPUNIT_TEST_SUITE(CalculatorTest); CPPUNIT_TEST(testCalculate); CPPUNIT_TEST_SUITE_END(); private: TestContext* tc_; static int atoi(const std::string& str) { int result; std::istringstream strm(str); strm >> result; return result; } public: virtual void setUp() { // load an XML & set the name of class tc_ = new XercesTestContext("../data/testcase.xml", "CalculatorTest"); } virtual void tearDown() { delete tc_; } void testCalculate() { // make a proxy from TestContext & name of method TestContextProxy proxy(tc_,"testCalculate"); // enumerate params/asserts while ( proxy.advance() ) { // extract params TestContext::params_type params = proxy.getTestCaseParams(); int x = atoi(params["var1"]); int y = atoi(params["var2"]); char op = params["opt"][0]; // execute Calculator c; int result = c.calculate(x,y,op); // verify! CPPUNIT_ASSERT_CONTEXTPROXY(proxy, "result", result); } } }; sounds good? ;-) -----:-----:-----:-----:-----:-----:-----:-----:-----:----- FUKUDA (episteme) Fumiki -- magical, but never a magic... |