Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Command line args in boost test

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. **

like image 681
Edward Strange Avatar asked Dec 17 '12 19:12

Edward Strange


People also ask

Is it possible to pass command line arguments to a test?

It is possible to pass custom command line arguments to the test module.

What ARG means in command line argument?

argc:- It is known as argument count. It is int. It stores the number of the command line arguments passed by a user from the terminal and also stores the name of the program. The value of argc must not be negative. argv:- It is a pointer array and it points to every argument which is being passed to the program.

What is command line arguments in oops?

A command-line argument is an information that directly follows the program's name on the command line when it is executed. To access the command-line arguments inside a Java program is quite easy. They are stored as strings in the String array passed to main( ).

What is command line arguments give example?

Command line arguments allow the user to affect the operation of an application. For example, an application might allow the user to specify verbose mode--that is, specify that the application display a lot of trace information--with the command line argument -verbose .


2 Answers

have a look at the master test suite. is

boost::unit_test::framework::master_test_suite().argc
boost::unit_test::framework::master_test_suite().argv

what you want?

like image 172
stefan Avatar answered Sep 20 '22 20:09

stefan


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!!");
}
like image 36
Raydel Miranda Avatar answered Sep 17 '22 20:09

Raydel Miranda