我在testBMath.cc
编写了cppUnit代码。我能够为前三个函数编写测试用例,这些函数有加,减和乘法。但我无法为Divide和Swap编写测试用例。我不知道在测试用例中如何处理除以零,以及如何在cppUnit测试用例中检查数字是否被交换。
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 by zero condition!";
}
return (a/b);
}
void swap(int &a, int &b)
{
int temp;
temp = b;
b = a;
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 for divide by zero how should I write
}
void testBMath::testSwap(void)
{
//How should I check for swap
}
void testBMath::tearDown(void)
{
delete mTestObj;
}
//-----------------------------------------------------------------------------
CPPUNIT_TEST_SUITE_REGISTRATION( testBMath );
int main(int argc, char* argv[])
{
// informs test-listener about testresults
CPPUNIT_NS::TestResult testresult;
// register listener for collecting the test-results
CPPUNIT_NS::TestResultCollector collectedresults;
testresult.addListener (&collectedresults);
// register listener for per-test progress output
CPPUNIT_NS::BriefTestProgressListener progress;
testresult.addListener (&progress);
// insert test-suite at test-runner by 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 were successful
return collectedresults.wasSuccessful() ? 0 : 1;
}
答案 0 :(得分:1)
我的建议,将例外从const char*
更改为更有意义的内容,例如std::runtime_error
:
int testMath::Division(int x, int y)
{
if( b == 0 )
{
throw std::runtime_error("Division by zero condition!");
}
return (a/b);
}
比测试看起来像:
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);
}