使用 Google C++ 的最简单示例使用 CMake 测试框架

发布于 2024-11-05 07:19:25 字数 429 浏览 1 评论 0原文

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

这是目录结构:

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

我不打算编写自己的 main() 函数。我想与 入门。 CMakeLists.txt 应该包含什么?

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.

Here is the directory structure:

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏末的微笑 2024-11-12 07:19:25

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

# 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 链接:

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

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

add_test(
    NAME runUnitTests
    COMMAND runUnitTests
)

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()

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
)
不美如何 2024-11-12 07:19:25

这是最简单的一个,

1.创建一个简单的源文件,

$ cat simplegtest.cpp 

#include<gtest/gtest.h>
TEST(Mytest, failing_test){
    EXPECT_TRUE(false);
}

2.使用以下命令编译它,

$ LDLIBS="-lgtest_main -lgtest" make simplegtest
g++     simplegtest.cpp  -lgtest_main -lgtest -o simplegtest

3.使用以下命令执行测试可执行文件,

$ ./simplegtest 
Running main() from /home/prashant/work/thirdparty/googletest-release-1.8.1/googletest/src/gtest_main.cc
[==========] Running 1 test from 1 test case.
[----------] Global test environment set-up.
[----------] 1 test from Mytest
[ RUN      ] Mytest.failing_test
simplegtest.cpp:4: Failure
Value of: false
  Actual: false
Expected: true
[  FAILED  ] Mytest.failing_test (0 ms)
[----------] 1 test from Mytest (0 ms total)

[----------] Global test environment tear-down
[==========] 1 test from 1 test case ran. (1 ms total)
[  PASSED  ] 0 tests.
[  FAILED  ] 1 test, listed below:
[  FAILED  ] Mytest.failing_test

 1 FAILED TEST

Here is a simplest one,

1.Create a simple source file,

$ cat simplegtest.cpp 

#include<gtest/gtest.h>
TEST(Mytest, failing_test){
    EXPECT_TRUE(false);
}

2.Compile it using below command,

$ LDLIBS="-lgtest_main -lgtest" make simplegtest
g++     simplegtest.cpp  -lgtest_main -lgtest -o simplegtest

3.Execute the test executable using below command,

$ ./simplegtest 
Running main() from /home/prashant/work/thirdparty/googletest-release-1.8.1/googletest/src/gtest_main.cc
[==========] Running 1 test from 1 test case.
[----------] Global test environment set-up.
[----------] 1 test from Mytest
[ RUN      ] Mytest.failing_test
simplegtest.cpp:4: Failure
Value of: false
  Actual: false
Expected: true
[  FAILED  ] Mytest.failing_test (0 ms)
[----------] 1 test from Mytest (0 ms total)

[----------] Global test environment tear-down
[==========] 1 test from 1 test case ran. (1 ms total)
[  PASSED  ] 0 tests.
[  FAILED  ] 1 test, listed below:
[  FAILED  ] Mytest.failing_test

 1 FAILED TEST
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文