473,624 Members | 2,217 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Unit Testing in C++

Hello,

I'm after doing some C++ unit testing, I'm using CppUnit. I like the
look of RudeMocks, but it doesn't work for Solaris/Sparc, so its no
good to me sadly.

So, I have class A, and it uses classes B and C. B and C have methods
A uses, but they're not virtual, and I don't want to hack the code so
that they are just to allow Unit Tests to work. This means that
inheriting from B and C to provide Mock classes is a non starter.

So, I've been trying to provide a mock implementation which is
basically a copy of B or C, but with the guts ripped out and swapped
for mock code. I've then tried to get the code for A, which is under
test, to include the mock versions of the classes headers it depends
on, and link to the objects built from the mock source.

The trouble is, A, B and C are all in the same directory. So, when A
does #include "B.h", it picks up the real version, even if I add the
include path to the mock header. That's because the current directory
takes precedence using VC++, it may well do with GCC and Suncc, which
I'm also using.

I've tried to get around this by not building an object file for class
A, but using A.obj which has already been built, but linking it
against my mock versions of B.obj and C.obj. This would work, except
that C.h defines C::~C(), and so A.obj ends up with a definition of
C::~C() in it, and I get multiply defined symbol link errors. I could
make sure all method definitions are in cpp files, but often having a
getter or setter inline is perfectly sensible.

How do people get around these issues of mocking out classes
effectively from a build system point of view?
Jun 27 '08 #1
20 2417
earthwormgaz wrote:
Hello,

I'm after doing some C++ unit testing, I'm using CppUnit. I like the
look of RudeMocks, but it doesn't work for Solaris/Sparc, so its no
good to me sadly.

So, I have class A, and it uses classes B and C. B and C have methods
A uses, but they're not virtual, and I don't want to hack the code so
that they are just to allow Unit Tests to work. This means that
inheriting from B and C to provide Mock classes is a non starter.

So, I've been trying to provide a mock implementation which is
basically a copy of B or C, but with the guts ripped out and swapped
for mock code. I've then tried to get the code for A, which is under
test, to include the mock versions of the classes headers it depends
on, and link to the objects built from the mock source.

The trouble is, A, B and C are all in the same directory. So, when A
does #include "B.h", it picks up the real version, even if I add the
include path to the mock header. That's because the current directory
takes precedence using VC++, it may well do with GCC and Suncc, which
I'm also using.
you can do this:

#ifdef USE_MOCK_CLASSE S
#include "Bmode.h"
#else
#include "B.h"
#endif

Define USE_MOCK_CLASSE S when you want to do the testing, that is, change
your make file to do it automatically

I've tried to get around this by not building an object file for class
A, but using A.obj which has already been built, but linking it
against my mock versions of B.obj and C.obj. This would work, except
that C.h defines C::~C(), and so A.obj ends up with a definition of
C::~C() in it, and I get multiply defined symbol link errors. I could
make sure all method definitions are in cpp files, but often having a
getter or setter inline is perfectly sensible.

How do people get around these issues of mocking out classes
effectively from a build system point of view?
Jun 27 '08 #2
On 24 Jun, 13:19, anon <a...@no.nowrot e:
earthwormgaz wrote:
Hello,
I'm after doing some C++ unit testing, I'm using CppUnit. I like the
look of RudeMocks, but it doesn't work for Solaris/Sparc, so its no
good to me sadly.
So, I have class A, and it uses classes B and C. B and C have methods
A uses, but they're not virtual, and I don't want to hack the code so
that they are just to allow Unit Tests to work. This means that
inheriting from B and C to provide Mock classes is a non starter.
So, I've been trying to provide a mock implementation which is
basically a copy of B or C, but with the guts ripped out and swapped
for mock code. I've then tried to get the code for A, which is under
test, to include the mock versions of the classes headers it depends
on, and link to the objects built from the mock source.
The trouble is, A, B and C are all in the same directory. So, when A
does #include "B.h", it picks up the real version, even if I add the
include path to the mock header. That's because the current directory
takes precedence using VC++, it may well do with GCC and Suncc, which
I'm also using.

you can do this:

#ifdef USE_MOCK_CLASSE S
#include "Bmode.h"
#else
#include "B.h"
#endif

Define USE_MOCK_CLASSE S when you want to do the testing, that is, change
your make file to do it automatically
I've tried to get around this by not building an object file for class
A, but using A.obj which has already been built, but linking it
against my mock versions of B.obj and C.obj. This would work, except
that C.h defines C::~C(), and so A.obj ends up with a definition of
C::~C() in it, and I get multiply defined symbol link errors. I could
make sure all method definitions are in cpp files, but often having a
getter or setter inline is perfectly sensible.
How do people get around these issues of mocking out classes
effectively from a build system point of view?

I'd rather not have to change the original source code just to
accommodate mocking for unit tests. Is that actually what most people
end up doing?

It seems a bit of an 'orrible fix to me.
Jun 27 '08 #3
On Jun 24, 9:14*am, earthwormgaz <earthworm...@g ooglemail.comwr ote:
[snip]
I'd rather not have to change the original source code just to
accommodate mocking for unit tests. Is that actually what most people
end up doing?
So, your plan is to do unit testing without writing any code?

Let us know how that works out.
Socks
Jun 27 '08 #4
On 24 Jun, 14:33, Puppet_Sock <puppet_s...@ho tmail.comwrote:
On Jun 24, 9:14*am, earthwormgaz <earthworm...@g ooglemail.comwr ote:
[snip]
I'd rather not have to change the original source code just to
accommodate mocking for unit tests. Is that actually what most people
end up doing?

So, your plan is to do unit testing without writing any code?

Let us know how that works out.
Socks
I'm expecting to write code, in the form of test code, and mock
objects. I'm hoping not to have to change the classes under test just
to test them.
Jun 27 '08 #5
On Jun 24, 3:14 pm, earthwormgaz <earthworm...@g ooglemail.comwr ote:
On 24 Jun, 13:19, anon <a...@no.nowrot e:
earthwormgaz wrote:
[...]
The trouble is, A, B and C are all in the same directory.
And if you know the problem, you know the way to fix it.
So, when A
does #include "B.h", it picks up the real version, even if I add the
include path to the mock header. That's because the current directory
takes precedence using VC++, it may well do with GCC and Suncc, which
I'm also using.
you can do this:
#ifdef USE_MOCK_CLASSE S
#include "Bmode.h"
#else
#include "B.h"
#endif
Define USE_MOCK_CLASSE S when you want to do the testing,
that is, change your make file to do it automatically
So you end up testing something different than what you deliver?
Not a good idea, by any means.
I've tried to get around this by not building an object
file for class A, but using A.obj which has already been
built, but linking it against my mock versions of B.obj
and C.obj. This would work, except that C.h defines
C::~C(), and so A.obj ends up with a definition of C::~C()
in it, and I get multiply defined symbol link errors. I
could make sure all method definitions are in cpp files,
but often having a getter or setter inline is perfectly
sensible.
How do people get around these issues of mocking out
classes effectively from a build system point of view?
I'd rather not have to change the original source code just to
accommodate mocking for unit tests. Is that actually what most people
end up doing?
It seems a bit of an 'orrible fix to me.
It is.

It's not too clear to me what your deliverables are, but
generally, you should organize your directory structure so that
everything in a given directory can be tested together. If you
need a mock something, then that something should normally be
from a different directory, and not part of the code being
developed in the same directory.

