我想帮助设置google test和cmake。我使用Visual Studio 2017作为我的ide /编译器。
我的主要问题是我不确定我的测试是运行还是工作!我运行RUN_TESTS项目,一切似乎运行正常,但我没有得到谷歌测试主要运行的任何打印。例如"从gtest_main.cc"运行main();等。
这就是我想要的......
我希望我的测试能够处理两种情况2)& 3)。
我也将我的代码包含在我的测试可执行文件中,如下所示,我认为这是正确的方法。
add_executable (unit_tests example.h example.cpp test/example_add.cpp test/example_subtract.cpp)
我也在使用#include" ../ example.h"在我的测试文件中包含我要测试的代码。我不认为这是对的。 CMake应该已经添加了项目设置的包含路径吗?
我的cmake项目有以下文件夹结构。 example.h / .cpp是我想要测试的一些代码。它目前是在我在上面的案例2)中描述的项目中建立的。
\ Project2的
--- \ SRC
------的CMakeLists.txt
------CMakeLists.txt.in
------ example.cpp
------ example.h文件
------ main.cpp中
------ \测试
--------- example_add.cpp
--------- example_subtract.cpp
的CMakeLists.txt
cmake_minimum_required (VERSION 3.9)
project (Project2)
include (CTest)
# The version number.
set (Project2_VERSION_MAJOR 1)
set (Project2_VERSION_MINOR 0)
# add the binary tree to the search path for include files
# so that we will find Project1Config.h
include_directories ("${PROJECT_BINARY_DIR}")
# Download and unpack googletest at configure time
configure_file(CMakeLists.txt.in googletest-download/CMakeLists.txt)
execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" .
RESULT_VARIABLE result
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/googletest-download )
if(result)
message(FATAL_ERROR "CMake step for googletest failed: ${result}")
endif()
execute_process(COMMAND ${CMAKE_COMMAND} --build .
RESULT_VARIABLE result
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/googletest-download )
if(result)
message(FATAL_ERROR "Build step for googletest failed: ${result}")
endif()
# Prevent overriding the parent project's compiler/linker
# settings on Windows
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
# Add googletest directly to our build. This defines
# the gtest and gtest_main targets.
add_subdirectory(${CMAKE_BINARY_DIR}/googletest-src
${CMAKE_BINARY_DIR}/googletest-build
EXCLUDE_FROM_ALL)
# The gtest/gtest_main targets carry header search path
# dependencies automatically when using CMake 2.8.11 or
# later. Otherwise, we have to add them here ourselves.
if (CMAKE_VERSION VERSION_LESS 2.8.11)
include_directories("${gtest_SOURCE_DIR}/include")
endif()
# add the executable
add_executable (Project2 main.cpp example.h example.cpp)
target_link_libraries(Project2 gtest_main)
target_link_libraries (Project2 ${EXTRA_LIBS})
add_executable (unit_tests example.h example.cpp test/example_add.cpp test/example_subtract.cpp)
target_link_libraries (unit_tests gtest_main)
#
#
# INSTALL
#
#
# add the install targets
install (TARGETS Project2 DESTINATION bin)
#
#
# TESTS
#
#
add_test (NAME example_test COMMAND Project2)
add_test (NAME unit COMMAND ${CMAKE_BINARY_DIR}/unit_tests)
CMakeLists.txt.in
cmake_minimum_required(VERSION 3.9)
project(googletest-download NONE)
include(ExternalProject)
ExternalProject_Add(googletest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG master
SOURCE_DIR "${CMAKE_BINARY_DIR}/googletest-src"
BINARY_DIR "${CMAKE_BINARY_DIR}/googletest-build"
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
INSTALL_COMMAND ""
TEST_COMMAND ""
)
example.h文件
#pragma once
double add_numbers(const double f1, const double f2);
double subtract_numbers(const double f1, const double f2);
double multiply_numbers(const double f1, const double f2);
example.cpp
#include "example.h"
double add_numbers(const double f1, const double f2)
{
return f1 + f2;
}
double subtract_numbers(const double f1, const double f2)
{
return f1 - f2;
}
double multiply_numbers(const double f1, const double f2)
{
return f1 * f2;
}
的main.cpp
#include <iostream>
int main()
{
std::cout << "hello, world!" << std::endl;
return 0;
}
example_add.cpp
#include "gtest/gtest.h"
#include "../example.h"
TEST(example, add)
{
double res;
res = add_numbers(1.0, 2.0);
ASSERT_NEAR(res, 3.0, 1.0e-11);
}
example_subtract.cpp
#include "gtest/gtest.h"
#include "../example.h"
TEST(example, subtract)
{
double res;
res = subtract_numbers(1.0, 2.0);
ASSERT_NEAR(res, -1.0, 1.0e-11);
}
答案 0 :(得分:2)
问题是我有两个main()函数。我在单元测试项目中删除了main,并正确调用了gtestmain.cc中的main。
答案 1 :(得分:0)
在测试可执行文件的main
函数中,您必须具有与此类似的内容
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
你应该做的是在你的测试目录中添加另一个文件,该文件应包含如下代码:
// test executable
#include "gtest/gtest.h"
#include "example_add.cpp"
#include "example_substract"
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}