使用Google C ++测试框架与CMake的最简单的例子 [英] Simplest example of using Google C++ Testing Framework with CMake

查看:371
本文介绍了使用Google C ++测试框架与CMake的最简单的例子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的C ++库(一个头文件,一个.cpp文件)。我想使用Google C ++测试框架为这个项目编写单元测试。

I have a very simple C++ library (one header file, one .cpp file). I want to write unit tests for this project using the Google C++ Testing Framework.

这里是目录结构:

~/project1
 |
 |-- project1.cpp
 |-- project1.h
 |-- project1_unittests.cpp
 \-- CMakeLists.txt

我不打算写我自己的main()函数。我想要与底层中提到的gtest_main链接。

I do not plan to write my own main() function. I want to link with gtest_main as mentioned in the primer. What should CMakeLists.txt contain?

推荐答案

启用CMake的内建测试子系统:

Enable CMake's built-in testing subsystem:

# For make-based builds, defines make target named test.
# For Visual Studio builds, defines Visual Studio project named RUN_TESTS.
enable_testing()

编译将运行单元测试的可执行文件,并将其与gtest和gtest_main:

Compile an executable that will run your unit tests and link it with gtest and gtest_main:

add_executable(runUnitTests
    project1_unittests.cpp
)
target_link_libraries(runUnitTests gtest gtest_main)

添加运行此可执行文件的测试:

Add a test which runs this executable:

add_test(
    NAME runUnitTests
    COMMAND runUnitTests
)

这篇关于使用Google C ++测试框架与CMake的最简单的例子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