Steve
Steve

Reputation: 6424

Struggling getting Boost library unit test framework to work

I've successfully built the Boost libraries using VS2013. I've also successfully included and used some of Boost.Filesystem and Boost.Log stuff. However, I'm struggling to get the following unit test to work in Visual Studio 2013:

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

class Multiplier {
public:
    explicit Multiplier(int i) : _value{ i } {}
    int multiply(int i) { return _value * i; }
private:
    int _value;
};

BOOST_AUTO_TEST_CASE(everything_test) {
    Multiplier m{ 5 };
    BOOST_CHECK_EQUAL(m.multiply(2), 10);
}

This obviously isn't a real unit test, but that's not the point... :)

By including boost/test/included/unit_text.hpp, I should be getting a main() function supplied for me, and it appears I am since I was able to get that error resolved. When I run my resulting executable, though, I get an Access violation reading location 0x00000000.

Am I supposed to run the executable to run the tests? If not, how do I run them? Running the tests seems like such a simple operation that would be obviously evident from the documentation, but I must be missing it.

Upvotes: 2

Views: 965

Answers (2)

serup
serup

Reputation: 3822

First make sure that you have linkage info in your makefile; example :

-lboost_system -lboost_log -lboost_signals -lboost_thread -lboost_filesystem -lboost_regex 

of course only add the boost which is needed for your specific testcase suite

Then in your test suite file have following:

#define BOOST_TEST_MODULE YourTestSuiteName                  
#define BOOST_TEST_DYN_LINK                            
#include <boost/test/unit_test.hpp>      
#include <boost/test/results_reporter.hpp>             
#define BOOST_AUTO_TEST_MAIN 

#ifndef NOTESTRESULTFILE                                                           
#ifdef BOOST_AUTO_TEST_MAIN                                                        
std::ofstream out;                                                                 

struct ReportRedirector                                                            
{                                                                                  
    ReportRedirector()                                                             
    {                                                                              
        out.open("test_results.xml");                                              
        assert( out.is_open() );                                                   
        boost::unit_test::results_reporter::set_stream(out);                       
    }                                                                              
};                                                                                 

BOOST_GLOBAL_FIXTURE(ReportRedirector)                                             
#endif                                                                             
#endif   

BOOST_AUTO_TEST_SUITE (YourTestSuiteName)            
BOOST_AUTO_TEST_SUITE_END( )                                            

BOOST_AUTO_TEST_CASE(YourTestCaseName)                                
{                                                                             
   cout<<"BOOST_AUTO_TEST_CASE( YourTestCaseName )\n{"<<endl;         

   BOOST_CHECK(false == true);  //TODO: testcase not finished  

   cout<<"}"<<endl;                  
}                                     

This setup works fine for me, but I am sure you can setup boost unit tests suites in a different way

Upvotes: 1

Dale Wilson
Dale Wilson

Reputation: 9434

Use

#include <boost/test/unit_test.hpp>

rather than

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

main is defined in the test/unit_test.hpp file. "included" is an implementation detail.

Upvotes: 1

Related Questions