cmake googletest不运行测试,没有输出

CMake + GoogleTest not running tests, no output

本文关键字:输出 运行测试 googletest cmake      更新时间:2023-10-16

我想帮助设置Google测试和CMAKE。我正在使用Visual Studio 2017作为IDE/编译器。

我的主要问题是我不确定我的测试是否正在运行甚至工作!我运行run_tests项目,一切似乎都可以运行,但是我没有打印出Google Test Main Ran。例如,"从gtest_main.cc中运行main(("等。

这就是我想要的...

  1. 将我的项目代码与测试代码分开
  2. 将我的代码(要测试的类(作为项目的一部分的选择
  3. 或作为项目链接到的单独库。

我希望我的测试能够处理两个案例2(&3(。

我还将我的代码包括在我的测试可执行文件中,如下所示,我认为这不是正确的方法。

add_executable (unit_tests example.h example.cpp test/example_add.cpp test/example_subtract.cpp)

,我还在测试文件中使用#include" ../example.h"来包括要测试的代码。我认为这是不对的。Cmake应该已经添加了项目设置的包含路径?

我的CMAKE项目具有以下文件夹结构。示例.h/.cpp是我要测试的一些代码。目前,它是在我描述的案例2(的项目中设置的。

project2

--- src
------ cmakelists.txt
-------cmakelists.txt.in
-------示例
-------示例.h
------ main.cpp
------ test
----------示例_add.cpp
----------示例_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);
}

问题是我有两个主要((函数。我删除了单元测试项目中的主,而gtestmain.cc中的主是正确地称为。

在测试的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();
}

重点是您需要在某个地方运行的初始化/执行代码,以某种方式通过您运行的可执行文件。如果您没有创建主函数,则它将由链接器[1]自动从gtest_main库(gtest_main.cc(导入。如果需要的话,您也可以制作自己的上述组合版本:

int main(int argc, char** argv)
{
    std::cout << "hello, world!" << std::endl;
    ::testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
} 

[1]只要链接器知道将其导入它(例如,您在链接时间的某个地方将其引用(。使用VS,Linker命令可以包括/子系统:Console Plus Links Shell32.Lib,其库内部参考主体。然后链接迫使搜索主要,因此"只是工作"。如果由于某种原因您的项目不使用这些选项,则还有链接选项迫使导入未参考的功能。或者,您也可以强行在源文件中使用技巧或示例中的源文件中引用main:

extern "C" int main(int, char**);
void _force_reference() { main(0, nullptr); }