简体   繁体   English

如何在谷歌测试中使用匹配器(错误:未声明的标识符)

[英]How to use matcher in google test (error: undeclared identifier)

I'm trying to use google test to test a C function.我正在尝试使用谷歌测试来测试 C function。 A simple test using ASSERT_NO_FATAL_FAILURE();使用 ASSERT_NO_FATAL_FAILURE() 的简单测试; and also EXPECT_THAT();.还有 EXPECT_THAT();。 But when I try to use matchers (like not null for example) the IDE says: Use of undeclared identifier 'NotNull'.但是当我尝试使用匹配器(例如不是 null)时,IDE 会说:使用未声明的标识符“NotNull”。

#include "gtest/gtest.h"

extern "C" {
    #include "first_tdd.h"
}

TEST(sum, sum_has_return)
{
    EXPECT_THAT(sum(), NotNull());
}
cmake_minimum_required(VERSION 3.19)
project(untitled C)

set(CMAKE_C_STANDARD 99)

add_executable(main.c sum.c)

# 'Google_test' is the subproject name
project(Google_tests)

# 'lib' is the folder with Google Test sources
add_subdirectory(googletest)
include_directories(${gtest_SOURCE_DIR}/include ${gtest_SOURCE_DIR}, ${gmock_SOURCE_DIR}/include ${gmock_SOURCE_DIR})

# 'Google_Tests_run' is the target name
# 'test1.cpp tests2.cpp' are source files with tests
set(Sources
        sum.c
        )
add_executable(Google_Tests_run sum_tests.cpp sum.c)
target_link_libraries(Google_Tests_run gtest gtest_main)

Message when I try to run the test.当我尝试运行测试时的消息。

 error: use of undeclared identifier 'NotNull'
        EXPECT_THAT(sum(), NotNull());
                           ^
1 error generated.
make[3]: *** [CMakeFiles/Google_Tests_run.dir/sum_tests.cpp.o] Error 1
make[2]: *** [CMakeFiles/Google_Tests_run.dir/all] Error 2
make[1]: *** [CMakeFiles/Google_Tests_run.dir/rule] Error 2
make: *** [Google_Tests_run] Error 2

Folder Structure文件夹结构

Does any one know if I should include something else?有谁知道我是否应该包括其他东西?

Thank you.谢谢你。

NotNull is declared in the namespace ::testing . NotNull在命名空间::testing中声明。 Possible fixes可能的修复

TEST(sum, sum_has_return)
{
    EXPECT_THAT(sum(), ::testing::NotNull());
}

or或者

TEST(sum, sum_has_return)
{
    using ::testing::NotNull;
    EXPECT_THAT(sum(), NotNull());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM