Skip to content

Commit 182f96e

Browse files
committed
add gtest and sample test
1 parent 236b899 commit 182f96e

File tree

3 files changed

+67
-1
lines changed

3 files changed

+67
-1
lines changed

CMakeLists.txt

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,47 @@ project(gtest_coverage)
33

44
set(CMAKE_CXX_STANDARD 20)
55

6-
add_executable(gtest_coverage main.cpp src/testsubject.cpp)
6+
# Reference: https://github.com/google/googletest/tree/master/googletest#incorporating-into-an-existing-cmake-project
7+
# Download and unpack googletest at configure time
8+
configure_file(CMakeLists.txt.in googletest-download/CMakeLists.txt)
9+
execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" .
10+
RESULT_VARIABLE result
11+
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download)
12+
if (result)
13+
message(FATAL_ERROR "CMake step for googletest failed: ${result}")
14+
endif ()
15+
execute_process(COMMAND ${CMAKE_COMMAND} --build .
16+
RESULT_VARIABLE result
17+
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download)
18+
if (result)
19+
message(FATAL_ERROR "Build step for googletest failed: ${result}")
20+
endif ()
21+
22+
# Prevent overriding the parent project's compiler/linker
23+
# settings on Windows
24+
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
25+
26+
# Add googletest directly to our build. This defines
27+
# the gtest and gtest_main targets.
28+
add_subdirectory(${CMAKE_CURRENT_BINARY_DIR}/googletest-src
29+
${CMAKE_CURRENT_BINARY_DIR}/googletest-build
30+
EXCLUDE_FROM_ALL)
31+
32+
# The gtest/gtest_main targets carry header search path
33+
# dependencies automatically when using CMake 2.8.11 or
34+
# later. Otherwise we have to add them here ourselves.
35+
if (CMAKE_VERSION VERSION_LESS 2.8.11)
36+
include_directories("${gtest_SOURCE_DIR}/include")
37+
endif ()
38+
39+
# Now simply link against gtest or gtest_main as needed.
40+
include_directories(cmake-build-debug/googletest-src/googlemock/include)
41+
include_directories(cmake-build-debug/googletest-src/googletest/include)
42+
43+
set(SOURCE_FILES src/testsubject.cpp)
44+
add_executable(main main.cpp ${SOURCE_FILES})
45+
46+
set(TEST_FILES test/tester.cpp)
47+
add_executable(test ${TEST_FILES} ${SOURCE_FILES})
48+
49+
target_link_libraries(test gtest gtest_main)

CMakeLists.txt.in

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
cmake_minimum_required(VERSION 2.8.2)
2+
3+
project(googletest-download NONE)
4+
5+
include(ExternalProject)
6+
ExternalProject_Add(googletest
7+
GIT_REPOSITORY https://github.com/google/googletest.git
8+
GIT_TAG master
9+
SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/googletest-src"
10+
BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/googletest-build"
11+
CONFIGURE_COMMAND ""
12+
BUILD_COMMAND ""
13+
INSTALL_COMMAND ""
14+
TEST_COMMAND ""
15+
)

test/tester.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#include <gtest/gtest.h>
2+
3+
#include "../src/testsubject.h"
4+
5+
TEST(MathTest, Square) {
6+
ASSERT_EQ(TestSubject::square(2), 4);
7+
ASSERT_EQ(TestSubject::square(3), 9);
8+
}

0 commit comments

Comments
 (0)