使用CppUnit进行单元测试

  1、CppUnit:C++单元测试库。它是知名单元测试框架JUnit的C++版本。

  2、下载和安装:从http://sourceforge.net/projects/cppunit/files/cppunit获取cppunit-1.12.1.tar.gz -> 解压得到cppunit-1.12.1目录 -> 以root依次执行./configure、make和make install(安装步骤可参考INSTALL文件)。

  32位Ubuntu系统下,安装后的头文件在/usr/local/include/cppunit,而库文件则在/usr/local/lib。

  3、示例:

// 被测试代码:money.h
#ifndef _MONEY_H
#define _MONEY_H

#include <iostream>
#include <string>
using namespace std;

class CMoney
{
public:
    CMoney(double amount, const string &currency) : m_amount(amount), m_currency(currency) {}

    double GetAmount() const { return m_amount; }
    string GetCurrency() const { return m_currency; }

    bool operator==(const CMoney &other) const
    {
        return ((m_amount == other.m_amount) && (m_currency == other.m_currency));
    }

    bool operator!=(const CMoney &other) const
    {
        return !(*this == other);
    }

    CMoney &operator+=(const CMoney &other)
    {
        if (m_currency != other.m_currency)
            cout <<  "Incompatible moneys" << endl;

        m_amount += other.m_amount;

        return *this;
    }

private:
    double m_amount;
    string m_currency;    // 货币(币种)
};

#endif
// 单元测试代码:money_test.cpp
#include <string>
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <cppunit/extensions/HelperMacros.h>
#include <cppunit/ui/text/TestRunner.h>
#include "money.h"
using namespace std;

class CMoneyTest : public CppUnit::TestFixture
{
    // 开始一个test suite的声明。参数为测试用例类的类型,该类必须继承自TestFixture
    CPPUNIT_TEST_SUITE(CMoneyTest);
    // 将方法/函数加入到该test suite。参数为测试用例的方法/函数名,其签名必须为void testMethod();
    CPPUNIT_TEST(testConstructor);
    CPPUNIT_TEST(testOptorEqual);
    CPPUNIT_TEST(testOptorNotEqual);
    CPPUNIT_TEST(testOptorAdd);
    // 结束一个test suite的声明
    CPPUNIT_TEST_SUITE_END();

public:
    /* 初始化。在每个测试用例前都会执行 */
    void setUp() {}
    /* 清除动作。在每个测试用例后都会执行 */
    void tearDown() {}

    /* 以下为四个测试用例,分别用于测试CMoney类的四个成员函数 */
    void testConstructor()
    {
        double dNum = 22124.44;
        string sCurrency = "DD";
        CMoney MyMoney(dNum, sCurrency);

// CPPUNIT_ASSERT_EQUAL_MESSAGE:若assert失败,则打印期望值、实际值以及额外信息 CPPUNIT_ASSERT_EQUAL(dNum, MyMoney.GetAmount()); CPPUNIT_ASSERT_EQUAL(sCurrency, MyMoney.GetCurrency()); }
/* test operator==() */ void testOptorEqual() { const CMoney money123FF(123, "FF"); const CMoney money123USD(123, "USD"); const CMoney money12FF(12, "FF"); const CMoney money12USD(12, "USD"); // check CPPUNIT_ASSERT(money123FF == money123FF); CPPUNIT_ASSERT(!(money12FF == money123FF)); CPPUNIT_ASSERT(!(money123USD == money123FF)); CPPUNIT_ASSERT(!(money12USD == money123FF)); } /* test operator!=() */ void testOptorNotEqual() { const CMoney money123FF(123, "FF"); const CMoney money123USD(123, "USD"); const CMoney money12FF(12, "FF"); const CMoney money12USD(12, "USD"); // check CPPUNIT_ASSERT(!(money123FF != money123FF)); CPPUNIT_ASSERT(money12FF != money123FF); CPPUNIT_ASSERT(money123USD != money123FF); CPPUNIT_ASSERT(money12USD != money123FF); } /* test operator+=() */ void testOptorAdd() { const CMoney money12FF(12, "FF"); CMoney money(123, "FF"); money += money12FF; // check。比较运行结果与预期结果是否一致 const CMoney expectedMoney(135, "FF"); CPPUNIT_ASSERT(expectedMoney == money); } }; // 将test suite注册到名为"alltest"的全局registry suite中,后续可通过getRegistry("alltest")获取到它 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(CMoneyTest, "alltest"); int main() { CPPUNIT_NS::TextUi::TestRunner runner; CPPUNIT_NS::Test *suite = CPPUNIT_NS::TestFactoryRegistry::getRegistry("alltest").makeTest(); runner.addTest(suite); /* 运行测试 */ runner.run(); }

  4、编译与运行:

$ g++ -o test money_test.cpp -lcppunit -ldl
$ ./test 
....


OK (4 tests)

  参考资料:

  http://blog.csdn.net/abcdef0966/article/details/5699248

不断学习中。。。

原文地址:https://www.cnblogs.com/hanerfan/p/4787495.html