2 minutes
Using googlemock with the Boost Test Library
googlemock is a framework that allows generating mock objects for unit tests. I’ve decided to use this framework as it looks wells designed and maintained. The default is to use googlemock with Google’s unit testing framework googletest, but as I was already using the Boost Testing Library, I was searching for a solution to use googlemock in Boost Test. Fortunately this isn’t to hard to accomplish. You only have to create a TestEventListener that forwards all results supplied by googletest to the Boost Testing Framework:
#include <sstream>
#include <boost/test/included/unit_test.hpp>
#include <gtest/gtest.h>
#include <gmock/gmock.h>
using namespace std;
using namespace testing;
class BoostTestAdapter: public EmptyTestEventListener {
virtual void OnTestStart(const TestInfo& /*testInfo*/) {
}
virtual void OnTestPartResult(const TestPartResult& testPartResult) {
if (testPartResult.failed()) {
stringstream s;
s << "Mock test failed (file = '"
<< testPartResult.file_name()
<< "', line = "
<< testPartResult.line_number()
<< "): "
<< testPartResult.summary();
BOOST_FAIL(s.str());
}
}
virtual void OnTestEnd(const ::testing::TestInfo& /*testInfo*/) {
}
};
Every time a partial test result of googletest is reported that fails, BOOST_FAIL
is called.
The only thing that is left now is to install the newly created adapter in the googletest framework and initialize it properly. This can be done using a fixture class in Boost Test:
class TestFixture {
public:
TestFixture() {
InitGoogleMock(
&boost::unit_test::framework::master_test_suite().argc,
boost::unit_test::framework::master_test_suite().argv);
TestEventListeners &listeners = UnitTest::GetInstance()->listeners();
// this removes the default error printer
delete listeners.Release(listeners.default_result_printer());
listeners.Append(new BoostTestAdapter);
}
~TestFixture() {
// nothing to tear down
}
};
BOOST_GLOBAL_FIXTURE(TestFixture)
boost boost.test C/C++ google googlemock Unit Testing work-planet
238 Words
2010-04-13 00:00 (Last updated: 2021-04-30 20:40)