I am really new to Unit testing, and I have the following really basic program:
#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;
}
I want to know how do I test the method isEq? I want 100% code coverage of this method and I want to use the Boost test framework. Any ideas? I am using VS 2009 SP1, what do I build and run? I am very confused with Unit testing.
UPDATE:
Thanks Stuart, however what Im doing is still not making sense. I know have the following code with the following file names:
//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
I want to remind you that I am using Visual Studio 2008 SP1 (running this project as a win32 console app). Whenever I build the following files it gives me errors. I am also unsure how the Boost lib is supposed to give me results of my test??? Will a new window open up??? What will happen exactly?
The simplest way to test it is to do something like this (I'm not attempting to write great test cases, I'll leave that to you):
#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);
}
No need to build Boost.Test if you do that, you just include the header and away you go. Hope that helps a bit!
p.s. As a side comment, you might want to pick a naming scheme for your functions and stick to it -- having both set_values and isEq looks a bit inconsistent, for what it's worth...
To continue the answer :
The class and functions in the source file need to be exported, so that they can be used in the Unit test file as follows :
//CRectangle.h
#define DLLEXPORT __declspec(dllexport)
class DLLEXPORT CRectangle
{
...
}
// CRectangle.cpp
void DLLEXPORT CRectangle::set_values (int a, int b)
{
...
}
bool DLLEXPORT CRectangle::isEq ()
{
..
}
On compiling the CRectangle source.lib.
Finally link the Unit Test program with source.lib, include "CRectangle.h" and call the functions in CRectangle.
Hope this helps..
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With