c++ - CppUnit : Not able to write test cases -
i have written cppunit codes in testbmath.cc
. able write test cases first 3 functions add,subtract , multiply. not able write test cases divide , swap.i don't know how handle divide 0 in test cases , how check numbers swapped or not in cppunit test cases.
testmath.h
#ifndef test_math_h__ #define test_math_h__ class testmath { public: int addition(int x, int y); int multiply(int x, int y); int subtraction(int x, int y); int division(int x, int y); void swap(int &x, int &y); }; #endif
testmath.cc
#include "testmath.h" int testmath::addition(int x, int y) { return (x + y); } int testmath::multiply(int x, int y) { return (x * y); } int testmath::subtraction(int x, int y) { return (x - y); } int testmath::division(int x, int y) { if( b == 0 ) { throw "division 0 condition!"; } return (a/b); } void swap(int &a, int &b) { int temp; temp = b; b = a; = temp; }
testbmath.cc
#include <iostream> #include <string> #include <list> #include "cppunit/testcase.h" #include "cppunit/testfixture.h" #include "cppunit/ui/text/texttestrunner.h" #include "cppunit/extensions/helpermacros.h" #include "cppunit/extensions/testfactoryregistry.h" #include "cppunit/testresult.h" #include "cppunit/testresultcollector.h" #include "cppunit/testrunner.h" #include "cppunit/brieftestprogresslistener.h" #include "cppunit/compileroutputter.h" #include "netinet/in.h" #include "testmath.h" using namespace cppunit; using namespace std; //----------------------------------------------------------------------------- class testbmath : public cppunit::testfixture { cppunit_test_suite(testbmath); cppunit_test(testaddition); cppunit_test(testmultiply); cppunit_test(testsubtraction); cppunit_test(testdivision); cppunit_test(testswap); cppunit_test_suite_end(); public: void setup(void); void teardown(void); protected: void testaddition(void); void testmultiply(void); void testsubtraction(void); void testdivision(void); void testswap(void); private: testmath *mtestobj; }; //----------------------------------------------------------------------------- void testbmath::setup(void) { mtestobj = new testmath(); } void testbmath::testaddition(void) { cppunit_assert(5 == mtestobj->addition(2,3)); } void testbmath::testmultiply(void) { cppunit_assert(6 == mtestobj->multiply(2,3)); } void testbmath::testsubtraction(void) { cppunit_assert(2 == mtestobj->subtraction(5,3)); } void testbmath::testdivision(void) { cppunit_assert(6 == mtestobj->division(12,2)); //but divide 0 how should write } void testbmath::testswap(void) { //how should check swap } void testbmath::teardown(void) { delete mtestobj; } //----------------------------------------------------------------------------- cppunit_test_suite_registration( testbmath ); int main(int argc, char* argv[]) { // informs test-listener testresults cppunit_ns::testresult testresult; // register listener collecting test-results cppunit_ns::testresultcollector collectedresults; testresult.addlistener (&collectedresults); // register listener per-test progress output cppunit_ns::brieftestprogresslistener progress; testresult.addlistener (&progress); // insert test-suite @ test-runner registry cppunit_ns::testrunner testrunner; testrunner.addtest (cppunit_ns::testfactoryregistry::getregistry().maketest ()); testrunner.run(testresult); // output results in compiler-format cppunit_ns::compileroutputter compileroutputter(&collectedresults, std::cerr); compileroutputter.write (); // return 0 if tests successful return collectedresults.wassuccessful() ? 0 : 1; }
my suggestions, change exception const char*
more meaningful, std::runtime_error
:
int testmath::division(int x, int y) { if( b == 0 ) { throw std::runtime_error("division 0 condition!"); } return (a/b); }
than test's like:
void testbmath::testdivision(void) { cppunit_assert(6 == mtestobj->division(12,2)); cppunit_assert_throw(mtestobj->division(12,0), std::runtime_error); } void testbmath::testswap(void) { int x = 2; int y = 3; mtestobj->swap(x, y); cppunit_assert(x == 3 && y == 2); }
Comments
Post a Comment