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
> 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
相关文章推荐
- 一个简单的Linux下Client/Server应答例子
- 一个关于linux socket的简单例子
- Linux下,使用C/C++编写"静态链接库"的一个简单例子
- linux c 一个autotools的最简单例子
- 一个Linux内核利用init_task进行进程管理的简单例子
- linux两个程序通过共享内存通信的一个简单例子
- 一个简单的Linux下Client/Server应答例子
- Linux平台自动部署脚本的一个简单例子
- linux下对信号量测试的一个简单例子
- 一个关于linux socket的简单例子
- linux两个程序通过共享内存通信的一个简单例子
- linux 一个简单的进程创建例子
- 一个简单的Linux多线程例子 带你洞悉互斥量 信号量 条件变量编程
- 一个简单的Linux下Client/Server应答例子
- 一个简单的Linux下Client/Server应答例子
- 一个关于linux socket的简单例子
- 一个简单的Linux下组播通信的例子
- 试用CppUnit--一个简单的例子
- 【Linux】一个简单的线程创建和同步的例子
- linux 一个简单的字符设备驱动例子