How to use matcher in google test (error: undeclared identifier)
How to use matcher in google test (error: undeclared identifier)
我正在尝试使用 google 测试来测试 C 函数。使用 ASSERT_NO_FATAL_FAILURE() 的简单测试;还有 EXPECT_THAT();。但是当我尝试使用匹配器时(例如 not 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)
当我尝试运行测试时留言。
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
有人知道我是否应该包括其他内容吗?
谢谢。
NotNull
在命名空间 ::testing
中声明。可能的修复
TEST(sum, sum_has_return)
{
EXPECT_THAT(sum(), ::testing::NotNull());
}
或
TEST(sum, sum_has_return)
{
using ::testing::NotNull;
EXPECT_THAT(sum(), NotNull());
}
我正在尝试使用 google 测试来测试 C 函数。使用 ASSERT_NO_FATAL_FAILURE() 的简单测试;还有 EXPECT_THAT();。但是当我尝试使用匹配器时(例如 not 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)
当我尝试运行测试时留言。
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
有人知道我是否应该包括其他内容吗?
谢谢。
NotNull
在命名空间 ::testing
中声明。可能的修复
TEST(sum, sum_has_return)
{
EXPECT_THAT(sum(), ::testing::NotNull());
}
或
TEST(sum, sum_has_return)
{
using ::testing::NotNull;
EXPECT_THAT(sum(), NotNull());
}