Re: Unit Testing in C++
On Jun 24, 3:14 pm, earthwormgaz <earthworm...@googlemail.com> wrote:
On 24 Jun, 13:19, anon <a...@no.no> wrote:
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_CLASSES
#include "Bmode.h"
#else
#include "B.h"
#endif
Define USE_MOCK_CLASSES 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:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34