cppunit学习

cppunit 学习

//name:money.h

#ifndef MONEY_H
#define MONEY_H


#include <string>

class Money
{
public:
Money( double amount)
    : m_amount( amount )
{
}

double getAmount() const
{
    return m_amount;
}

private:
double m_amount;
};


#endif

我们现在需要用Cppunit来测试它的这两个函数-----构造函数和getAmount()函数.

第一步:当然是配置cppunit环境了,这里就不多说了(下面讲的环境是在vc下的)
第二步:利用向导建立一个console application,名字为MoneyApp,编辑项目内的MoneyApp.cpp如下:
// MoneyApp.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <cppunit/CompilerOutputter.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <cppunit/ui/text/TestRunner.h>


int main(int argc, char* argv[])
{
// Get the top level suite from the registry

CppUnit::Test *suite = CppUnit::TestFactoryRegistry::getRegistry().makeTest();

// Adds the test to the list of test to run
CppUnit::TextUi::TestRunner runner;
runner.addTest( suite );

// Change the default outputter to a compiler error format outputter
runner.setOutputter( new CppUnit::CompilerOutputter( &runner.result(),
                                                       std::cerr ) );
// Run the tests.
bool wasSucessful = runner.run();

// Return error code 1 if the one of test failed.

return wasSucessful ? 0 : 1;
}

第三步:现在才到了我们为Money类写测试的时候了,我们首先新建一个头文件MoneyTest.h,其内容如下:
#ifndef MONEYTEST_H
#define MONEYTEST_H

#include <cppunit/extensions/HelperMacros.h>

class MoneyTest : public CppUnit::TestFixture
{
//其实就是在这里面加入要测试的项目
CPPUNIT_TEST_SUITE( MoneyTest );
CPPUNIT_TEST( testGetAmount );
CPPUNIT_TEST_SUITE_END();

public:
void setUp();
void tearDown();


void testConstructor();
void testGetAmount();
};


#endif // MONEYTEST_H

然后,我们再新建一个cpp文件MoneyTest.cpp,(对了,真正的测试正是在这里面),其内容如下:
#include "stdafx.h"
#include "MoneyTest.h"
#include "Money.h"

//这句很重要,应与前面的"CppUnit::Test *suite = CppUnit::TestFactoryRegistry::getRegistry().makeTest()"对照起来看
// Registers the fixture into the 'registry'
CPPUNIT_TEST_SUITE_REGISTRATION( MoneyTest );

void
MoneyTest::setUp()
{

}


void
MoneyTest::tearDown()
{
}

void
MoneyTest::testConstructor()
{

}

void
MoneyTest::testGetAmount()
{
const double longNumber = 12345678.90123;
Money money ( longNumber);

CPPUNIT_ASSERT_EQUAL( longNumber,money.getAmount());
}
第四步:好了,可以按ctrl+F5跑一下了,运行结果是这样:

.

OK (1)
Press any key to continue

如果想看一看断言失败是什么效果的话,可以这样:
把CPPUNIT_ASSERT_EQUAL( longNumber,money.getAmount());
改为CPPUNIT_ASSERT_EQUAL( 100,money.getAmount());
则运行结果就是这样.
.F

D:/ZHANGYD/MoneyApp/MoneyTest.cpp(38):Assertion
Test name: MoneyTest::testGetAmount
equality assertion failed
- Expected: 100
- Actual : 1.23457e+007

Failures !!!

在cppunit中的七种断言方式

CPPUNIT_ASSERT(condition):判断condition的值是否为真,如果为假则生成错误信息。

CPPUNIT_ASSERT_MESSAGE(message, condition):与CPPUNIT_ASSERT类似,但结果为假时报告messsage信息。

CPPUNIT_FAIL(message):直接报告messsage错误信息。

CPPUNIT_ASSERT_EQUAL(expected, actual):判断expected和actual的值是否相等,如果不等输出错误信息。

CPPUNIT_ASSERT_EQUAL_MESSAGE(message, expected, actual):与CPPUNIT_ASSERT_EQUAL类似,但断言失败时输出message信息。

CPPUNIT_ASSERT_DOUBLES_EQUAL(expected, actual, delta):判断expected与actual的偏差是否小于delta,用于浮点数比较。

CPPUNIT_ASSERT_THROW(expression, ExceptionType):判断执行表达式expression后是否抛出ExceptionType异常。

CPPUNIT_ASSERT_NO_THROW(expression):断言执行表达式expression后无异常抛出。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值