[Boost-users] [UTF] Accessing global fixture members within tests

910 views
Skip to first unread message

Simon Pickles

unread,
Jan 2, 2010, 10:42:38 AM1/2/10
to boost...@lists.boost.org
Hi

I am using the Unit Test Framework to test my use of boost.python. I am
using a global fixture to initialize the python intepreter a single time
(rather than before each test).

My problem is I would like to use the Python initialising class in my
tests, and I can't figure out how to do this. I don't want to use normal
fixtures since each test would call initialize the interpreter, causing
problems.

Here is what I want to do:

//////////////////////////////////////////////////////
#include <boost//python.hpp>
#include <boost//test//unit_test.hpp>

#include "..//pythonManager//pythonManager.h"

using namespace boost;

// First make a global fixture which sets up python interpreter
struct PythonManagerFixture
{
PythonManagerFixture()
{
pm.Init();
}
PythonManager pm;
};

// Set up suite
BOOST_AUTO_TEST_SUITE(TestPythonManager)

// Only init python interpreter once
BOOST_GLOBAL_FIXTURE(PythonManagerFixture);

BOOST_AUTO_TEST_CASE(TestInit)
{
// Get python globals dict
python::dict globalDict =
python::extract<python::dict>(pm.m_pyGlobals); // COMPILE ERROR 'pm' :
undeclared identifier
// Has logging module been imported?
BOOST_REQUIRE(globalDict.has_key("logging"));
}

BOOST_FIXTURE_TEST_CASE(TestLogCallback, PythonManagerFixture) //
RUNTIME ERROR pm.Init called a second time - bad
{
function<void(const string&, const string&)> f =
pm.GetLogFunction(); // pm is in scope though....
BOOST_REQUIRE(f);
}

// End the suite
BOOST_AUTO_TEST_SUITE_END()
///////////////////////////////////////////////////////////////////

I need a way to supply the PythonManager instance, pm, to the test cases.

Thanks for any advice

Simon
_______________________________________________
Boost-users mailing list
Boost...@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users

Simon Pickles

unread,
Jan 2, 2010, 11:14:04 AM1/2/10
to boost...@lists.boost.org
Ah, I needed the subtle distinction of a BOOST_FIXTURE_TEST_SUITE, not a
BOOST_FIXTURE_TEST_CASE

Simon Pickles

unread,
Jan 2, 2010, 11:18:24 AM1/2/10
to boost...@lists.boost.org
Alas, I am mistaken. BOOST_FIXTURE_TEST_SUITE is simply a convenience
macro, removing the need to add a fixture to each test case.

What I need is a way to perform the fixture setup ONCE, yet allow test
cases access to the public members

Gennadiy Rozental

unread,
Jan 3, 2010, 7:31:29 PM1/3/10
to boost...@lists.boost.org
Simon Pickles <sipickles <at> googlemail.com> writes:

>
> Simon Pickles wrote:
> > Simon Pickles wrote:
> >> Hi
> >>
> >> I am using the Unit Test Framework to test my use of boost.python. I
> >> am using a global fixture to initialize the python intepreter a
> >> single time (rather than before each test).
> >>
> >> My problem is I would like to use the Python initialising class in my
> >> tests, and I can't figure out how to do this. I don't want to use
> >> normal fixtures since each test would call initialize the
> >> interpreter, causing problems.

Boost.Test does not provide means to do this directly. You can alsays mimic some
kind of singleton though:

struct GlobalFixure {
GlobalFixure*& instance() { static GlobalFixure* s_inst = 0; return s_inst; }

GlobalFixure()
{
instance() = this;
...
}
};


...

GlobalFixure::instance().m_member.do_something();

Gennadiy

Reply all
Reply to author
Forward
0 new messages