| 1 |
cmake_minimum_required(VERSION 3.10) |
| 2 |
project(TestGoogleTest) |
| 3 |
include(CTest) |
| 4 |
|
| 5 |
include(GoogleTest) |
| 6 |
|
| 7 |
find_package(GTest REQUIRED) |
| 8 |
|
| 9 |
add_executable(test_gtest1 main1.cxx) |
| 10 |
target_link_libraries(test_gtest1 GTest::GTest) |
| 11 |
|
| 12 |
# Simple test of defaults |
| 13 |
gtest_add_tests(TARGET test_gtest1 |
| 14 |
TEST_LIST testList |
| 15 |
) |
| 16 |
set(expectedTests |
| 17 |
GoogleTest.LinksAndRuns |
| 18 |
GoogleTest.ConditionalFail |
| 19 |
) |
| 20 |
if(NOT testList STREQUAL "${expectedTests}") |
| 21 |
message(FATAL_ERROR "Expected test list: ${expectedTests} |
| 22 |
Actual test list: ${testList}") |
| 23 |
endif() |
| 24 |
|
| 25 |
|
| 26 |
# Same target, different arguments, so use test prefix and suffix to |
| 27 |
# differentiate from the above test cases |
| 28 |
gtest_add_tests(TARGET test_gtest1 |
| 29 |
TEST_LIST testList |
| 30 |
TEST_PREFIX "set2." |
| 31 |
TEST_SUFFIX ".foo" |
| 32 |
EXTRA_ARGS --forceFail |
| 33 |
) |
| 34 |
|
| 35 |
set(expectedTests |
| 36 |
set2.GoogleTest.LinksAndRuns.foo |
| 37 |
set2.GoogleTest.ConditionalFail.foo |
| 38 |
) |
| 39 |
if(NOT testList STREQUAL "${expectedTests}") |
| 40 |
message(FATAL_ERROR "Expected test list: ${expectedTests} |
| 41 |
Actual test list: ${testList}") |
| 42 |
endif() |
| 43 |
|
| 44 |
set_tests_properties(set2.GoogleTest.ConditionalFail.foo PROPERTIES WILL_FAIL YES) |
| 45 |
|
| 46 |
|
| 47 |
# Search specific sources to get the test list. Include an empty file |
| 48 |
# to ensure they are handled correctly too. |
| 49 |
add_executable(test_gtest2 main2.cxx empty.cxx) |
| 50 |
target_link_libraries(test_gtest2 GTest::Main) |
| 51 |
gtest_add_tests(TARGET test_gtest2 |
| 52 |
TEST_LIST testList |
| 53 |
SOURCES main2.h empty.cxx |
| 54 |
) |
| 55 |
set(expectedTests |
| 56 |
GoogleTest.SomethingElse |
| 57 |
GoogleTest.OffTest1 |
| 58 |
GoogleTest.OffTest2 |
| 59 |
GoogleTest.OffTest3 |
| 60 |
) |
| 61 |
if(NOT testList STREQUAL "${expectedTests}") |
| 62 |
message(FATAL_ERROR "Expected test list: ${expectedTests} |
| 63 |
Actual test list: ${testList}") |
| 64 |
endif() |
| 65 |
set(disabledTests |
| 66 |
GoogleTest.OffTest1 |
| 67 |
GoogleTest.OffTest2 |
| 68 |
GoogleTest.OffTest3 |
| 69 |
) |
| 70 |
foreach(T ${disabledTests}) |
| 71 |
get_test_property(${T} DISABLED testDisabled) |
| 72 |
if(NOT testDisabled) |
| 73 |
message(FATAL_ERROR "Test ${T} should be disabled but is not") |
| 74 |
endif() |
| 75 |
endforeach() |
| 76 |
|
| 77 |
# Non-keyword form, auto-find sources |
| 78 |
add_executable(test_gtest3 main3.cxx) |
| 79 |
target_link_libraries(test_gtest3 GTest::Main) |
| 80 |
gtest_add_tests(test_gtest3 "" AUTO) |
| 81 |
if(NOT TEST GoogleTest.Foo) |
| 82 |
message(FATAL_ERROR "Test case GoogleTest.Foo not defined") |
| 83 |
endif() |
| 84 |
# Check if test has property DEF_SOURCE_LINE |
| 85 |
get_test_property(GoogleTest.Foo DEF_SOURCE_LINE testSourceLineFoo) |
| 86 |
if(NOT testSourceLineFoo) |
| 87 |
message(FATAL_ERROR "Test GoogleTest.Foo should have DEF_SOURCE_LINE property") |
| 88 |
endif() |
| 89 |
# If check property ends with correct value |
| 90 |
string(FIND "${testSourceLineFoo}" "main3.cxx:3" testSourceLineFooFound) |
| 91 |
if(${testSourceLineFooFound} EQUAL -1) |
| 92 |
message(FATAL_ERROR "Test GoogleTest.Foo should have DEF_SOURCE_LINE property with value main3.cxx:3") |
| 93 |
endif() |
| 94 |
|
| 95 |
if(NOT TEST GoogleTest.Bar) |
| 96 |
message(FATAL_ERROR "Test case GoogleTest.Bar not defined") |
| 97 |
endif() |
| 98 |
# Check if test has property DEF_SOURCE_LINE |
| 99 |
get_test_property(GoogleTest.Bar DEF_SOURCE_LINE testSourceLineBar) |
| 100 |
if(NOT testSourceLineBar) |
| 101 |
message(FATAL_ERROR "Test GoogleTest.Bar should have DEF_SOURCE_LINE property") |
| 102 |
endif() |
| 103 |
# If check property ends with correct value |
| 104 |
string(FIND "${testSourceLineBar}" "main3.cxx:8" testSourceLineBarFound) |
| 105 |
if(${testSourceLineBarFound} EQUAL -1) |
| 106 |
message(FATAL_ERROR "Test GoogleTest.Bar should have DEF_SOURCE_LINE property with value main3.cxx:8") |
| 107 |
endif() |
| 108 |
|
| 109 |
# Non-keyword form, explicitly specified sources. Allows a non-target to be |
| 110 |
# given for the executable. |
| 111 |
add_executable(test_gtest4 main4.cxx) |
| 112 |
target_link_libraries(test_gtest4 GTest::Main) |
| 113 |
gtest_add_tests($<TARGET_FILE:test_gtest4> "" main4.h) |
| 114 |
if(NOT TEST GoogleTest.NoKeywords) |
| 115 |
message(FATAL_ERROR "Test case GoogleTest.NoKeywords not defined") |
| 116 |
endif() |