Sign in to follow this  
maximAL

Boost.test - howto?

Recommended Posts

i wanted to use unit tests for a library i'm just writing and i decided for boost.test mostly because it has a good reputation and will most likely still be supported in a few years (as my project is supposed to be very long-lived). but unfortunately i already run into problems just implementing the most basic examples. eg.:
#include <my_class.hpp>
#define BOOST_TEST_MODULE MyTest
#include <boost/test/unit_test.hpp>

BOOST_AUTO_TEST_CASE( my_test )
{
    my_class test_object( "qwerty" );

    BOOST_CHECK( test_object.is_valid() );
}

this won't link, because main is undefined (i dynamically linked the lib). now, of course i know there must be a main somewhere. but on several occasions the doc mentions it's automatically produced by boost.test. when i include <boost/test/included/unit_test.hpp> instead, it works. the doc also mentions function like unit_test_main and what not, you may have to define yourself but all in all it's still completely unclear to me, how the whole system works and what i really need to get anything up and running. disappointingly, the doc seems mostly incomplete, with many not-working examples, dead links/images...and many typos. now, since this is a boost lib where talking about here, it's probably my fault. i hope. otherwise i might resort to CxxTest. that seems to work ootb like a charm. but the last version is already a few years old and who knows if there will be an update someday (eg. to adept to the new C++ standard).

Share this post


Link to post
Share on other sites
I had a similar problem IIRC, but never got round to fixing it. I'm fairly sure the problem started with boost 1.35 and that it used to work with 1.34, so it may be worth trying an older version of the lib.

Share this post


Link to post
Share on other sites
I poked around with boost.test a few days ago too. Unfortunately, I don't have the code here, so I can't say exactly how I got it working. I think you're using the wrong macro though.

BOOST_TEST_MODULE only defines *a* module, iirc. There's another macro for defining the *main* module (which generates a main function)

Also, the docs actually exist, and are pretty much up to date. They just seem to have *also* kept a ton of old, outdated docs online as well. I spent a few hours trying to figure out these old docs, before I stumbled across the "proper" version. But once you find the proper docs, it works very well.

If I remember, I'll post my code when I get home. And a link to the proper documentation. :)

In any case, once you get past these problems, I think it's a really nice framework.

Share this post


Link to post
Share on other sites
Quote:
Original post by donny dont
Are you using bjam to build or visual studio?

for an example as basic as that one, a command line call to g++ should be enough ;)

Quote:
Original post by donny Spoonbender
BOOST_TEST_MODULE only defines *a* module, iirc. There's another macro for defining the *main* module (which generates a main function)

there also is the BOOST_TEST_MAIN variable, but that doesn't change anything.

Share this post


Link to post
Share on other sites
This minimal example compiles fine for me:


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

BOOST_AUTO_TEST_CASE( my_test )
{
BOOST_CHECK( 1 == 1 );
}



and I *think* that if you use the docs located here, they should be up to date, and the links to source code examples should actually work. (If you google for the docs, you most likely end up with the wrong version, one with lots of dead links)

Share this post


Link to post
Share on other sites
unfortunately, this also doesn't work for me. but i figured out that i have version 1.34 instead of the lastest 1.36 installed (i just took what my package manager spit out). maybe i try to build 1.36 on my own...

Share this post


Link to post
Share on other sites
It isn't too painful, just can take quite a bit of waiting, to compile boost on linux. Though Boost.Test is somewhat of a header only library so you can get away with not doing the actual compile.

http://www.boost.org/doc/libs/1_36_0/more/getting_started/unix-variants.html

That should get you started up.

Share this post


Link to post
Share on other sites
so, even with the latest version it still won't work, but the error messages differ this time.
now i get compiler errors because "init_unit_test_func" is undefined. i had a look at the source and found this in framework.hpp:
#ifdef BOOST_TEST_ALTERNATIVE_INIT_API
typedef bool (*init_unit_test_func)();
#else
typedef test_suite* (*init_unit_test_func)( int, char* [] );
#endif


defining BOOST_TEST_ALTERNATIVE_INIT_API doesn't help. but copying the first typedef in my test program does - in contrast to the second typedef.
really, wtf?

Share this post


Link to post
Share on other sites
What exactly does your test program look like atm?
In particular, how are you including/linking to boost.test? And do you have any relevant #defines?

Depending on whether you use statically or dynamically linked version (or the header-only one) of boost.test, it uses different signatures for the initialization function. (In both cases, you shouldn't need to declare it yourself though, but the BOOST_TEST_ALTERNATIVE_INIT_API
macro is used to override the default init function signature.

Which compiler are you using, btw?

Share this post


Link to post
Share on other sites
code:

#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_ALTERNATIVE_INIT_API
#define BOOST_TEST_MODULE MyTest
#include </usr/local/include/boost-1_36/boost/test/unit_test.hpp>

BOOST_AUTO_TEST_CASE( my_test )
{
BOOST_CHECK( 1 == 1 );
}



i'm using g++ 4.2...
Quote:

g++ test00.cpp -o test00 -lboost_unit_test_framework-gcc42-mt-1_36
In file included from test00.cpp:4:
/usr/local/include/boost-1_36/boost/test/unit_test.hpp:46: error: 'init_unit_test_func' was not declared in this scope
/usr/local/include/boost-1_36/boost/test/unit_test.hpp:46: error: expected primary-expression before 'int'
/usr/local/include/boost-1_36/boost/test/unit_test.hpp:46: error: expected primary-expression before 'char'
/usr/local/include/boost-1_36/boost/test/unit_test.hpp:46: error: initializer expression list treated as compound expression
/usr/local/include/boost-1_36/boost/test/unit_test.hpp: In function 'int main(int, char**)':
/usr/local/include/boost-1_36/boost/test/unit_test.hpp:59: error: 'boost::unit_test::unit_test_main' cannot be used as a function

Share this post


Link to post
Share on other sites
Any particular reason for the first two #defines?
(In particular, I'm pretty sure the "alternative init API" is default when using dynamic linking, but also, is there a reason why you use dynamic linking at all, and does it work without it?)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this