参考 : https://google.github.io/googletest/quickstart-cmake.html
- フォルダ構成
project
+-- src : こっちに本体
+-- test
CMakeLists.txt
+-- sub1
CMakeLists.txt
test_X.cpp
+-- sub2
CMakeLists.txt
test_Y.cpp
CMakeLists.txt
test/CMakeLists.txt
cmake_minimum_required(VERSION 2.8)
enable_testing()
project(test_project)
# GoogleTest requires at least C++14
set(CMAKE_CXX_STANDARD 14)
include(FetchContent)
FetchContent_Declare(
googletest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG release-1.12.1
)
# For Windows: Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)
add_subdirectory(sub1)
add_subdirectory(sub2)
test/sub1/CMakeLists.txt
enable_testing()
add_executable(
test_sub1
test_X.cpp
)
target_link_libraries(
test_sub1
GTest::gtest_main
)
include(GoogleTest)
gtest_discover_tests(test_sub1)
test/sub2/CMakeLists.txt
enable_testing()
add_executable(
test_sub2
test_Y.cpp
)
target_link_libraries(
test_sub2
GTest::gtest_main
)
include(GoogleTest)
gtest_discover_tests(test_sub2)
- テストコード
test/sub1/test_X.cpp
#include <gtest/gtest.h>
// Demonstrate some basic assertions.
TEST(HelloTest1, BasicAssertions) {
// Expect two strings not to be equal.
EXPECT_STRNE("hello", "world");
// Expect equality.
EXPECT_EQ(7 * 6, 42);
}
test/sub2/test_Y.cpp
#include <gtest/gtest.h>
// Demonstrate some basic assertions.
TEST(HelloTest2, BasicAssertions) {
// Expect two strings not to be equal.
EXPECT_STRNE("hello", "world");
// Expect equality.
EXPECT_EQ(7 * 6, 42);
}
- ビルド&テスト実行
$ cmake -S . -B build
$ cmake --build build
$ cd build && ctest