Use Catch2 with Cmake

Use Catch2 with Cmake,第1张

Use Catch2 with Cmake

创建如下文件目录

├───build/
├───src/
    └───Sort.cpp
├───tests/
    └───lib/
    └───SortTest.cpp
    └───CMakeLists.txt
 ├─── CMakeLists.txt

编辑根目录下的 CMakeLists.txt

cmake_minimum_required(VERSION 3.10) 

# project name and version
project(Example VERSION 1.0) 

# add the executable 
add_executable(Example src/Sort.cpp) 

# specify the C++ standard 
set(CMAKE_CXX_STANDARD 11) 
set(CMAKE_CXX_STANDARD_REQUIRED True)

add_subdirectory("tests")

在tests/lib目录下git clone Catch2 源码 

GitHub - catchorg/Catch2: A modern, C++-native, test framework for unit-tests, TDD and BDD - using C++14, C++17 and later (C++11 support is in v2.x branch, and C++03 on the Catch1.x branch)https://github.com/catchorg/Catch2

下载完成后目录结构为

├───build/
├───src/
	└───Sort.cpp
├───tests/
	└───lib/
 		└───Catch2/
	└───SortTest.cpp
	└───CMakeLists.txt
├─── CMakeLists.txt

编辑tests目录下的CMakeLists.txt

cmake_minimum_required(VERSION 3.10) 

# specify the C++ standard 
set(CMAKE_CXX_STANDARD 11) 
set(CMAKE_CXX_STANDARD_REQUIRED True)

add_subdirectory(lib/Catch2)

# These tests can use the Catch2-provided main
add_executable(unit_tests SortTest.cpp)
target_link_libraries(unit_tests PRIVATE Catch2::Catch2WithMain)

# These tests need their own main
# add_executable(custom-main-tests test.cpp test-main.cpp)
# target_link_libraries(custom-main-tests PRIVATE Catch2::Catch2)

命令行进入build目录运行 cmake ..

打开build目录下生成的Example.sln

将unit_tests工程设置为启动项

在tests/SortTest.cpp中加入头文件`#include`

运行visual studio

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/zaji/5651186.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-12-16
下一篇2022-12-16

发表评论

登录后才能评论

评论列表(0条)

    保存