使用Boost进行C++单元测试

3

我很菜鸟,对单元测试一窍不通,下面是我基础的程序:

#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;
}

我想知道如何测试isEq方法?我希望这个方法的代码覆盖率达到100%,并使用Boost测试框架。有什么想法吗?我正在使用VS 2009 SP1,我需要构建和运行什么?我对单元测试非常困惑。
更新:感谢Stuart,但我仍然不明白我在做什么。我现在有以下代码和以下文件名:
//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);
}

// 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;
}

//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

// 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

我想提醒您,我正在使用Visual Studio 2008 SP1(将此项目作为win32控制台应用程序运行)。每当我构建以下文件时,它会给我错误。我也不确定Boost lib应该如何给我测试结果???会打开一个新窗口吗?会发生什么事情?

2个回答

5

测试它的最简单方法是像这样做(我并不试图编写伟大的测试用例,我会把这个留给你):

#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);
}

如果您这样做,就不需要构建Boost.Test,只需包含头文件即可。希望这能有所帮助!

附言:另外,您可能需要选择一个函数命名方案并坚持使用它--同时使用set_valuesisEq看起来有点不一致,如果值得关注的话...


我在CRectangle.h中有一个类骨架和在CRectangle.cpp中的定义,测试方法如何获取对类方法的引用? - Pritesh Acharya
@PriteshAcharya:你需要在测试文件中添加#include "CRectangle.h",就像之前的评论中提到的那样。 - Stuart Golodetz
@StuartGolodetz 只添加类声明不足以让我在测试方法中创建对象。 - Pritesh Acharya
@PriteshAcharya:你收到了什么错误信息? - Stuart Golodetz
2
@PriteshAcharya 你需要将CRectangle.cpp与你的测试程序链接起来。在普通程序中使用CRectangle时没有任何区别。 - MauganRa
显示剩余4条评论

2

继续回答:

源文件中的类和函数需要被导出,这样它们才能在单元测试文件中使用,具体如下:

//CRectangle.h

#define DLLEXPORT __declspec(dllexport)

class DLLEXPORT CRectangle
{
...
}

// CRectangle.cpp

void DLLEXPORT  CRectangle::set_values (int a, int b)
{
...
}

bool DLLEXPORT  CRectangle::isEq ()
{
..
}

在编译CRectangle source.lib后,最终将单元测试程序与source.lib链接起来,包括“CRectangle.h”并调用CRectangle中的函数。
希望这可以帮到你。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接