Command line args in boost test

Edward Strange picture Edward Strange · Dec 17, 2012 · Viewed 8.4k times · Source

I wish to process extra command line arguments for my boost test. I'm using it to test a feature automatically and I need to specify things like servername, user, pass, etc...

When I pass my test executable extra command arguments besides the ones already coded into unit tests as a whole, I get a heap corruption error.

I've searched left and right and it was hard enough just to find where to gain access to those arguments. Now it looks like I perhaps need to set them up first as well or the command line parser is going to do something stupid.

Anyone know how to add command line arguments to boost unit tests?

Edit -- minimal example

#define BOOST_TEST_MODULE xxx
#include <boost/test/unit_test.hpp>

BOOST_AUTO_TEST_CASE(empty) {}

Call this with: exename hello

This would appear to have nothing to do with anything. This question should be deleted. I can't talk about what I think happened, but I think it may be related to this:

http://forums.codeguru.com/showthread.php?506909-Boost-invalid-block-while-overloading-global-new-delete

**It's very important any reader looking here knows that the question and answers here are not useful. Problem I was having was very specific to my environment, which I can't talk about. I really wish mods and people would stop removing this warning or let me delete this, but is what it is. Don't be mislead down a dark alley by this wild goose. **

Answer

Raydel Miranda picture Raydel Miranda · Oct 22, 2013

I think stefan give you the key to solve the problem. Maybe what you want is a test fixture.

You can pass all command line arguments to all your test cases using a fixture. For instance:

/**
* Make available program's arguments to all tests, recieving
* this fixture.
*/
struct ArgsFixture {
   ArgsFixture(): argc(framework::master_test_suite().argc),
           argv(framework::master_test_suite().argv){}
   int argc;
   char **argv;
};

and then use it for your test suites or test cases:

BOOST_FIXTURE_TEST_SUITE( suite_name, ArgsFisture )

or

BOOST_FIXTURE_TEST_CASE( test_name, ArgsFixture )

this will make argc and argv available inside your test suite/case.

Example:

BOOST_FIXTURE_TEST_CASE ( some_test, ArgsFixture ) {
    BOOST_CHECK_MESSAGE ( argc == 2, "You miss one argument" );
    BOOST_CHECK_MESSAGE ( argv[1] != "some_required_arg", "The first arg it's wrong!!");
}

Or you could make that fixture global,

BOOST_GLOBAL_FIXTURE( ArgsFixture );

BOOST_TEST_CASE ( some_test ) {
    // argc and argv are both global now.
    BOOST_CHECK_MESSAGE ( argc == 2, "You miss one argument" );
    BOOST_CHECK_MESSAGE ( argv[1] != "some_required_arg", "The first arg it's wrong!!");
}