summaryrefslogtreecommitdiff
path: root/Tests/RunCMake/GoogleTest
diff options
context:
space:
mode:
authorMatthew Woehlke <matthew.woehlke@kitware.com>2017-07-27 09:47:28 -0400
committerMatthew Woehlke <matthew.woehlke@kitware.com>2017-07-27 09:47:28 -0400
commitbfcda4013a52e81a1ffc2f10a6006ba75b9b607d (patch)
treed47130290e0d71912e392418d59737b2bbb9f511 /Tests/RunCMake/GoogleTest
parentaa97170f2b25a99d2cc69fd6b2a059e52872f341 (diff)
downloadcmake-bfcda4013a52e81a1ffc2f10a6006ba75b9b607d.tar.gz
Add dynamic test discovery for for Google Test
Add a new gtest_discover_tests function to GoogleTest.cmake, implementing dynamic test discovery (i.e. tests are discovered by actually running the test executable and asking for the list of available tests, which is used to dynamically declare the tests) rather than the source-parsing approach used by gtest_add_tests. Compared to the source-parsing approach, this has the advantage of being robust against users declaring tests in unusual ways, and much better support for advanced features such as parameterized tests. A unit test, modeled after the TEST_INCLUDE_DIR[S] test, is also included. Note that the unit test does not actually require that Google Test is available. The new functionality does not actually depend on Google Test as such; it only requires that the test executable lists tests in the expected format when invoked with --gtest_list_tests, which the unit test can fake readily.
Diffstat (limited to 'Tests/RunCMake/GoogleTest')
-rw-r--r--Tests/RunCMake/GoogleTest/CMakeLists.txt3
-rw-r--r--Tests/RunCMake/GoogleTest/GoogleTest-test-stdout.txt25
-rw-r--r--Tests/RunCMake/GoogleTest/GoogleTest.cmake15
-rw-r--r--Tests/RunCMake/GoogleTest/RunCMakeTest.cmake26
-rw-r--r--Tests/RunCMake/GoogleTest/fake_gtest.cpp41
5 files changed, 110 insertions, 0 deletions
diff --git a/Tests/RunCMake/GoogleTest/CMakeLists.txt b/Tests/RunCMake/GoogleTest/CMakeLists.txt
new file mode 100644
index 0000000000..dc92486973
--- /dev/null
+++ b/Tests/RunCMake/GoogleTest/CMakeLists.txt
@@ -0,0 +1,3 @@
+cmake_minimum_required(VERSION 3.6)
+project(${RunCMake_TEST} NONE)
+include(${RunCMake_TEST}.cmake)
diff --git a/Tests/RunCMake/GoogleTest/GoogleTest-test-stdout.txt b/Tests/RunCMake/GoogleTest/GoogleTest-test-stdout.txt
new file mode 100644
index 0000000000..5f7753dba1
--- /dev/null
+++ b/Tests/RunCMake/GoogleTest/GoogleTest-test-stdout.txt
@@ -0,0 +1,25 @@
+Test project .*
+ Start 1: TEST:basic\.case_foo!1
+1/8 Test #1: TEST:basic\.case_foo!1 \.+ +Passed +[0-9.]+ sec
+ Start 2: TEST:basic\.case_bar!1
+2/8 Test #2: TEST:basic\.case_bar!1 \.+ +Passed +[0-9.]+ sec
+ Start 3: TEST:basic\.disabled_case!1
+3/8 Test #3: TEST:basic\.disabled_case!1 \.+\*+Not Run \(Disabled\) +[0-9.]+ sec
+ Start 4: TEST:disabled\.case!1
+4/8 Test #4: TEST:disabled\.case!1 \.+\*+Not Run \(Disabled\) +[0-9.]+ sec
+ Start 5: TEST:typed/short\.case!1
+5/8 Test #5: TEST:typed/short\.case!1 \.+ +Passed +[0-9.]+ sec
+ Start 6: TEST:typed/float\.case!1
+6/8 Test #6: TEST:typed/float\.case!1 \.+ +Passed +[0-9.]+ sec
+ Start 7: TEST:value/test\.case/1!1
+7/8 Test #7: TEST:value/test\.case/1!1 \.+ +Passed +[0-9.]+ sec
+ Start 8: TEST:value/test\.case/"foo"!1
+8/8 Test #8: TEST:value/test\.case/"foo"!1 \.+ +Passed +[0-9.]+ sec
+
+100% tests passed, 0 tests failed out of 6
+
+Total Test time \(real\) = +[0-9.]+ sec
+
+The following tests did not run:
+.*3 - TEST:basic\.disabled_case!1 \(Disabled\)
+.*4 - TEST:disabled\.case!1 \(Disabled\)
diff --git a/Tests/RunCMake/GoogleTest/GoogleTest.cmake b/Tests/RunCMake/GoogleTest/GoogleTest.cmake
new file mode 100644
index 0000000000..9a3677fae7
--- /dev/null
+++ b/Tests/RunCMake/GoogleTest/GoogleTest.cmake
@@ -0,0 +1,15 @@
+project(test_include_dirs)
+include(CTest)
+include(GoogleTest)
+
+enable_testing()
+
+add_executable(fake_gtest fake_gtest.cpp)
+
+gtest_discover_tests(
+ fake_gtest
+ TEST_PREFIX TEST:
+ TEST_SUFFIX !1
+ EXTRA_ARGS how now "\"brown\" cow"
+ PROPERTIES LABELS TEST
+)
diff --git a/Tests/RunCMake/GoogleTest/RunCMakeTest.cmake b/Tests/RunCMake/GoogleTest/RunCMakeTest.cmake
new file mode 100644
index 0000000000..aec8568d05
--- /dev/null
+++ b/Tests/RunCMake/GoogleTest/RunCMakeTest.cmake
@@ -0,0 +1,26 @@
+include(RunCMake)
+
+function(run_GoogleTest)
+ # Use a single build tree for a few tests without cleaning.
+ set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/GoogleTest-build)
+ set(RunCMake_TEST_NO_CLEAN 1)
+ if(RunCMake_GENERATOR MATCHES "Make|Ninja")
+ set(RunCMake_TEST_OPTIONS -DCMAKE_BUILD_TYPE=Debug)
+ endif()
+ file(REMOVE_RECURSE "${RunCMake_TEST_BINARY_DIR}")
+ file(MAKE_DIRECTORY "${RunCMake_TEST_BINARY_DIR}")
+ run_cmake(GoogleTest)
+ run_cmake_command(GoogleTest-build
+ ${CMAKE_COMMAND}
+ --build .
+ --config Debug
+ )
+ run_cmake_command(GoogleTest-test
+ ${CMAKE_CTEST_COMMAND}
+ -C Debug
+ -L TEST
+ --no-label-summary
+ )
+endfunction()
+
+run_GoogleTest()
diff --git a/Tests/RunCMake/GoogleTest/fake_gtest.cpp b/Tests/RunCMake/GoogleTest/fake_gtest.cpp
new file mode 100644
index 0000000000..f1bd7efc8d
--- /dev/null
+++ b/Tests/RunCMake/GoogleTest/fake_gtest.cpp
@@ -0,0 +1,41 @@
+#include <iostream>
+#include <string>
+
+int main(int argc, char** argv)
+{
+ // Note: GoogleTest.cmake doesn't actually depend on Google Test as such;
+ // it only requires that we produces output in the expected format when
+ // invoked with --gtest_list_tests. Thus, we fake that here. This allows us
+ // to test the module without actually needing Google Test.
+ if (argc > 1 && std::string(argv[1]) == "--gtest_list_tests") {
+ std::cout << "basic." << std::endl;
+ std::cout << " case_foo" << std::endl;
+ std::cout << " case_bar" << std::endl;
+ std::cout << " DISABLED_disabled_case" << std::endl;
+ std::cout << "DISABLED_disabled." << std::endl;
+ std::cout << " case" << std::endl;
+ std::cout << "typed/0. # TypeParam = short" << std::endl;
+ std::cout << " case" << std::endl;
+ std::cout << "typed/1. # TypeParam = float" << std::endl;
+ std::cout << " case" << std::endl;
+ std::cout << "value/test." << std::endl;
+ std::cout << " case/0 # GetParam() = 1" << std::endl;
+ std::cout << " case/1 # GetParam() = \"foo\"" << std::endl;
+ return 0;
+ }
+
+ if (argc > 5) {
+ // Simple test of EXTRA_ARGS
+ if (std::string(argv[3]) == "how" && std::string(argv[4]) == "now" &&
+ std::string(argv[5]) == "\"brown\" cow") {
+ return 0;
+ }
+ }
+
+ // Print arguments for debugging, if we didn't get the expected arguments
+ for (int i = 1; i < argc; ++i) {
+ std::cerr << "arg[" << i << "]: '" << argv[i] << "'\n";
+ }
+
+ return 1;
+}