From e22ac1a473729ea95c2f1ea0bfea56d09a7a1c98 Mon Sep 17 00:00:00 2001 From: Matthias Grob Date: Sat, 2 Mar 2019 18:18:00 +0100 Subject: [PATCH 01/12] Add unit testing possibility using googletest on POSIX --- CMakeLists.txt | 14 ++++ Makefile | 7 +- cmake/gtest/CMakeLists.txt.in | 18 +++++ cmake/gtest/gtest.cmake | 43 ++++++++++++ cmake/gtest/px4_add_gtest.cmake | 68 +++++++++++++++++++ src/include/visibility.h | 2 +- .../AttitudeControl/AttitudeControlTest.cpp | 12 ++++ .../AttitudeControl/CMakeLists.txt | 2 + 8 files changed, 164 insertions(+), 2 deletions(-) create mode 100644 cmake/gtest/CMakeLists.txt.in create mode 100644 cmake/gtest/gtest.cmake create mode 100644 cmake/gtest/px4_add_gtest.cmake create mode 100644 src/modules/mc_att_control/AttitudeControl/AttitudeControlTest.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index d1815f6d8c44..5ddc25ed08f0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -413,6 +413,20 @@ if (NOT EXTERNAL_MODULES_LOCATION STREQUAL "") endforeach() endif() +#============================================================================= +# Testing - Automatic unit and integration testing with CTest +# + +option(unit_testing "Configure unit test targets" OFF) + +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 Test USES_TERMINAL) +endif() + #============================================================================= # subdirectories # diff --git a/Makefile b/Makefile index c1f0202204a6..a6550ba2e3dc 100644 --- a/Makefile +++ b/Makefile @@ -337,9 +337,14 @@ format: # Testing # -------------------------------------------------------------------- -.PHONY: tests tests_coverage tests_mission tests_mission_coverage tests_offboard tests_avoidance +.PHONY: tests tests_coverage tests_mission tests_mission_coverage tests_offboard tests_avoidance unit_test .PHONY: rostest python_coverage +unit_test: + $(eval CMAKE_ARGS += -Dunit_testing=ON) + $(eval ARGS += unit_test) + $(call cmake-build,px4_sitl_default) + tests: @$(MAKE) --no-print-directory px4_sitl_test test_results \ ASAN_OPTIONS="color=always:check_initialization_order=1:detect_stack_use_after_return=1" \ diff --git a/cmake/gtest/CMakeLists.txt.in b/cmake/gtest/CMakeLists.txt.in new file mode 100644 index 000000000000..06348418414d --- /dev/null +++ b/cmake/gtest/CMakeLists.txt.in @@ -0,0 +1,18 @@ +cmake_minimum_required(VERSION 2.8.4) + +project(googletest-download NONE) + +include(ExternalProject) +ExternalProject_Add(googletest + URL https://github.com/google/googletest/archive/master.zip + SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/googletest-src" + BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/googletest-build" + CONFIGURE_COMMAND "" + BUILD_COMMAND "" + INSTALL_COMMAND "" + TEST_COMMAND "" + # Wrap download, configure and build steps in a script to log output + LOG_DOWNLOAD ON + LOG_CONFIGURE ON + LOG_BUILD ON +) diff --git a/cmake/gtest/gtest.cmake b/cmake/gtest/gtest.cmake new file mode 100644 index 000000000000..94c4103ce5c2 --- /dev/null +++ b/cmake/gtest/gtest.cmake @@ -0,0 +1,43 @@ +############################################################################ +# +# Copyright (c) 2019 PX4 Development Team. All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in +# the documentation and/or other materials provided with the +# distribution. +# 3. Neither the name PX4 nor the names of its contributors may be +# used to endorse or promote products derived from this software +# without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +# POSSIBILITY OF SUCH DAMAGE. +# +############################################################################ + +# Download and unpack googletest at configure time +configure_file(${CMAKE_CURRENT_LIST_DIR}/CMakeLists.txt.in googletest-download/CMakeLists.txt) +execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" . RESULT_VARIABLE result1 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download) +execute_process(COMMAND ${CMAKE_COMMAND} --build . RESULT_VARIABLE result2 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download) +if(result1 OR result2) + message(FATAL_ERROR "Preparing googletest failed: ${result1} ${result2}") +endif() + +# Add googletest, defines gtest and gtest_main targets +add_subdirectory(${CMAKE_CURRENT_BINARY_DIR}/googletest-src ${CMAKE_CURRENT_BINARY_DIR}/googletest-build EXCLUDE_FROM_ALL) diff --git a/cmake/gtest/px4_add_gtest.cmake b/cmake/gtest/px4_add_gtest.cmake new file mode 100644 index 000000000000..42124acac328 --- /dev/null +++ b/cmake/gtest/px4_add_gtest.cmake @@ -0,0 +1,68 @@ +############################################################################ +# +# Copyright (c) 2019 PX4 Development Team. All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in +# the documentation and/or other materials provided with the +# distribution. +# 3. Neither the name PX4 nor the names of its contributors may be +# used to endorse or promote products derived from this software +# without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +# POSSIBILITY OF SUCH DAMAGE. +# +############################################################################ + +include(px4_base) + +#============================================================================= +# +# px4_add_gtest +# +# Adds a googletest unit test to the unit_test target. +# +function(px4_add_gtest) + # skip if unit testing is not configured + if(unit_testing) + # parse source file and library dependencies from arguments + px4_parse_function_args( + NAME px4_add_gtest + ONE_VALUE SRC + MULTI_VALUE LINKLIBS + REQUIRED SRC + ARGN ${ARGN}) + + # infer test name from source filname without extension + get_filename_component(TESTNAME ${SRC} NAME_WE) + + # build a binary for the unit test + add_executable(${TESTNAME} EXCLUDE_FROM_ALL ${SRC}) + + # link the libary to test and gtest + target_link_libraries(${TESTNAME} ${LINKLIBS} gtest_main) + + # add the test to the ctest plan + add_test(NAME ${TESTNAME} COMMAND ${TESTNAME}) + + # attach it to the unit test target + add_dependencies(unit_test ${TESTNAME}) + endif() +endfunction() diff --git a/src/include/visibility.h b/src/include/visibility.h index 1a956f02e8e5..3717b6370497 100644 --- a/src/include/visibility.h +++ b/src/include/visibility.h @@ -86,7 +86,7 @@ #ifdef __cplusplus #include #endif -#pragma GCC poison exit +//#pragma GCC poison exit #include diff --git a/src/modules/mc_att_control/AttitudeControl/AttitudeControlTest.cpp b/src/modules/mc_att_control/AttitudeControl/AttitudeControlTest.cpp new file mode 100644 index 000000000000..7dc005507851 --- /dev/null +++ b/src/modules/mc_att_control/AttitudeControl/AttitudeControlTest.cpp @@ -0,0 +1,12 @@ +#include +#include + +using namespace matrix; + +TEST(AttitudeControlTest, AllZeroCase) { + AttitudeControl attitude_control; + matrix::Vector3f rate_setpoint = attitude_control.update(Quatf(), Quatf(), 0.f); + EXPECT_EQ(rate_setpoint(0), 0.f); + EXPECT_EQ(rate_setpoint(1), 0.f); + EXPECT_EQ(rate_setpoint(2), 0.f); +} diff --git a/src/modules/mc_att_control/AttitudeControl/CMakeLists.txt b/src/modules/mc_att_control/AttitudeControl/CMakeLists.txt index 6e29d4762ac0..f4b1ace74ac9 100644 --- a/src/modules/mc_att_control/AttitudeControl/CMakeLists.txt +++ b/src/modules/mc_att_control/AttitudeControl/CMakeLists.txt @@ -38,3 +38,5 @@ target_include_directories(AttitudeControl PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} ) + +px4_add_gtest(SRC AttitudeControlTest.cpp LINKLIBS AttitudeControl) From 407be75018ff60dfb0335399a4b54b122bded762 Mon Sep 17 00:00:00 2001 From: Matthias Grob Date: Sun, 3 Mar 2019 17:55:59 +0100 Subject: [PATCH 02/12] Improve ctest unit test naming and filtering --- CMakeLists.txt | 2 +- cmake/gtest/px4_add_gtest.cmake | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 5ddc25ed08f0..d1c65c2c09d4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -424,7 +424,7 @@ include(px4_add_gtest) if(unit_testing) include(gtest) - add_custom_target(unit_test COMMAND GTEST_COLOR=1 ${CMAKE_CTEST_COMMAND} -V -R Test USES_TERMINAL) + add_custom_target(unit_test COMMAND GTEST_COLOR=1 ${CMAKE_CTEST_COMMAND} -V -R UnitTest- USES_TERMINAL) endif() #============================================================================= diff --git a/cmake/gtest/px4_add_gtest.cmake b/cmake/gtest/px4_add_gtest.cmake index 42124acac328..2d50f089772e 100644 --- a/cmake/gtest/px4_add_gtest.cmake +++ b/cmake/gtest/px4_add_gtest.cmake @@ -50,8 +50,10 @@ function(px4_add_gtest) REQUIRED SRC ARGN ${ARGN}) - # infer test name from source filname without extension + # infer test name from source filname get_filename_component(TESTNAME ${SRC} NAME_WE) + string(REPLACE Test "" TESTNAME ${TESTNAME}) + set(TESTNAME UnitTest-${TESTNAME}) # build a binary for the unit test add_executable(${TESTNAME} EXCLUDE_FROM_ALL ${SRC}) From 58399be651c8e8d3cf6aaea3625d671a177a381e Mon Sep 17 00:00:00 2001 From: Matthias Grob Date: Sat, 9 Mar 2019 17:44:24 +0100 Subject: [PATCH 03/12] AttitudeControlTest: fix style --- .../mc_att_control/AttitudeControl/AttitudeControlTest.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/modules/mc_att_control/AttitudeControl/AttitudeControlTest.cpp b/src/modules/mc_att_control/AttitudeControl/AttitudeControlTest.cpp index 7dc005507851..abb35142c48c 100644 --- a/src/modules/mc_att_control/AttitudeControl/AttitudeControlTest.cpp +++ b/src/modules/mc_att_control/AttitudeControl/AttitudeControlTest.cpp @@ -3,7 +3,8 @@ using namespace matrix; -TEST(AttitudeControlTest, AllZeroCase) { +TEST(AttitudeControlTest, AllZeroCase) +{ AttitudeControl attitude_control; matrix::Vector3f rate_setpoint = attitude_control.update(Quatf(), Quatf(), 0.f); EXPECT_EQ(rate_setpoint(0), 0.f); From 5f1d6333531d99e9f9b2924ea4589a5e4e5daa48 Mon Sep 17 00:00:00 2001 From: Matthias Grob Date: Sat, 9 Mar 2019 17:44:31 +0100 Subject: [PATCH 04/12] gtest: download specific version --- cmake/gtest/CMakeLists.txt.in | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmake/gtest/CMakeLists.txt.in b/cmake/gtest/CMakeLists.txt.in index 06348418414d..fb9ff2f0ab99 100644 --- a/cmake/gtest/CMakeLists.txt.in +++ b/cmake/gtest/CMakeLists.txt.in @@ -4,7 +4,7 @@ project(googletest-download NONE) include(ExternalProject) ExternalProject_Add(googletest - URL https://github.com/google/googletest/archive/master.zip + URL https://github.com/google/googletest/archive/8b6d3f9c4a774bef3081195d422993323b6bb2e0.zip SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/googletest-src" BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/googletest-build" CONFIGURE_COMMAND "" From 8c165f0671f069617b1acd3197b2e5189bc6ae0b Mon Sep 17 00:00:00 2001 From: Matthias Grob Date: Sun, 10 Mar 2019 15:14:08 +0100 Subject: [PATCH 05/12] gtest: fix poisoned exit in compile flags visibility.h is included globally in PX4 via cmake compile flags. It contains poisoning the exit() command which is used by gtest to close the test application. Removing the flag for gtest compilation fixes the compile error: gtest.cc:4757:7: error: attempt to use poisoned "exit" --- cmake/gtest/gtest.cmake | 6 ++++++ src/include/visibility.h | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/cmake/gtest/gtest.cmake b/cmake/gtest/gtest.cmake index 94c4103ce5c2..3b74cfb211be 100644 --- a/cmake/gtest/gtest.cmake +++ b/cmake/gtest/gtest.cmake @@ -41,3 +41,9 @@ endif() # Add googletest, defines gtest and gtest_main targets add_subdirectory(${CMAKE_CURRENT_BINARY_DIR}/googletest-src ${CMAKE_CURRENT_BINARY_DIR}/googletest-build EXCLUDE_FROM_ALL) + +# Remove visibility.h from the compile flags for gtest because of poisoned exit() +get_target_property(GTEST_COMPILE_FLAGS gtest COMPILE_OPTIONS) +list(REMOVE_ITEM GTEST_COMPILE_FLAGS "-include") +list(REMOVE_ITEM GTEST_COMPILE_FLAGS "visibility.h") +set_target_properties(gtest PROPERTIES COMPILE_OPTIONS "${GTEST_COMPILE_FLAGS}") diff --git a/src/include/visibility.h b/src/include/visibility.h index 3717b6370497..1a956f02e8e5 100644 --- a/src/include/visibility.h +++ b/src/include/visibility.h @@ -86,7 +86,7 @@ #ifdef __cplusplus #include #endif -//#pragma GCC poison exit +#pragma GCC poison exit #include From d6a42d52862ee27fae3522957e8f02e64f21df14 Mon Sep 17 00:00:00 2001 From: Matthias Grob Date: Sun, 10 Mar 2019 23:30:24 +0100 Subject: [PATCH 06/12] AttitudeControlTest: compare vectors, not every element --- .../mc_att_control/AttitudeControl/AttitudeControlTest.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/modules/mc_att_control/AttitudeControl/AttitudeControlTest.cpp b/src/modules/mc_att_control/AttitudeControl/AttitudeControlTest.cpp index abb35142c48c..81c08778983b 100644 --- a/src/modules/mc_att_control/AttitudeControl/AttitudeControlTest.cpp +++ b/src/modules/mc_att_control/AttitudeControl/AttitudeControlTest.cpp @@ -7,7 +7,5 @@ TEST(AttitudeControlTest, AllZeroCase) { AttitudeControl attitude_control; matrix::Vector3f rate_setpoint = attitude_control.update(Quatf(), Quatf(), 0.f); - EXPECT_EQ(rate_setpoint(0), 0.f); - EXPECT_EQ(rate_setpoint(1), 0.f); - EXPECT_EQ(rate_setpoint(2), 0.f); + EXPECT_EQ(rate_setpoint, Vector3f()); } From a2e95e06fac9f7d00f14abcc920a70b616a005eb Mon Sep 17 00:00:00 2001 From: Matthias Grob Date: Sun, 14 Apr 2019 20:55:35 +0200 Subject: [PATCH 07/12] 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 d1c65c2c09d4..409dc2c3f341 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 a6550ba2e3dc..a0be770fe72d 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 2d50f089772e..5125104e7c4f 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 87aa14895c3b..0545b9ce6871 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}) From 0e8041fb8dc5f77b8f7f35856c59e03defd6e778 Mon Sep 17 00:00:00 2001 From: Matthias Grob Date: Tue, 23 Apr 2019 07:43:35 +0100 Subject: [PATCH 08/12] sitl_tests: add prefix "sitl-" for regex test filtering --- cmake/gtest/px4_add_gtest.cmake | 2 +- platforms/posix/cmake/sitl_tests.cmake | 9 +++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/cmake/gtest/px4_add_gtest.cmake b/cmake/gtest/px4_add_gtest.cmake index 5125104e7c4f..8fd367b3da6c 100644 --- a/cmake/gtest/px4_add_gtest.cmake +++ b/cmake/gtest/px4_add_gtest.cmake @@ -53,7 +53,7 @@ function(px4_add_gtest) # infer test name from source filname get_filename_component(TESTNAME ${SRC} NAME_WE) string(REPLACE Test "" TESTNAME ${TESTNAME}) - set(TESTNAME UnitTest-${TESTNAME}) + set(TESTNAME unit-${TESTNAME}) # build a binary for the unit test add_executable(${TESTNAME} EXCLUDE_FROM_ALL ${SRC}) diff --git a/platforms/posix/cmake/sitl_tests.cmake b/platforms/posix/cmake/sitl_tests.cmake index 0545b9ce6871..1deff3d85063 100644 --- a/platforms/posix/cmake/sitl_tests.cmake +++ b/platforms/posix/cmake/sitl_tests.cmake @@ -54,9 +54,10 @@ if (CMAKE_SYSTEM_NAME STREQUAL "CYGWIN") endif() foreach(test_name ${tests}) + set(test_name_prefix sitl-${test_name}) configure_file(${PX4_SOURCE_DIR}/posix-configs/SITL/init/test/test_template.in ${PX4_SOURCE_DIR}/posix-configs/SITL/init/test/test_${test_name}_generated) - add_test(NAME ${test_name} + add_test(NAME ${test_name_prefix} COMMAND ${PX4_SOURCE_DIR}/Tools/sitl_run.sh $ none @@ -66,10 +67,10 @@ foreach(test_name ${tests}) ${PX4_BINARY_DIR} WORKING_DIRECTORY ${SITL_WORKING_DIR}) - set_tests_properties(${test_name} PROPERTIES FAIL_REGULAR_EXPRESSION "${test_name} FAILED") - set_tests_properties(${test_name} PROPERTIES PASS_REGULAR_EXPRESSION "${test_name} PASSED") + set_tests_properties(${test_name_prefix} PROPERTIES FAIL_REGULAR_EXPRESSION "${test_name} FAILED") + set_tests_properties(${test_name_prefix} PROPERTIES PASS_REGULAR_EXPRESSION "${test_name} PASSED") - sanitizer_fail_test_on_error(${test_name}) + sanitizer_fail_test_on_error(${test_name_prefix}) endforeach() From c5de3d0d2c643a4b44eee7b3ece08ded55ba30fa Mon Sep 17 00:00:00 2001 From: Matthias Grob Date: Tue, 23 Apr 2019 08:36:03 +0100 Subject: [PATCH 09/12] 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 From 1b0cfed0c22a157f284a2c4101c0608d38e9f13b Mon Sep 17 00:00:00 2001 From: Matthias Grob Date: Tue, 23 Apr 2019 08:41:21 +0100 Subject: [PATCH 10/12] Makefile: fix reconfigure check passing with prefix substring Example: Before when you passed "make tests TESTFILTER=Attitude" and subsequently "make tests TESTFILTER=Atti" it found the string "TESTFILTER=Atti" in "TESTFILTER=Attitude" and hence the check if the configuration is already correct passed. The fix checks for the configuration parameter including the subsequent space separator and after that strips the space away again such that the list VERIFIED_CMAKE_OPTIONS doesn't contain trailing spaces. --- Makefile | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index 41730ce25de8..6c3f596546fa 100644 --- a/Makefile +++ b/Makefile @@ -56,9 +56,12 @@ endif # directory build/px4_fmu-v2_default and then call make # in that directory with the target upload. -# explicity set default build target +# explicity set default build target all: px4_sitl_default +# define a space character to be able to explicitly find it in strings +space := $(subst ,, ) + # Parsing # -------------------------------------------------------------------- # assume 1st argument passed is the main target, the @@ -165,8 +168,8 @@ define cmake-cache-check @$(eval CACHED_CMAKE_OPTIONS = $(shell cd $(BUILD_DIR) 2>/dev/null && cmake -L 2>/dev/null | sed -n 's/\([^[:blank:]]*\):[^[:blank:]]*\(=[^[:blank:]]*\)/\1\2/gp' )) @# transform the options in CMAKE_ARGS into the OPTION=VALUE format without -D @$(eval DESIRED_CMAKE_OPTIONS = $(shell echo $(CMAKE_ARGS) | sed -n 's/-D\([^[:blank:]]*=[^[:blank:]]*\)/\1/gp' )) - @# find each currently desired option in the already cached ones - @$(eval VERIFIED_CMAKE_OPTIONS = $(foreach option,$(DESIRED_CMAKE_OPTIONS),$(findstring $(option),$(CACHED_CMAKE_OPTIONS)))) + @# find each currently desired option in the already cached ones making sure the complete configured string value is the same + @$(eval VERIFIED_CMAKE_OPTIONS = $(foreach option,$(DESIRED_CMAKE_OPTIONS),$(strip $(findstring $(option)$(space),$(CACHED_CMAKE_OPTIONS))))) @# if the complete list of desired options is found in the list of verified options we don't need to reconfigure and CMAKE_CACHE_CHECK stays empty @$(eval CMAKE_CACHE_CHECK = $(if $(findstring $(DESIRED_CMAKE_OPTIONS),$(VERIFIED_CMAKE_OPTIONS)),,y)) endef @@ -474,7 +477,6 @@ distclean: gazeboclean $(error "$@ cannot be the first argument. Use '$(MAKE) help|list_config_targets' to get a list of all possible [configuration] targets."),@#) # Print a list of non-config targets (based on http://stackoverflow.com/a/26339924/1487069) -space := $(subst ,, ) help: @echo "Usage: $(MAKE) " @echo "Where is one of:" From e11a7dcbf155b0fede7667919d1f6bc123e179d7 Mon Sep 17 00:00:00 2001 From: Matthias Grob Date: Tue, 23 Apr 2019 10:22:14 +0100 Subject: [PATCH 11/12] Testing: Clean up unit_test target because test_results does all tests now --- CMakeLists.txt | 18 +++++++++++++++--- Makefile | 8 +------- cmake/gtest/px4_add_gtest.cmake | 4 ++-- platforms/posix/cmake/sitl_tests.cmake | 13 ------------- 4 files changed, 18 insertions(+), 25 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 63ec99a73150..42c9381543fb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -416,18 +416,30 @@ endif() # optionally enable cmake testing (supported only on posix) option(CMAKE_TESTING "Configure test targets" OFF) if(CMAKE_TESTING) - include(CTest) + include(CTest) # sets BUILD_TESTING variable endif() + +# enable test filtering to run only specific tests with the ctest -R regex functionality set(TESTFILTER "" CACHE STRING "Filter string for ctest to selectively only run specific tests (ctest -R)") +# if testing is enabled download and configure gtest 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 ${TESTFILTER} USES_TERMINAL) endif() +add_custom_target(test_results + COMMAND GTEST_COLOR=1 ${CMAKE_CTEST_COMMAND} --output-on-failure -T Test -R ${TESTFILTER} USES_TERMINAL + DEPENDS + px4 + examples__dyn_hello + test_mixer_multirotor + USES_TERMINAL + COMMENT "Running tests" + WORKING_DIRECTORY ${PX4_BINARY_DIR}) +set_target_properties(test_results PROPERTIES EXCLUDE_FROM_ALL TRUE) + #============================================================================= # subdirectories # diff --git a/Makefile b/Makefile index 6c3f596546fa..0c378cffe04e 100644 --- a/Makefile +++ b/Makefile @@ -340,15 +340,9 @@ format: # Testing # -------------------------------------------------------------------- -.PHONY: tests tests_coverage tests_mission tests_mission_coverage tests_offboard tests_avoidance unit_test +.PHONY: tests tests_coverage tests_mission tests_mission_coverage tests_offboard tests_avoidance .PHONY: rostest python_coverage -unit_test: - $(eval CMAKE_ARGS += -DCMAKE_TESTING=ON) - $(eval CMAKE_ARGS += -DCONFIG=px4_sitl_test) - $(eval ARGS += unit_test) - $(call cmake-build,px4_sitl_test) - tests: $(eval CMAKE_ARGS += -DCMAKE_TESTING=ON) $(eval CMAKE_ARGS += -DCONFIG=px4_sitl_test) diff --git a/cmake/gtest/px4_add_gtest.cmake b/cmake/gtest/px4_add_gtest.cmake index 8fd367b3da6c..4ae03387db20 100644 --- a/cmake/gtest/px4_add_gtest.cmake +++ b/cmake/gtest/px4_add_gtest.cmake @@ -37,7 +37,7 @@ include(px4_base) # # px4_add_gtest # -# Adds a googletest unit test to the unit_test target. +# Adds a googletest unit test to the test_results target. # function(px4_add_gtest) # skip if unit testing is not configured @@ -65,6 +65,6 @@ function(px4_add_gtest) add_test(NAME ${TESTNAME} COMMAND ${TESTNAME}) # attach it to the unit test target - add_dependencies(unit_test ${TESTNAME}) + add_dependencies(test_results ${TESTNAME}) endif() endfunction() diff --git a/platforms/posix/cmake/sitl_tests.cmake b/platforms/posix/cmake/sitl_tests.cmake index 010428d84254..dcba66cb863a 100644 --- a/platforms/posix/cmake/sitl_tests.cmake +++ b/platforms/posix/cmake/sitl_tests.cmake @@ -145,19 +145,6 @@ foreach(cmd_name ${test_cmds}) set_tests_properties(posix_${cmd_name} PROPERTIES PASS_REGULAR_EXPRESSION "Shutting down") endforeach() - -add_custom_target(test_results - COMMAND ${CMAKE_CTEST_COMMAND} --output-on-failure -T Test -R ${TESTFILTER} - DEPENDS - px4 - examples__dyn_hello - test_mixer_multirotor - unit_test - USES_TERMINAL - COMMENT "Running tests in sitl" - WORKING_DIRECTORY ${PX4_BINARY_DIR}) -set_target_properties(test_results PROPERTIES EXCLUDE_FROM_ALL TRUE) - if (CMAKE_BUILD_TYPE STREQUAL Coverage) setup_target_for_coverage(test_coverage "${CMAKE_CTEST_COMMAND} --output-on-failure -T Test" tests) setup_target_for_coverage(generate_coverage "${CMAKE_COMMAND} -E echo" generic) From 715a9ba37dbd317235bc83f9ebffc57090418453 Mon Sep 17 00:00:00 2001 From: Matthias Grob Date: Thu, 25 Apr 2019 08:16:45 +0200 Subject: [PATCH 12/12] Testing: correct license headers --- CMakeLists.txt | 2 +- .../AttitudeControl/AttitudeControlTest.cpp | 33 +++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 42c9381543fb..6e6be184abe9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ ############################################################################ # -# Copyright (c) 2017 PX4 Development Team. All rights reserved. +# Copyright (c) 2017 - 2019 PX4 Development Team. All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions diff --git a/src/modules/mc_att_control/AttitudeControl/AttitudeControlTest.cpp b/src/modules/mc_att_control/AttitudeControl/AttitudeControlTest.cpp index 81c08778983b..ad07cb9d866f 100644 --- a/src/modules/mc_att_control/AttitudeControl/AttitudeControlTest.cpp +++ b/src/modules/mc_att_control/AttitudeControl/AttitudeControlTest.cpp @@ -1,3 +1,36 @@ +/**************************************************************************** + * + * Copyright (C) 2019 PX4 Development Team. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name PX4 nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + #include #include