c++ - GTEST - 限定名称不在“:”标记之前命名类
问题描述
这是一个示例代码:
#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
文档:
第一个参数是测试夹具类的名称,它也是测试用例名称的两倍。
第一个参数用作测试类名称的一部分,因此将复合标识符放在那里会使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)
{
…
推荐阅读
- ibm-midrange - 寻找 Iseries / AS400 cl 代码来运行 lansa 导入和导出
- python - 如何修复 csv 文件日期转换函数中的无效字符错误
- jint - 金特。如何设置类的属性或调用方法?
- c# - 在 TreeView 中使用 ContextMenu
- java - 我有一个 Querydsl 问题。(壮举。BooleanBuilder)
- python - 递归函数中的默认字典参数
- ruby-on-rails - Sidekiq Procfile 队列排除
- java - 如何从地图中获取重复值
> - apache - Apache、Linux 2、Elastic Beanstalk - 将 http 和 www 重写为 https 非 www
- html - 如何在博客文章中包含基于 frontmatter 的 css 文件?