简体   繁体   English

Catch2与CMake和Visual Studio

[英]Catch2 with CMake and Visual Studio

I'm trying to setup Catch2 test framework for a library I have built. 我正在尝试为我建立的库设置Catch2测试框架。 I'm using CMake and Visual Studio 2017. 我正在使用CMake和Visual Studio 2017。

My project structure is: 我的项目结构是:

executable-project/
|-- library
      |--include/
      |    |--SUT.h 
      |--src/
      |    |--SUT.cpp
      |--tests/
      |    |--catch.hpp
      |    |--SUTTest.cpp
      |CMakeLists.txt
|include/
|src/
| |--main.cpp
|CMakeLists.txt

SUT.h , SUT.cpp and SUTTest.cpp are just testing a factorial function defined based on the example . SUT.hSUT.cppSUTTest.cpp只是测试基于示例定义的阶乘函数。

My CMakeLists.txt file in the library is 我在库中的CMakeLists.txt文件是

cmake_minimum_required (VERSION 3.8)

set_property(GLOBAL PROPERTY USE_FOLDERS ON)

file(GLOB HEADER_FILES "${CMAKE_CURRENT_SOURCE_DIR}/include/*.h")
file(GLOB SOURCE_FILES "${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp")
file(GLOB TEST_FILES "${CMAKE_CURRENT_SOURCE_DIR}/tests/*.cpp")

add_library(MY_LIBRARY ${HEADER_FILES} ${SOURCE_FILES} ${TEST_FILES})

target_include_directories(MY_LIBRARY PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include")

source_group("Test Files" FILES ${TEST_FILES})

The SUTTest.cpp file is: SUTTest.cpp文件是:

#define CATCH_CONFIG_MAIN

#include "catch.hpp"
#include "SUT.h"

TEST_CASE("Factorials are computed", "[factorial]")
{
    SUT sut;

    REQUIRE(sut.Factorial(0) == 0);
    REQUIRE(sut.Factorial(1) == 1);
    REQUIRE(sut.Factorial(2) == 2);
    REQUIRE(sut.Factorial(3) == 6);
    REQUIRE(sut.Factorial(10) == 3628800);
}

The "executable-project" simply makes use of the library (it's the actual application). “可执行项目”仅使用该库(它是实际的应用程序)。 The CMakeLists.txt in that project dynamically links the library to it. 该项目中的CMakeLists.txt动态链接库。

When I build this solution in Visual Studio, build works fine. 当我在Visual Studio中构建此解决方案时,构建工作正常。

However, the test isn't failing despite asserting that the Factorial(0) == 0 . 但是,即使断言Factorial(0) == 0 ,测试也不会失败。 Also, Visual Studio is not discovering the tests in the tests folder. 此外,Visual Studio不在tests文件夹中发现测试。 What I'm trying to achieve are: 我想要实现的是:

  1. When I click on Build in Visual Studio, the tests to be run as part of the build. 当我在Visual Studio中单击“生成”时,测试将作为生成的一部分运行。

  2. (Optional) to be able for Visual Studio to discover the tests. (可选)使Visual Studio能够发现测试。

EDIT: 编辑:

cmake_minimum_required (VERSION 3.8)

project("My Project")

file(GLOB HEADER_FILES "${CMAKE_CURRENT_SOURCE_DIR}/include/*.h")
file(GLOB SOURCE_FILES "${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp")

add_subdirectory(library)
add_executable(MY_APP main.cpp ${HEADER_FILES} ${SOURCE_FILES})

target_include_directories(MY_APP PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include")
target_link_libraries(MY_APP MY_LIBRARY)

the test isn't failing despite asserting that the Factorial(0) == 0 尽管断言Factorial(0)== 0,测试仍未失败

You should assert what is true/expected. 您应该断言什么是真实的/预期的。 Then if your written code is wrong, it will fail. 然后,如果您编写的代码错误,它将失败。

REQUIRE( Factorial(0) == 1 ); (not == 0 ) (不是== 0

When I click on Build in Visual Studio, the tests to be run as part of the build. 当我在Visual Studio中单击“生成”时,测试将作为生成的一部分运行。

Within the Visual Studio IDE, the build and run are two separate steps. 在Visual Studio IDE中,构建和运行是两个单独的步骤。 If you want to sequence it automatically, write a simple .bat script which first builds and then runs. 如果要自动排序,请编写一个简单的.bat脚本,该脚本首先生成然后运行。

(Optional) to be able for Visual Studio to discover the tests. (可选)使Visual Studio能够发现测试。

Catch2 has CMake integration which contains CMake scripts for automatic registration of TEST_CASE s in CTest . Catch2具有CMake集成,其中包含CMake脚本,用于在CTest中自动注册TEST_CASE

cmake_minimum_required(VERSION 3.5)

project(baz LANGUAGES CXX VERSION 0.0.1)

find_package(Catch2 REQUIRED)
add_executable(foo test.cpp)
target_link_libraries(foo Catch2::Catch2)

include(CTest)
include(Catch)
catch_discover_tests(foo)

See Catch2 CMake Integration for details. 有关详细信息,请参见Catch2 CMake集成

Finally managed to get Catch2 to work with Visual Studio. 最终设法使Catch2与Visual Studio一起使用。

First I downloaded Catch2 release from their GitHub and installed it on my computer using (this is actually documented here ). 首先,我从他们的GitHub下载了Catch2版本,并使用来将其安装在我的计算机上(实际上在此处进行了记录 )。

cmake -Bbuild -S. -DBUILD_TESTING=OFF
cmake --build build/ --target install (Need admin rights)

This installs Catch2 in C:\\Program Files (x86)\\Catch2. 这会将Catch2安装在C:\\ Program Files(x86)\\ Catch2中。 Then I copied everything in this install directory to my project's deps/Catch2 folder. 然后,我将此安装目录中的所有内容复制到项目的deps / Catch2文件夹中。 Then just add the below to the CMakeLists.txt: 然后只需将以下内容添加到CMakeLists.txt:

find_package(Catch2 REQUIRED PATHS "${CMAKE_CURRENT_SOURCE_DIR}/deps/Catch2")
target_link_libraries(MY_LIBRARY Catch2::Catch2)

include(CTest)
include(Catch)
catch_discover_tests(MY_LIBRARY)

This adds the RUN_TESTS project to Visual Studio. 这会将RUN_TESTS项目添加到Visual Studio。 When I build this project, my unit tests run. 在构建该项目时,将运行单元测试。 Hope this is helpful to anyone trying to integrate with CMake. 希望这对尝试与CMake集成的人有所帮助。

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

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