I have a couple global pointers defined in a UtTestBase.hpp
which are to be used by the files that include it (e.g: UtTest1.cpp
).
SetUpTestSuite()
is static
and thus performs a shared set up for all the tests. I view it as preconfiguration. Doing the way I have would complain about multiple definitions of the globals however using extern
for each doesn't work either.
Using extern
errors out
namespace TEST
{
extern std::weak_ptr<int> weakPtr;
extern std::shared_ptr<int> sharedInt;
// ...
}
// errors
undefined reference to `weakPtr' in line "weakPtr = sharedInt;"
undefined reference to `sharedInt' in line "sharedInt = std::make_shared<int>();"
From what I have seen the usage of extern
involves declaring the variables in a header and defining in a respective source file that uses it
What's the way around? static inline
is other option but does it make sense for each file to have a separate instance of a global given they are assigned in SetUpTestSuite()
?
UtTestBase.hpp
namespace TEST
{
std::weak_ptr<int> weakPtr;
std::shared_ptr<int> sharedInt;
struct UtTestBase
{
static void SetUpTestSuite()
{
sharedInt = std::make_shared<int>();
weakPtr = sharedInt;
}
// .. some common methods
};
}
UtTestBase.cpp
#include "UtTestBase.hpp"
namespace TEST
{
TEST_F(UtTestBase, Test1)
{
// ...
}
}
UtTest1.cpp
#include "UtTestBase.hpp"
namespace TEST
{
struct UtTest1 : public UtTestBase
{
void SetUp() override
{
// do init for a local test
}
// should have sharedInt and weakPtr assigned
};
}