c++ - CMake + GoogleTest 没有运行测试,没有输出

标签 c++ cmake googletest

我需要一些帮助来设置 google test 和 cmake。我使用 Visual Studio 2017 作为我的 ide/编译器。

我的主要问题是我不确定我的测试是否正在运行甚至工作!我运行 RUN_TESTS 项目,一切似乎运行正常,但我没有打印出 google test main 运行的任何内容。例如“从 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 项目有以下文件夹结构。 example.h/.cpp 是我要测试的一些代码。它目前是在一个项目中设置的,就像我在上面的案例 2) 中描述的那样。

\项目2

---\源代码
------CMakeLists.txt
------CMakeLists.txt.in
------示例.cpp
------例子.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);
}

最佳答案

问题是我有两个 main() 函数。我删除了我的单元测试项目中的 main,并且正确调用了 gtestmain.cc 中的 main。

关于c++ - CMake + GoogleTest 没有运行测试,没有输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49532504/

相关文章:

找不到 CMake 库

c++ - 谷歌测试几个文件

c++ - 带有 std::ate 的 std::ofstream 未在末尾打开

c++ - 使用命名空间标准;和载体

c++ - WTL 子窗口事件处理

c++ - 即使在上下文终止后 ZMQ recv() 仍在阻塞

visual-studio-2010 - 如何在 cmake 中设置构建路径?

matlab - 如何使用cmake命令更改MATLAB路径以编译FLANN的mex文件?

c++ - googletest 符号的 "multiple definition"仅在使用 gradle 在 Travis CI 上编译时

c++ -/usr/bin/ld : cannot find -lgmock on ubuntu while running gmock program