Luq
Luq

Reputation: 67

How to run a function after all test cases BOOST c++

I have a problem, I am pondering; if there are any macros, features in C++ BOOST library, that will run a function once after ALL test cases.

I want to run a function which will be creating a json file with no. of executed tests, no. of passed test and no. of all tests. So i have that problem, because i MUST work with file that is build in specific way:

f1()
f2()
f3()
BOOST_AUTO_TEST_CASE(f1){}
BOOST_AUTO_TEST_CASE(f2){}
BOOST_AUTO_TEST_CASE(f3){}

So I cannot create main() function which would do that for me, because im getting an error that there is a previous declaration in /boost/test/unit_test.hpp.

Thank you in advance. Greetings.

Upvotes: 2

Views: 1565

Answers (2)

pepr
pepr

Reputation: 20762

OT: This may be off-topic as the question explicitly mentioned the BOOST library.

My personal experience is that there are unit-testing frameworks for C++ that are actually more suitable for the C++ language (taking advantage of the C++ language itself; not blindly copying the old unit-testing framework design), easier to configure (just copying one file with the full implementation; no external dependencies), and easier to use.

Have a look at the Catch unit-testing framework. It also supports nicely the Test-Driven Development, and the Behavior-Driven Development.

(For example, the nanodbc project recently switched from the BOOST to Catch after some evaluation. The existing tests can be rewritten almost mechanically -- replacements via editor tools.)

Upvotes: 0

Dan Mašek
Dan Mašek

Reputation: 19041

There are at least two possible approaches to this problem.

The first would be to use a fixture, or more specifically a global fixture.

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

struct Fixture {
  Fixture()   { /* Run on startup */ }
  ~Fixture()  { /* Run on tear down */ }
};

BOOST_GLOBAL_FIXTURE(Fixture);

BOOST_AUTO_TEST_CASE(test_case)
// ... and so on

The other option would be to use the functionality provided in the library for overriding the entry point, so that you can use your own main() as you attempted.

Upvotes: 5

Related Questions