I am trying to use global fixtures in my tests but I may be misinterpreting the documentations. The following toy example doesn't work :
#include <boost/test/unit_test.hpp>
struct TruthFixture {
int x = 3;
int f (int a) {
return a + 3;
}
};
BOOST_GLOBAL_FIXTURE( TruthFixture )
BOOST_AUTO_TEST_CASE(test_variable) {
printf("x = %d\n", x);
}
BOOST_AUTO_TEST_CASE(test_function) {
printf("f(2) = %d\n", f(2));
}
the error message I get is the following:
test/test.cpp:14:22: error: use of undeclared identifier 'x'; did you mean 'TruthFixture::x'?
printf("x = %d\n", x);
^
TruthFixture::x
test/test.cpp:4:7: note: 'TruthFixture::x' declared here
int x = 3;
^
test/test.cpp:14:22: error: invalid use of non-static data member 'x'
printf("x = %d\n", x);
^
test/test.cpp:18:25: error: use of undeclared identifier 'f'; did you mean 'TruthFixture::f'?
printf("f(2) = %d\n", f(2));
^
TruthFixture::f
test/test.cpp:6:7: note: 'TruthFixture::f' declared here
int f (int a) {
^
test/test.cpp:18:25: error: call to non-static member function without an object argument
printf("f(2) = %d\n", f(2));
^
4 errors generated.
clearly the BOOST_GLOBAL_FIXTURE declaration is not doing anything.
But if I drop the global fixture declaration and revert to the per function declaration, it works:
#include <boost/test/unit_test.hpp>
struct TruthFixture {
int x = 3;
int f (int a) {
return a + 3;
}
};
BOOST_FIXTURE_TEST_CASE(test_variable, TruthFixture) {
printf("x = %d\n", x);
}
BOOST_FIXTURE_TEST_CASE(test_function, TruthFixture) {
printf("f(2) = %d\n", f(2));
}