我对单元测试非常陌生,我有以下非常基本的程序:
#include "stdafx.h"
// classes example
#include <iostream>
using namespace std;
class CRectangle {
int x, y;
public:
void set_values (int,int);
int area () {return (x*y);}
bool isEq ();
};
void CRectangle::set_values (int a, int b) {
x = a;
y = b;
}
bool CRectangle::isEq () {
if(x==y)
{
return true;
}
else
{
return false;
}
}
int main () {
CRectangle rect;
rect.set_values (3,3);
cout << "area: " << rect.area();
cout << " isEq: " << rect.isEq() << " \n";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我想知道如何测试 isEq 方法?我想要这个方法的代码覆盖率达到 100%,并且我想要使用 Boost 测试框架。有任何想法吗?我正在使用 VS 2009 SP1,我要构建和运行什么?我对单元测试非常困惑。
更新:
谢谢斯图尔特,但是我所做的仍然没有意义。我知道以下代码具有以下文件名:
//FILENAME: test.cpp
#include "stdafx.h"
#define BOOST_TEST_MODULE isEq Test
#include <boost/test/included/unit_test.hpp>
#include "CRectangle.h"
BOOST_AUTO_TEST_CASE(isEq_test)
{
CRectangle rect;
rect.set_values(5,5);
BOOST_CHECK_EQUAL(rect.isEq(), true);
rect.set_values(23,9);
BOOST_CHECK_EQUAL(rect.isEq(), false);
}
Run Code Online (Sandbox Code Playgroud)
// FILENAME: CRectangle.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "CRectangle.h"
//#include <boost/test/included/unit_test.hpp>
// classes example
#include <iostream>
using namespace std;
void CRectangle::set_values (int a, int b) {
x = a;
y = b;
}
bool CRectangle::isEq () {
if(x==y)
{
return true;
}
else
{
return false;
}
}
int main () {
CRectangle rect;
rect.set_values (3,3);
cout << "area: " << rect.area();
cout << " isEq: " << rect.isEq() << " \n";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
//FILENAME: CRectangle.h
//void CRectangle::set_values (int a, int b);
//bool CRectangle::isEq ();
#ifndef CRECTANGLE_H
#define CRECTANGLE_H
class CRectangle {
int x, y;
public:
void set_values (int,int);
int area () {return (x*y);}
bool isEq ();
};
#endif
Run Code Online (Sandbox Code Playgroud)
// FILENAME: stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//
#pragma once
#include "targetver.h"
#include <stdio.h>
#include <tchar.h>
// TODO: reference additional headers your program requires here
#ifdef _UT
#define ut_private public
#define ut_protected public
#else
#define ut_private private
#define ut_protected protected
#endif
Run Code Online (Sandbox Code Playgroud)
我想提醒您,我正在使用 Visual Studio 2008 SP1(将此项目作为 win32 控制台应用程序运行)。每当我构建以下文件时,都会出现错误。我也不确定 Boost 库应该如何给我测试结果???会打开新窗口吗???究竟会发生什么?
测试它的最简单方法是执行以下操作(我并不是试图编写出色的测试用例,我将其留给您):
#define BOOST_TEST_MODULE isEq Test
#include <boost/test/included/unit_test.hpp>
#include "CRectangle.h"
BOOST_AUTO_TEST_CASE(isEq_test)
{
CRectangle rect;
rect.set_values(5,5);
BOOST_CHECK_EQUAL(rect.isEq(), true);
rect.set_values(23,9);
BOOST_CHECK_EQUAL(rect.isEq(), false);
}
Run Code Online (Sandbox Code Playgroud)
如果这样做,则无需构建 Boost.Test,只需包含标头即可。希望有点帮助!
ps 作为附带评论,您可能想为您的函数选择一个命名方案并坚持使用它 - 两者兼而有之,set_values并且isEq看起来有点不一致,因为它的价值......
| 归档时间: |
|
| 查看次数: |
8442 次 |
| 最近记录: |