473,662 Members | 2,724 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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=(Sette rGetter 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 TestSetterGette r: public CppUnit::TestCa se {

CPPUNIT_TEST_SU ITE( TestSetterGette r );

CPPUNIT_TEST( testSetGet );

CPPUNIT_TEST_SU ITE_END();

SetterGetter *psetterGetter_ ;

public:
TestSetterGette r(): psetterGetter_( 0) {}

void set(SetterGette r& sg) {psetterGetter_ = &sg;}

void testSetGet() {
psetterGetter_->set(0);
CPPUNIT_ASSERT_ EQUAL(0, psetterGetter_->get());
}
};
class TestImpl1: public TestSetterGette r {

Impl1 impl1_;

public:
TestImpl1() {set(impl1_);}
};
CPPUNIT_TEST_SU ITE_REGISTRATIO N( TestImpl1 );
class TestImpl2: public TestSetterGette r {

Impl2 impl2_;

public:
TestImpl2() {set(impl2_);}
};
CPPUNIT_TEST_SU ITE_REGISTRATIO N( TestImpl2 );

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

The solution that worked for me is:
1. Create the test base class, that derives from cppUnit::TestCa se, 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_SU ITE_END_ABSTRAC T() 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 TestSetterGette r: public CppUnit::TestCa se {

CPPUNIT_TEST_SU ITE( TestSetterGette r );

CPPUNIT_TEST( testSetGet );

// Different termination macro for the test declarations.
CPPUNIT_TEST_SU ITE_END_ABSTRAC T();

auto_ptr<Setter GetterpsetterGe tter_;

virtual auto_ptr<Setter GettermakeSette rGetter() = 0;

public:
void setUp() {psetterGetter_ = makeSetterGette r();}

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 TestSetterGette r {

// A different macro for the initialization of the test
declarations.
CPPUNIT_TEST_SU B_SUITE( TestImpl1, TestSetterGette r );

CPPUNIT_TEST_SU ITE_END();

auto_ptr<Setter GettermakeSette rGetter() {
return auto_ptr<Setter Getter>(new Impl1());
}

public:

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

};
CPPUNIT_TEST_SU ITE_REGISTRATIO N( TestImpl1 ); // Just register your
fixture and you will be in business.
class TestImpl2: public TestSetterGette r {

CPPUNIT_TEST_SU B_SUITE( TestImpl2, TestSetterGette r );
CPPUNIT_TEST_SU ITE_END();

auto_ptr<Setter GettermakeSette rGetter() {return
auto_ptr<Setter Getter>(new Impl2());}

public:

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

};
CPPUNIT_TEST_SU ITE_REGISTRATIO N( 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 ParameterizedTe stCase.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
9294
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 application where I need to have server callbacks to the client RMI applications. I've used the technique where the client passes an UnicastRemoteObject of itself to a RMI server method that registers the clientinterface object in a Vector. Now when I do...
0
1136
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: http://uucode.com/texts/genxml/genxml.pdf <abstract>
2
3827
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 '__ZN7CppUnit8TestCase3runEPNS_10TestResultE' prog has exited due to signal 6 (SIGABRT). Any idea why I would be seeing this? The program compiles with no problems, it just looks like it can't find a method it needs. I've
9
1968
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 bad design for C++. Perhaps it's not bad design in the sense that it will fail, or that it can't be maintained. But there seems to be something fundamentally un-C++ about this. Does anybody else see what I'm talking about here? class...
9
2348
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 deleted pointer would have caused an exception, but it does not - or my coding or thinking is incorrect? #include <iostream> using namespace std;
1
1833
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 any one succeed with DSPPlugin. I have found on the page of CPPUNIT a partial answer http://cppunit.sourceforge.net/cgi-bin/moin.cgi/BuildingCppUnit1 but still, I wonder if anyone have a better solution. Thanks TF
4
9043
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
12613
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 here has some experience, please share it to me. And also I expect answers to these questions: 1. Should I write test codes for every member function of my class? If not, which function should be tested? 2. What else should I consider apart from...
4
4152
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 code. I want to know if i need to build it and how can I use this one. Hope you can help me guys.
0
8435
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8857
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8768
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8547
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6186
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5655
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4348
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2763
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1999
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.