If you need mock object (which you should avoid if
possible---but it isn't always possible on larger projects), you
should arrange for them at the start, putting them in a separate
directory. If the mock objects don't contain any templates or
inline functions, you use the standard header for it, and link
against a locally defined mock library. If B contains templates
or inline functions (both things to be avoided as much as
possible exactly for these sort of reasons), you're probably
stuck with having to compile against a different header file as
well (again, in the mock directory), but beware that this
seriously affects the reliability of your tests, in a negative
way.

To pick up the mock objects, of course, you just add the
appropriate -L (and -I, if you can't avoid it) in the compiler
command (before the other -L or -I).

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 27 '08 #6


James Kanze wrote:
It's not too clear to me what your deliverables are, but
generally, you should organize your directory structure so that
everything in a given directory can be tested together. If you
need a mock something, then that something should normally be
from a different directory, and not part of the code being
developed in the same directory.
Hmm, I'd come to think that I wanted to mock everything around the
class under test. You seem to be saying mock around the system that
includes the class under test, unless you have to do otherwise.

This is where the unit test/integration test line blurs ...

Say then that classes B and C interface into someLib::D, you'd provide
a mock of D, and use test hooks from that?
Jun 27 '08 #7
earthwormgaz wrote:
I'd rather not have to change the original source code just to
accommodate mocking for unit tests. Is that actually what most people
end up doing?
I try not to but sometimes it's the only way. It certainly points to
strong coupling when you have to trick your source into allowing a mock,
and maybe you want to do something about that, but sometimes that's just
the way it is.

You can't write perfect code. You shouldn't expect it and you shouldn't
try. Just do the best you can under the conditions you find yourself in.
Jun 27 '08 #8
earthwormgaz wrote:
>
James Kanze wrote:
>It's not too clear to me what your deliverables are, but
generally, you should organize your directory structure so that
everything in a given directory can be tested together. If you
need a mock something, then that something should normally be
from a different directory, and not part of the code being
developed in the same directory.

Hmm, I'd come to think that I wanted to mock everything around the
class under test. You seem to be saying mock around the system that
includes the class under test, unless you have to do otherwise.
I don't think the person you are replying to fully understands the
purpose of unit tests. You definitely do want to test each unit
individually; there should be a test for each public class at the least.
This often involves mock objects.
Jun 27 '08 #9
On Jun 24, 5:53 pm, Noah Roberts <u...@example.n etwrote:
earthwormgaz wrote:
James Kanze wrote:
It's not too clear to me what your deliverables are, but
generally, you should organize your directory structure so that
everything in a given directory can be tested together. If you
need a mock something, then that something should normally be
from a different directory, and not part of the code being
developed in the same directory.
Hmm, I'd come to think that I wanted to mock everything around the
class under test. You seem to be saying mock around the system that
includes the class under test, unless you have to do otherwise.
I don't think the person you are replying to fully understands the
purpose of unit tests.
That from you?
You definitely do want to test each unit individually; there
should be a test for each public class at the least.
Hmmm, the name is "unit test", not class test. You want to test
each unit more or less individually. On the other hand, you can
generally consider lower level stuff as "part of the system".
(I never try to mock std::vector, for example. I just suppose
that it works.)
This often involves mock objects.
Not if your code is well designed and well engineered. It
sometimes involves mock objects, but I find that with good
design, it this isn't the case that frequently.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jun 27 '08 #10

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

Similar topics

4
3732
by: Hugh Cowan | last post by:
Hello, I don't program full-time (anymore), but I do try and stay on-top of the latest technologies and like most are always trying to upgrade my skills and remain current (as much as is possible). Most of my programming these days involves using PHP for creating script files for automating tasks and procedures (locally), and also for anything that might be needed by our divisional Intranet (not a huge site by any stretch of the...
11
2320
by: rhat | last post by:
Hi Everyone, I've recently been reading some articles about unit-testing in Python , but I am a bit confused: where do I go to get started with this? I tried googling for "unittest" but all I've found are some old links to projects that already use it, and the older (as the articles put it) PyUnit project. I'm sorry if this is a obvious question or one that has already been answered, but unit-testing sounds interesting and I'm not sure...
38
3388
by: Christoph Zwerschke | last post by:
In August 2001, there was a thread about the "Art of Unit Testing": http://groups.google.com/group/comp.lang.python/browse_frm/thread/aa2bd17e7f995d05/71a29faf0a0485d5 Paul Moore asked the legitimate question why there is no hook for a "global" fixture code that is run only once for the whole TestCase, as opposed to the normal "setUp" and "tearDown" code that is run for every single test in the TestCase. A "global fixture" would be...
14
2737
by: | last post by:
Hi! I'm looking for unit-testing tools for .NET. Somthing like Java has --> http://www.junit.org regards, gicio
11
2291
by: D. Yates | last post by:
Hi, In an effort to improve my own code and try to teach others that I work with that unit testing IS MANDATORY. I'm interested in examples of unit testing that other people are using. How many folks rely on NUNIT? Do you create a testing class per application, per class, per assembly to exercise your code?
4
2159
by: Peter Rilling | last post by:
Does VS.NET 2005 Professional support integrated unit testing, or is that only with the team system?
72
5226
by: Jacob | last post by:
I have compiled a set og unit testing recommendations based on my own experience on the concept. Feedback and suggestions for improvements are appreciated: http://geosoft.no/development/unittesting.html Thanks.
7
1872
by: Diffident | last post by:
Hello All, Can anyone please suggest me a good unit testing tool. I have seen NUnit but not sure on how I can use it to test my methods which involve session variables, viewstate variables, textbox values. I understand that NUnit is more suitable for OO methods which take set of parameters and return an output parameter. How about tools for testing methods which use state variables and form values?
4
18962
by: Dat AU DUONG | last post by:
Hi, I am new to Unit testing, could you tell me where I could find information (hopefully step by step) and what is the benefit of unit testing. I am a sole developer in a company, therefore I don't get expose to much of this technology. Thanks in advance. Regards Dat.
5
2239
by: Ben Finney | last post by:
Howdy all, PEP 299 <URL:http://www.python.org/dev/peps/pep-0299details an enhancement for entry points to Python programs: a module attribute (named '__main__') that will be automatically called if the module is run as a program. The PEP has status "Rejected", citing backward-compatibility issues, and Guido's pronouncement that "It's not worth the change (in docs, user habits, etc.) and there's nothing particularly broken."
0
8234
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
8620
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
8335
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,...
0
8474
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7158
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4174
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2605
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
1
1784
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1482
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.