GTEST - 合格名称未在“:”标记之前命名 class
GTEST - qualified name does not name a class before ':' token
这是一个示例代码:
#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();
}
我收到以下错误:
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’
如何解决? TEST_F 是否必须与固定装置位于同一名称空间中?
来自 TEST_F
文档:
The first parameter is the name of the test fixture class, which also doubles as the test case name.
第一个参数用作测试 class 名称的一部分,因此将复合标识符放在那里会使 TEST_F(A::B::MyFixture, Test1)
宏扩展为无效代码:
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()
{
}
这是基于宏的单元测试框架的众多缺点之一。
如果您想使用来自不同命名空间的夹具,您需要将其名称引入当前命名空间,然后在宏中使用非限定名称:
using A::B::MyFixture;
TEST_F(MyFixture, Test1)
{
…
这是一个示例代码:
#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();
}
我收到以下错误:
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’
如何解决? TEST_F 是否必须与固定装置位于同一名称空间中?
来自 TEST_F
文档:
The first parameter is the name of the test fixture class, which also doubles as the test case name.
第一个参数用作测试 class 名称的一部分,因此将复合标识符放在那里会使 TEST_F(A::B::MyFixture, Test1)
宏扩展为无效代码:
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()
{
}
这是基于宏的单元测试框架的众多缺点之一。
如果您想使用来自不同命名空间的夹具,您需要将其名称引入当前命名空间,然后在宏中使用非限定名称:
using A::B::MyFixture;
TEST_F(MyFixture, Test1)
{
…