Boost logo

Boost Users :

Subject: Re: [Boost-users] Recommended mock framework to use with Boost?
From: Peter Goetz (peter.gtz_at_[hidden])
Date: 2011-01-07 13:42:26


2011/1/5 doug livesey <biot023_at_[hidden]>:
> Hi -- I'm trying to learn to develop properly with C++ and Boost, and to
> that end am learning along with Boost.Test, as I'm a BDD coder in another
> language (Ruby) in my day job.
> I'm at the point, now, where my tests could really benefit from a mocking
> framework. There doesn't appear to be one in Boost (not documented, at
> least), so I wondered if anyone on the list could recommend a mocking
> framework that they use that works well with Boost.Test?
> Any and all advice gratefully received,
>    Doug.
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users
>

Hi,

I'm using Google Mock. It's quite powerful and in my opinion very
clear. But you have to write some glue code to make it work with Boost
Test. Something like this:

class BoostTestAdapter: public EmptyTestEventListener {

virtual void OnTestPartResult(const TestPartResult& testPartResult);
{
    if (testPartResult.failed()) {
        std::stringstream s;
        if (testPartResult.file_name())
        {
            s << std::endl
                << testPartResult.file_name()
                <<"(" << testPartResult.line_number() << "): "
                << testPartResult.summary();
        }
        else
        {
            s << std::endl
                << "unknown file: "
                << testPartResult.summary();
        }
        BOOST_ERROR(s.str());
    }
}

};

struct GoogleMockFixture {

GoogleMockFixture::GoogleMockFixture()
{
    InitGoogleMock(&boost::unit_test::framework::master_test_suite().argc,
        boost::unit_test::framework::master_test_suite().argv);
    TestEventListeners &listeners = UnitTest::GetInstance()->listeners();
    delete listeners.Release(listeners.default_result_printer());
    listeners.Append(new BoostTestAdapter);
}

~GoogleMockFixture() {}

};

BOOST_GLOBAL_FIXTURE(GoogleMockFixture)

The error reporting is not perfect yet, but usually you get the source
file and line of the error directly in the error message.

Hope that helps!

Peter


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