RE: [Cppunit-devel] Parameterizing tests by using XML
Brought to you by:
blep
From: Michel A. <Arm...@si...> - 2002-07-19 06:36:17
|
Hi all. Actually I was thinking about a little bit different solution here. It differs mainly in two points: 1. the framework should be responsible for parsing the xml file and invoke the test with a particular set of parameters (as opposed to let each of the test cases perform the parsing) 2. the DTD of the xml file is a less sophisticated It follows a short description of my suggestion: A (new) BEGIN_TEST_SUITE macro is able to accept the name of a (input-) file. If this (new) macro is used a new type of TestCaller (ParameterizedTestCaller) will be used for this suite. If a ParameterizedTestCaller is executing its test, it will scan the in-file for a section for this particular test. The section will be parsed and a TestSuite object will be created that contains one Test object for each set of parameters. This newly created TestSuite object will then be executed using the run(TestResult* result) method. The (parameterized) test method will be called with two std::istreams: the one contains the (input) parameters, the other one contains expected results (can also be an empty stream). Thus the test method is able to read its parameters using the operator>>. If there is no section for a test method, it is only called once (with no parameters?). Example: MyTest.xml: <test name=”testMethod”> <call name=”simple”> <description> Simple and valid set of parameters. Format of “in” is: “int int std::string” Format of “exp” is: “int” </description> <in> 3 -1 myCorrectString </in> <exp> 25 </exp> </call> </test> MyTest.cpp: void testMethod(std::istream& param_in, std::istream& exp_in) { int param1, param2; std::string param3; param_in >> param1 >> param2 >> param3; int exp; exp_in >> exp; // perform tests using these parameters ... } I was also thinking about introducing a TestContext class that would be responsible for hosting the information gained from the xml file (e.g. input and expected streams). But I am not sure about how to do this in a way that a improved flexibility/exchangability is gained. I will expand this issue in a separate email... Regards Armin > -----Original Message----- > From: FUKUDA Fumiki [mailto:ff...@nt...] > Sent: Freitag, 19. Juli 2002 01:25 > To: cpp...@li... > Subject: Re: [Cppunit-devel] Parameterizing tests by using XML > > > 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... > > > ------------------------------------------------------- > This sf.net email is sponsored by:ThinkGeek > Welcome to geek heaven. > http://thinkgeek.com/sf > _______________________________________________ > Cppunit-devel mailing list > Cpp...@li... > https://lists.sourceforge.net/lists/listinfo/cppunit-devel > |