linux下cppunit的一个简单的例子

/*************************************************************************
    > File Name: main.cpp
    > Author: CZQ
    > Function:
    > Created Time: 2016年09月20日 星期二 14时29分04秒
 ************************************************************************/


#include<cppunit/extensions/TestFactoryRegistry.h>
//使用文本执行器
#include<cppunit/ui/text/TestRunner.h>
//如果不更改 TestSuite ,本文件后期不需要更改


int main()
{
    CppUnit::TextUi::TestRunner runner;

    //从注册的 TestSuite 中获取特定的TestSuite,没有参数获取未命名的TestSuite
    CppUnit::TestFactoryRegistry &registry = CppUnit::TestFactoryRegistry::getRegistry("alltest");
    //添加这个TestSuite到TestRunner中
    runner.addTest(registry.makeTest());

    runner.run();
}



/*************************************************************************
    > File Name: test_math.cpp
    > Author: CZQ
    > Function: a simple test
    > Created Time: 2016年09月20日 星期二 14时29分04秒
 ************************************************************************/


#include"test_math.h"

//把这个 TestSuite 注册到名字为“alltest”的 TestSuite中,如果没有定义也会自动定义
//也可以用 CPPUNIT_TEST_SUITE_REGISTRATION(TestMath);注册到全局的一个未命名的 TestSuite 中
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(TestMath, "alltest");

void TestMath::setUp()
{
    x = 2;
    y = 3;
}

void TestMath::tearDown()
{
}

void TestMath::test_add()
{
    int result = x + y;

    CPPUNIT_ASSERT(result == 5);
}

void TestMath::test_sub()
{
    int result = x - y;

    CPPUNIT_ASSERT(result == -1);
}


/*************************************************************************
    > File Name: test_math.h
    > Author: CZQ
    > Function:a simple math test
    > Created Time: 2016年09月20日 星期二 14时29分03秒
 ************************************************************************/


#ifndef _TEST_MATH_H
#define _TEST_MATH_H

//要使用 TestFixture 这个类,所以引入 HelperMacros.h头
#include<cppunit/extensions/HelperMacros.h>

//声明一个测试类
class TestMath : public CppUnit::TestFixture
{
    //添加一个 TestSuite
    CPPUNIT_TEST_SUITE(TestMath);
    //添加新的测试用例到 TestSuite ,定义新的测试类
    CPPUNIT_TEST(test_add);
    CPPUNIT_TEST(test_sub);

    CPPUNIT_TEST_SUITE_END();

protected:
    int x, y;

public:
    TestMath()
    {}
    //初始化函数
    void setUp();
    //清理函数
    void tearDown();

    void test_add();
    void test_sub();
};


#endif





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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值