您的位置:首页 > 编程语言 > C语言/C++

试用CppUnit--一个简单的例子

2006-02-22 10:34 330 查看
用CppUnit其它与用Junit差不多,基本原理是一致的,不过我以为,与Junit相比,配置它稍微麻烦了一点.
(下一篇文章,将介绍cppunit在vc6.0及vc.net下的配置)

我以一个简单的Money类为例

//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 !!!
Run: 1   Failure total: 1   Failures: 1   Errors: 0
Press any key to continue



输入您的搜索字词 提交搜索表单
<script type="text/javascript"><!--
google_ad_client = "pub-7330597899926046";
google_ad_format = "350x30_sdo";
google_link_target = 2;
google_color_bg = "ffffff";
google_color_link = "000000";
google_encoding = "GB2312";
//-->
</script> <script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
<script type="text/javascript"><!--
google_ad_client = "pub-7330597899926046";
google_ad_slot = "8791774696";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script> <script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息