From 5020dfdc3d08d7737bed97841b0ec6bc41c80187 Mon Sep 17 00:00:00 2001
From: Matthias Grob <maetugr@gmail.com>
Date: Sun, 14 Apr 2019 20:55:35 +0200
Subject: [PATCH] Testing: switch unit tests to BUILD_TESTING

---
 CMakeLists.txt                         | 16 ++++++++--------
 Makefile                               | 14 +++++++++-----
 cmake/gtest/px4_add_gtest.cmake        |  2 +-
 platforms/posix/cmake/sitl_tests.cmake |  1 +
 4 files changed, 19 insertions(+), 14 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index d1c65c2c09..409dc2c3f3 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -299,10 +299,6 @@ if (${PX4_PLATFORM} STREQUAL "posix")
 	if (NOT CMAKE_INSTALL_PREFIX)
 		set(CMAKE_INSTALL_PREFIX "/usr" CACHE PATH "Install path prefix" FORCE)
 	endif()
-
-	# cmake testing only on posix
-	enable_testing()
-	include(CTest)
 endif()
 
 #=============================================================================
@@ -417,14 +413,18 @@ endif()
 # Testing - Automatic unit and integration testing with CTest
 #
 
-option(unit_testing "Configure unit test targets" OFF)
+# optionally enable cmake testing (supported only on posix)
+option(CMAKE_TESTING "Configure test targets" OFF)
+if(CMAKE_TESTING)
+	include(CTest)
+endif()
 
 list(APPEND CMAKE_MODULE_PATH ${PX4_SOURCE_DIR}/cmake/gtest/)
 include(px4_add_gtest)
 
-if(unit_testing)
-   include(gtest)
-   add_custom_target(unit_test COMMAND GTEST_COLOR=1 ${CMAKE_CTEST_COMMAND} -V -R UnitTest- USES_TERMINAL)
+if(BUILD_TESTING)
+	include(gtest)
+	add_custom_target(unit_test COMMAND GTEST_COLOR=1 ${CMAKE_CTEST_COMMAND} -V -R UnitTest- USES_TERMINAL)
 endif()
 
 #=============================================================================
diff --git a/Makefile b/Makefile
index a6550ba2e3..a0be770fe7 100644
--- a/Makefile
+++ b/Makefile
@@ -341,14 +341,18 @@ format:
 .PHONY: rostest python_coverage
 
 unit_test:
-	$(eval CMAKE_ARGS += -Dunit_testing=ON)
+	$(eval CMAKE_ARGS += -DCMAKE_TESTING=ON)
+	$(eval CMAKE_ARGS += -DCONFIG=px4_sitl_test)
 	$(eval ARGS += unit_test)
-	$(call cmake-build,px4_sitl_default)
+	$(call cmake-build,px4_sitl_test)
 
 tests:
-	@$(MAKE) --no-print-directory px4_sitl_test test_results \
-	ASAN_OPTIONS="color=always:check_initialization_order=1:detect_stack_use_after_return=1" \
-	UBSAN_OPTIONS="color=always"
+	$(eval CMAKE_ARGS += -DCMAKE_TESTING=ON)
+	$(eval CMAKE_ARGS += -DCONFIG=px4_sitl_test)
+	$(eval ARGS += test_results)
+	$(eval ASAN_OPTIONS += color=always:check_initialization_order=1:detect_stack_use_after_return=1)
+	$(eval UBSAN_OPTIONS += color=always)
+	$(call cmake-build,px4_sitl_test)
 
 tests_coverage:
 	@$(MAKE) clean
diff --git a/cmake/gtest/px4_add_gtest.cmake b/cmake/gtest/px4_add_gtest.cmake
index 2d50f08977..5125104e7c 100644
--- a/cmake/gtest/px4_add_gtest.cmake
+++ b/cmake/gtest/px4_add_gtest.cmake
@@ -41,7 +41,7 @@ include(px4_base)
 #
 function(px4_add_gtest)
 	# skip if unit testing is not configured
-	if(unit_testing)
+	if(BUILD_TESTING)
 		# parse source file and library dependencies from arguments
 		px4_parse_function_args(
 			NAME px4_add_gtest
diff --git a/platforms/posix/cmake/sitl_tests.cmake b/platforms/posix/cmake/sitl_tests.cmake
index 87aa14895c..0545b9ce68 100644
--- a/platforms/posix/cmake/sitl_tests.cmake
+++ b/platforms/posix/cmake/sitl_tests.cmake
@@ -151,6 +151,7 @@ add_custom_target(test_results
 			px4
 			examples__dyn_hello
 			test_mixer_multirotor
+			unit_test
 		USES_TERMINAL
 		COMMENT "Running tests in sitl"
 		WORKING_DIRECTORY ${PX4_BINARY_DIR})
-- 
GitLab