vs2015 cppunit配置及使用

原文链接

第一步

下载源代码 http://sourceforge.net/projects/cppunit/files/cppunit/1.12.1/cppunit-1.12.1.tar.gz/download

第二步

解压下载的文件,并进入到src目录,打开CppUnitLibraries.dsw


提示升级安装什么的,都点确定


右键cppunit,选择属性,在弹出窗口将目标文件名由\(\$(ProjectName)\)改成\(\$(ProjectName)d\)


右键cppunit_dll,选择属性,在弹出窗口将目标文件名由\(\$(ProjectName)\)改成cppunitd_dll


右键DllPlugInTester,选择属性,在弹出窗口将目标文件名由\(\$(ProjectName)\)改成\(\$(ProjectName)d\_dll\)
右键TestPlugInRunner,选择属性,在弹出窗口将目标文件名由\(\$(ProjectName)\)改成\(\$(ProjectName)d\)
右键TestRunner,选择属性,在弹出窗口将目标文件名由\(\$(ProjectName)\)改成\(\$(ProjectName)d\)

第三步

右键DSPlugin,选择属性,在配置属性-链接器-高级,将无入口点改为 是


打开TestRunner-UserInterface-MsDevCallerListCtrl.cpp,在67行中将version由7.0改成8.0



编译生成lib库

选择生成-批生成


全选后点击生成


结果

使用

新建一个项目

右键项目-属性

配置属性-VC++目录

包含目录添加include

库目录添加lib

配置属性-链接器-输入

附加依赖项添加cppunitd.lib

calculator类测试

总共要建立5个文件,建完分布如下

代码部分

// calculator.h
#pragma once
#ifndef CALCULATOR_H_
#define CALCULATOR_H_

class Calculator {
public:
	Calculator();
	void add(double x);
	void substract(double x);
	void multiply(double x);
	void divide(double x);
	void square(double x);
	void suqareRoot(double x);
	void clear();
	double get_result();
private:
	double result;
};

#endif // CALCULATOR_H_
// calculator.cpp
#include "stdafx.h"
#include "calculator.h"
#include <iostream>
#include <cmath>
using namespace std;
Calculator::Calculator() : result(0.0) {}
void Calculator::add(double x) {
	result += x;
}
void Calculator::substract(double x) {
	result -= x;
}
void Calculator::multiply(double x) {
	result *= x;
}
void Calculator::divide(double x) {
	result /= x;
}
void Calculator::square(double x) {
	result = x * x;
}
void Calculator::suqareRoot(double x) {
	result = pow(result, x);
}
void Calculator::clear() {
	result = 0;
}
double Calculator::get_result() {
	return result;
}
// test_calculator.h
#pragma once
#include <cppunit/extensions/HelperMacros.h>  // 头文件

class test_calculator : public CppUnit::TestFixture
{
	CPPUNIT_TEST_SUITE(test_calculator);
	// 添加两个测试函数
	CPPUNIT_TEST(test_calculator1);
	CPPUNIT_TEST(test_calculator2);
	CPPUNIT_TEST(test_calculator3);
	CPPUNIT_TEST_SUITE_END();
public:
	void setUp();
	void tearDown();
	test_calculator();
	~test_calculator();
	void test_calculator1();
	void test_calculator2();
	void test_calculator3();
};
// test_calculator.cpp
#include "stdafx.h"
#include "test_calculator.h"
#include "calculator.h"
#include <iostream>
using namespace std;
// 将测试包注册为aTest
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(test_calculator, "aTest");

test_calculator::test_calculator() {}
test_calculator::~test_calculator() {}
void test_calculator::setUp() 
{
	cout << "begin test" << endl;
}
void test_calculator::tearDown() {
	cout << "end test" << endl;
}

void test_calculator::test_calculator1()
{
	Calculator cal;  // 创建一个计算器类
	cal.add(100);
	CPPUNIT_ASSERT(cal.get_result() == 100);
}
void test_calculator::test_calculator2()
{
	Calculator cal;
	cal.add(123);
	cal.multiply(100);
	CPPUNIT_ASSERT(cal.get_result() == 12300);
}

void test_calculator::test_calculator3()
{
	Calculator cal;
	cal.add(123);
	cal.divide(123);
	CPPUNIT_ASSERT(cal.get_result() == 1);
}
// test_main.cpp
#include "stdafx.h"
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <cppunit/ui/text/TestRunner.h>
#include <cppunit/TestResult.h>
#include <cppunit/TestResultCollector.h>
#include <cppunit/BriefTestProgressListener.h>
#include <cppunit/CompilerOutputter.h>

int _tmain()
{
	CPPUNIT_NS::TestResult tr;
	CPPUNIT_NS::TestResultCollector trc;
	tr.addListener(&trc);

	CPPUNIT_NS::BriefTestProgressListener progress;
	tr.addListener(&progress);

	CPPUNIT_NS::TestRunner runner;
	CPPUNIT_NS::TestFactoryRegistry &reg = CppUnit::TestFactoryRegistry::getRegistry("aTest");
	runner.addTest(reg.makeTest());
	runner.run(tr);

	CPPUNIT_NS::CompilerOutputter outputter(&trc, CPPUNIT_NS::stdCOut());
	outputter.write();
	system("pause");
	return 0;
}
posted @ 2020-05-20 20:26  麻辣猪仔  阅读(874)  评论(0编辑  收藏  举报