Here is an example code:
#include <gtest/gtest.h>
#include <gmock/gmock.h>
namespace A { namespace B {
struct MyFixture: public ::testing::Test
{
};
}}
TEST_F(A::B::MyFixture, Test1)
{
}
int main(int argc, char** argv)
{
::testing::InitGoogleMock(&argc, argv);
return RUN_ALL_TESTS();
}
I get the following error:
gtest-internal.h:1255: error: qualified name does not name a class before ‘:’ token
gtest.h:2305: in expansion of macro ‘GTEST_TEST_’
main.cpp:29: in expansion of macro ‘TEST_F’
gtest-internal.h:1255: error: expected ‘{’ before ‘:’ token
gtest.h:2305: in expansion of macro ‘GTEST_TEST_’
main.cpp:29: in expansion of macro ‘TEST_F’
gtest-internal.h:1255: error: expected unqualified-id before ‘:’ token
gtest.h:2305: in expansion of macro ‘GTEST_TEST_’
main.cpp:29: in expansion of macro ‘TEST_F’
How to fix that ? Does TEST_F have to be in the same namespace as a fixture ?
From TEST_F
documentation:
The first parameter is the name of the test fixture class, which also doubles as the test case name.
First parameter is used as part of test class name so putting compound identifier there makes TEST_F(A::B::MyFixture, Test1)
macro expand to invalid code:
class A::B::MyFixture_Test1_Test: public A::B::MyFixture
{
public: A::B::MyFixture_Test1_Test()
{
}
private: virtual void TestBody();
static ::testing::TestInfo* const test_info_;
A::B::MyFixture_Test1_Test(A::B::MyFixture_Test1_Test const &) = delete;
void operator=(A::B::MyFixture_Test1_Test const &) = delete;
};
::testing::TestInfo* const A::B::MyFixture_Test1_Test::test_info_ = ::testing::internal::MakeAndRegisterTestInfo
(
"A::B::MyFixture"
, "Test1"
, nullptr
, nullptr
, ::testing::internal::CodeLocation
(
"d:\\projects\\googletest-master\\googletest\\src\\gtest-all.cc", 60
)
, (::testing::internal::GetTypeId<A::B::MyFixture>())
, A::B::MyFixture::SetUpTestCase
, A::B::MyFixture::TearDownTestCase
, new ::testing::internal::TestFactoryImpl<A::B::MyFixture_Test1_Test>
);
void A::B::MyFixture_Test1_Test::TestBody()
{
}
This is one of the many drawbacks of macro-based unit test frameworks.
If you want to use fixture from different namespace you will need to bring its name into current namespace and then use non-qualified name in macro:
using A::B::MyFixture;
TEST_F(MyFixture, Test1)
{
…