473,322 Members | 1,504 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Reusing tests in cppUnit

Suppose I want to test several implementations of the same protocol
(interface with clearly defined semantics) using cppUnit. How to reuse
the test that checks the semantics?

Take, for example, the simple SetterGetter interface and the even
simpler implementations Impl1 and Impl2.

class SetterGetter {

SetterGetter& operator=(SetterGetter const& ); // Not Implemented

public:
virtual ~SetterGetter() {}

virtual void set(int ) = 0;

virtual int get() const = 0;
};
class Impl1: public SetterGetter {

/* This class clearly fails to comply with
the intended semantics, thus the protocol
test should fail when passed an object of
this class */

public:
virtual void set(int ) {}

virtual int get() const {return 0;}
};
class Impl2: public SetterGetter {
int i_;

public:
virtual void set(int i) {i_ = i;}

virtual int get() const {return i_;}
};
I would like to write a single class that tests the semantics and that
can be reused for any implementation of the protocol. Any ideas?

I made an attempt, but it did not work. The output said that there was
a Segmentation fault. See below:

class TestSetterGetter: public CppUnit::TestCase {

CPPUNIT_TEST_SUITE( TestSetterGetter );

CPPUNIT_TEST( testSetGet );

CPPUNIT_TEST_SUITE_END();

SetterGetter *psetterGetter_;

public:
TestSetterGetter(): psetterGetter_(0) {}

void set(SetterGetter& sg) {psetterGetter_ = &sg;}

void testSetGet() {
psetterGetter_->set(0);
CPPUNIT_ASSERT_EQUAL(0, psetterGetter_->get());
}
};
class TestImpl1: public TestSetterGetter {

Impl1 impl1_;

public:
TestImpl1() {set(impl1_);}
};
CPPUNIT_TEST_SUITE_REGISTRATION( TestImpl1 );
class TestImpl2: public TestSetterGetter {

Impl2 impl2_;

public:
TestImpl2() {set(impl2_);}
};
CPPUNIT_TEST_SUITE_REGISTRATION( TestImpl2 );

Jan 15 '07 #1
3 3555
Never mind, I got it ...

The solution that worked for me is:
1. Create the test base class, that derives from cppUnit::TestCase, and
define the tests for the protocol in it. Two things to notice: the
termination macro for the declaration of the tests in the fixture must
be CPPUNIT_TEST_SUITE_END_ABSTRACT() and the class should not be
registered as a fixture. See below (the auto_ptr is to avoid having to
deallocate the memory explicitly):

#include <memory>
using namespace std;

class TestSetterGetter: public CppUnit::TestCase {

CPPUNIT_TEST_SUITE( TestSetterGetter );

CPPUNIT_TEST( testSetGet );

// Different termination macro for the test declarations.
CPPUNIT_TEST_SUITE_END_ABSTRACT();

auto_ptr<SetterGetterpsetterGetter_;

virtual auto_ptr<SetterGettermakeSetterGetter() = 0;

public:
void setUp() {psetterGetter_ = makeSetterGetter();}

void tearDown() {}

protected:
void testSetGet() {
psetterGetter_->set(1);
CPPUNIT_ASSERT_EQUAL(1, psetterGetter_->get());
}
};
// No test registration here.

2. Then, for the implementations of the tests of the different
implementations (sorry for the tongue twisting), one thing to notice is
that the initialization of the test declaration requires a different
macro which includes a declaration of the base test. See below:
class TestImpl1: public TestSetterGetter {

// A different macro for the initialization of the test
declarations.
CPPUNIT_TEST_SUB_SUITE( TestImpl1, TestSetterGetter );

CPPUNIT_TEST_SUITE_END();

auto_ptr<SetterGettermakeSetterGetter() {
return auto_ptr<SetterGetter>(new Impl1());
}

public:

// In my example, no extra tests to add here

};
CPPUNIT_TEST_SUITE_REGISTRATION( TestImpl1 ); // Just register your
fixture and you will be in business.
class TestImpl2: public TestSetterGetter {

CPPUNIT_TEST_SUB_SUITE( TestImpl2, TestSetterGetter );
CPPUNIT_TEST_SUITE_END();

auto_ptr<SetterGettermakeSetterGetter() {return
auto_ptr<SetterGetter>(new Impl2());}

public:

// In my example, no extra tests to add here

};
CPPUNIT_TEST_SUITE_REGISTRATION( TestImpl2 );

All of this complication only because reflection is nowhere to be seen
on C++.

Jan 15 '07 #2


On Jan 15, 8:19 pm, "Belebele" <beluc...@gmail.comwrote:
Never mind, I got it ...
snipped...

A similar (but IMHO cleaner) approach is the (google-able)
'Parametrised test case'

Andrew

Jan 15 '07 #3
A similar (but IMHO cleaner) approach is the (google-able)
'Parametrised test case'
I looked it up. I don't feel I understood it very well. It got the
impression that can be used to run the same test on a set of input
data. It was not clear to me how to apply it to the example I posted.
Also, I could not even find the ParameterizedTestCase.h file in my
cppUnit installation (1.10.2)

Could you please elaborate? Could you use the example from my previous
posts? Thanks.

Jan 22 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jo Voordeckers | last post by:
Hello all, I'm pretty new to the Java newsgroups so I apologize for dropping this into several maybe offtopic groups. I'm sorry! So on to my problem... I've come to a point in our RMI...
0
by: Oleg Paraschenko | last post by:
Hello, I'd like to introduce an article which might be of some interest: Reusing XML Processing Code in non-XML Applications HTML: http://uucode.com/texts/genxml/genxml.html PDF: ...
2
by: Scott | last post by:
I'm trying to run cppunit on my system under Mac OS X 10.3.3 with Xcode, and I'm getting the following error when I try to run the program: ZeroLink: unknown symbol...
9
by: Steven T. Hatton | last post by:
I finally got this thing to build. There's something to be said for using the release of the cvs image sometimes. :-/ I started reading the docs, and this example struck me as a fundamentally...
9
by: Alan | last post by:
Using VC++ (1998) compiler with PFE32 editor in Win2K Pro SP4. (DigitalMars CD on order. ) The program (below) instantiates a class and then deletes it. I would have thought that reusing the...
1
by: To Forum | last post by:
hi, After searching around with google, I have not reach a final answer for my problem with installation with CPPUNIT. 1/ how can I register the dll file in VC7, please tell me in detail 2/ have...
4
by: Old Wolf | last post by:
#include <stdio.h> #include <stdarg.h> Is this safe: void foo(const char *fmt, ...) { va_list ap; va_start(ap,fmt);
59
by: Hooyoo | last post by:
How to use CPPUnit effectively? When I want to use CPPUnit, I always fint it useless and waste my time. I think that's mainly because I don't know how to use CPPUnit effectively. So if somebody...
4
by: romcab | last post by:
Hi guys, I'd been searching about CPPUnit and I can't find a very good source about it. At first I thought it was a software that I need to install but found out that what I have is the source...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.