1

I've got a lot of Boost unit tests. I can't find the place where I should put my signal handler. There's no main() function in the files in unit tests directory. It seems that main() is hidden in some macros.

In unit_test.hpp I see:

namespace boost { namespace unit_test {

int BOOST_TEST_DECL unit_test_main( init_unit_test_func init_func, int argc, char* argv[] );

}

But how can I implement my own main() function, to be able to set a signal handler there?

Toby Speight
  • 27,591
  • 48
  • 66
  • 103
JimmyY
  • 11
  • 3
  • 1
    You can disable the main function generation and provide your own one. An example is [here](http://www.boost.org/doc/libs/1_63_0/libs/test/doc/html/boost_test/utf_reference/link_references/link_boost_test_no_main.html) – mkaes Feb 01 '17 at 10:31
  • Thanks for the answer! – JimmyY Feb 01 '17 at 11:56

1 Answers1

0

As per the instructions here, you can provide your own main function to set the signal handler in. Here is some code adopted for this:

#define BOOST_TEST_MODULE custom_main
#define BOOST_TEST_NO_MAIN
#define BOOST_TEST_ALTERNATIVE_INIT_API
#include <boost/test/included/unit_test.hpp>
#include <iostream>

#include <unistd.h>
#include <signal.h>

void signalHandler(int sig)
{
    std::cerr << "Inside signal handler" << std::endl;
}

namespace utf = boost::unit_test;

BOOST_AUTO_TEST_CASE(test1)
{
  BOOST_TEST(false);
}

int main(int argc, char* argv[], char* envp[])
{
    signal(SIGINT, signalHandler);
    signal(SIGTERM, signalHandler);
    return utf::unit_test_main(init_unit_test, argc, argv);
}
Smeeheey
  • 9,906
  • 23
  • 39
  • Maybe you know how to force boost unit tests output names of all tests it runs? Google unittests have options -v. But it doesn't work for boost unittests. Thx. – JimmyY Feb 02 '17 at 08:53