4

I have a suite of tests that can be run in a few different modes. Other than some global configuration, or a fixture config, the test case code is the same.

Is there some way in the boost test library to achieve this without having to write a wrapper around all the individual test cases?

Note this is not to be a command-line switch, it should be part of the same execution.

edA-qa mort-ora-y
  • 30,295
  • 39
  • 137
  • 267

1 Answers1

4

The unary function test case is probably what you want. The only downside is that automatic registration (could be based on some kind of factory function) does not seem to be supported for it.

They also have test case template and that does have automatic registration, so it would be possible to abuse it by defining type for each configuration if there's not too many of them.

Edit: The test case template could be used something like this:

// Parameter is the type of parameter you need. Might be anything from simple int (in
// which case the template parameter may be a value, not reference) to complex object.
// It just has to be possible to create (static) global instances of it.

template <const Parameter &param>
struct Fixture {
    // do whatever you want, param is normal object reference here
    // it's not a member, but you can:
    const Parameter &getParameter() { return param; }
}

static Parameter p1(whatever);
static Parameter p2(something_else);
// ...

typedef boost::mpl::list<Fixture<p1>, Fixture<p2> > Fixtures;

BOOST_AUTO_TEST_CASE_TEMPLATE(test, F, Fixtures)
{
    F fixture; // Unfortunately you can't make it true fixture, so you have to have instance
    // Test what you want
}
Jan Hudec
  • 73,652
  • 13
  • 125
  • 172
  • These don't quite achieve what I want, since the parameter affects everything inside the test suite. I'd still have to write wrappers. Though the documentation for the template test is so minimal I can't say if it would work. – edA-qa mort-ora-y Jun 21 '11 at 14:45
  • Well, the template allows automatic registration, so there is less code to write, but the test cases would still need to explicitly use template parameter (similar to manually using a fixture by constructing local instance). – Jan Hudec Jun 22 '11 at 06:25
  • This might actually work. Using a parameter inside the test case is not an issue. The test hierarchy is not ideal (I'd like two suite runs, not each test duplicated) but I'll see how it works. – edA-qa mort-ora-y Jun 22 '11 at 10:29