您的位置:首页 > 运维架构 > Linux

linux下cppunit的一个简单的例子

2016-09-20 15:15 316 查看
/*************************************************************************

    > 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 ®istry = 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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c++ linux