From 542f61f04b8d13b047ce19e37ed3b1e2b9859679 Mon Sep 17 00:00:00 2001
From: Matthias Grob <maetugr@gmail.com>
Date: Tue, 23 Apr 2019 08:36:03 +0100
Subject: [PATCH] testing build: add possibility to filter tests with ctest
 regex

---
 CMakeLists.txt                         | 3 ++-
 Makefile                               | 1 +
 platforms/posix/cmake/sitl_tests.cmake | 2 +-
 3 files changed, 4 insertions(+), 2 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 409dc2c3f341..63ec99a73150 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -418,13 +418,14 @@ option(CMAKE_TESTING "Configure test targets" OFF)
 if(CMAKE_TESTING)
 	include(CTest)
 endif()
+set(TESTFILTER "" CACHE STRING "Filter string for ctest to selectively only run specific tests (ctest -R)")
 
 list(APPEND CMAKE_MODULE_PATH ${PX4_SOURCE_DIR}/cmake/gtest/)
 include(px4_add_gtest)
 
 if(BUILD_TESTING)
 	include(gtest)
-	add_custom_target(unit_test COMMAND GTEST_COLOR=1 ${CMAKE_CTEST_COMMAND} -V -R UnitTest- USES_TERMINAL)
+	add_custom_target(unit_test COMMAND GTEST_COLOR=1 ${CMAKE_CTEST_COMMAND} -V -R ${TESTFILTER} USES_TERMINAL)
 endif()
 
 #=============================================================================
diff --git a/Makefile b/Makefile
index a0be770fe72d..41730ce25de8 100644
--- a/Makefile
+++ b/Makefile
@@ -349,6 +349,7 @@ unit_test:
 tests:
 	$(eval CMAKE_ARGS += -DCMAKE_TESTING=ON)
 	$(eval CMAKE_ARGS += -DCONFIG=px4_sitl_test)
+	$(eval CMAKE_ARGS += -DTESTFILTER=$(TESTFILTER))
 	$(eval ARGS += test_results)
 	$(eval ASAN_OPTIONS += color=always:check_initialization_order=1:detect_stack_use_after_return=1)
 	$(eval UBSAN_OPTIONS += color=always)
diff --git a/platforms/posix/cmake/sitl_tests.cmake b/platforms/posix/cmake/sitl_tests.cmake
index 1deff3d85063..010428d84254 100644
--- a/platforms/posix/cmake/sitl_tests.cmake
+++ b/platforms/posix/cmake/sitl_tests.cmake
@@ -147,7 +147,7 @@ endforeach()
 
 
 add_custom_target(test_results
-		COMMAND ${CMAKE_CTEST_COMMAND} --output-on-failure -T Test
+		COMMAND ${CMAKE_CTEST_COMMAND} --output-on-failure -T Test -R ${TESTFILTER}
 		DEPENDS
 			px4
 			examples__dyn_hello