Boost logo

Boost Users :

Subject: Re: [Boost-users] How to isolate a class under test?
From: Richard (legalize+jeeves_at_[hidden])
Date: 2010-06-10 12:36:20


[Please do not mail me a copy of your followup]

boost-users_at_[hidden] spake the secret code
<20100609080130.21006u389wm3wotm_at_[hidden]> thusly:

>I am testing class A. However, class A makes some calls to class B.
>Does Boost.Test provide any "test harness" capability so that the
>responses from class B can be canned. I really only want to test
>class A, not A and B together. I hope the question makes sense.

You need to decouple the classes somehow. The easiest way is to use
an interface to separate them, in C++ terms a pure virtual base class
for B. In my 5-part tutorial on using Boost.Test, you can see the use
of an interface to decouple a class from its collaborator in part 4.
<http://legalizeadulthood.wordpress.com/2009/07/05/c-unit-tests-with-boost-test-part-4/>

There are lots of ways to decouple two collaborators, but I like
"Extract Interface" the best. If for some reason your code is too
performance sensitive for an interface sitting in the middle, you can
decouple the collaborators with templates. I give an example of how
to decouple your code from the Win32 API using templates here:
<http://legalizeadulthood.wordpress.com/2007/12/17/faking-out-the-win32-api-with-templates/>

The book "Working Effectively With Legacy Code" provides lots of good
ideas on how to decouple your system under test from its dependencies.
I wrote a review of it here:
<http://legalizeadulthood.wordpress.com/2007/04/11/working-effectively-with-legacy-code-by-michael-c-feathers/>

-- 
"The Direct3D Graphics Pipeline" -- DirectX 9 draft available for download
 <http://legalizeadulthood.wordpress.com/the-direct3d-graphics-pipeline/>
      Legalize Adulthood! <http://legalizeadulthood.wordpress.com>

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net