0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

備忘録 GoogleTest&CMake&CTest

Last updated at Posted at 2022-10-06

参考 : 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
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?