Boost logo

Boost Users :

Subject: Re: [Boost-users] whitebox testing with boost unit test framework?
From: Chris Cleeland (chris.cleeland_at_[hidden])
Date: 2011-12-22 14:39:03


On Thu, Dec 22, 2011 at 1:15 PM, Ahmed Badran <ahmed.badran_at_[hidden]> wrote:
> In your header (the class to be tested, you need to forward declare the test
> namespace and the tester class, e.g.
>
> // ClassToBeTested.h
>
> // You may be able to generate the forward declaration using a script and
> put it in an include file that you
> // include here.
> namespace test
> {
>     class TesterOfClassToBeTested;
> }
>
> namespace myOtherNamespace { // optional
>     class ClassToBeTested
>     {
>         friend class test::TesterOfClassToBeTested;   // here's the one line
> :)
>         public:
>         private:
>     };
> }

I had done that. But I think I've figured out my problem.

> // TesterOfClassToBeTested.cpp
>
> #include "ClassToBeTested.h"
>
> #define foreach BOOST_FOREACH
>
> using namespace std;
> using namespace boost;
> using namespace stage;
>
> namespace test
> {
>     class TesterOfClassToBeTested
>     {
>         public:
>         TesterOfClassToBeTested()
>         {
>         }
>         void copy_constructor()
>         {
>             ClassToBeTested loc2;
>             ClassToBeTested loc(loc2);
>             BOOST_REQUIRE(loc.locIdStruct == loc2.locIdStruct);
>         }
>         ~TesterOfClassToBeTested()
>         {
>         }
>         private:
>     };
> }
>
> BOOST_AUTO_TEST_SUITE(TesterOfClassToBeTested)
>
> struct fixture
> {
>     test::TesterOfClassToBeTested t;
> };
>
>
> BOOST_FIXTURE_TEST_CASE(copy_constructor, fixture)
> {
>     t.copy_constructor();
> }
>
> BOOST_AUTO_TEST_SUITE_END()

What you've got above is a different usage of Boost::Unit than what I
have. In my code, I have something like

struct ClassToBeTested_suite_fixture { ... };

BOOST_FIXTURE_TEST_SUITE( ClassToBeTested_suite, ClassToBeTested_suite_fixture )
BOOST_AUTO_TEST_CASE( case1 )
{
  // code to do testing
}
BOOST_AUTO_TEST_CASE( case2 )
{
  // code to test a different case
}
BOOST_AUTO_TEST_SUITE_END()

I designated friendship to ClassToBeTested_suite_fixture, but was
trying to access the private data members in 'case1', 'case2', etc.
That doesn't work because even though the cases inherit from the
fixture, friendship does not get inherited. Indeed, when I went back
to the header for ClassToBeTested and added a friend designation for
ClassToBeTested::case1, it compiled just fine.

I am a n00b to boost's unit test framework. I stumbled upon some
examples that used the construct that I have been using, and they've
worked just fine up until this case. Now that I have a need, I will
have to learn just a little more. "Extreme Learning" :)

Thanks for showing me a way that works for you. I'm certain it would
have taken me a lot longer to figure that out to the point where I may
have simply reverted to preprocessor tricks of "#define
(protected|private) public" in the test case code.

-- 
Chris Cleeland

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