From be8d4e436179eb9e6d6db3f57fc6c85b2f292840 Mon Sep 17 00:00:00 2001 From: Julian Oes Date: Wed, 29 May 2019 14:09:55 +0200 Subject: [PATCH] Add ntest to build This adds the ntest systemcmd using ntestlib to the build. This is the equivalent of gtest testing on Posix for NuttX. This works as follows: 1. At cmake configure time all tests are added to global cmake lists. 2. Then when ntest is configured (last), the collected test sources and dependencies are added to the ntest build. When compiling for NuttX we need to include ntestlib.h, when building for Posix we need to include gtest.h (see px4_unittesting.h). --- CMakeLists.txt | 3 +- boards/px4/fmu-v2/test.cmake | 1 + boards/px4/fmu-v3/default.cmake | 1 + boards/px4/fmu-v4/default.cmake | 1 + boards/px4/fmu-v5/default.cmake | 1 + cmake/gtest/px4_add_gtest.cmake | 45 ++++++++++++------ platforms/nuttx/CMakeLists.txt | 10 +++- .../test/src/lockstep_scheduler_test.cpp | 2 +- src/lib/hysteresis/HysteresisTest.cpp | 2 +- .../AttitudeControl/AttitudeControlTest.cpp | 5 +- .../mc_pos_control/Takeoff/TakeoffTest.cpp | 5 +- src/platforms/px4_unittesting.h | 47 +++++++++++++++++++ 12 files changed, 100 insertions(+), 23 deletions(-) create mode 100644 src/platforms/px4_unittesting.h diff --git a/CMakeLists.txt b/CMakeLists.txt index ee42c30777b3..ad5160383e0d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -425,9 +425,10 @@ 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 testing is enabled download and configure gtest if(BUILD_TESTING) include(gtest) endif() diff --git a/boards/px4/fmu-v2/test.cmake b/boards/px4/fmu-v2/test.cmake index 7f631eb9bac4..16d3f13287d9 100644 --- a/boards/px4/fmu-v2/test.cmake +++ b/boards/px4/fmu-v2/test.cmake @@ -107,6 +107,7 @@ px4_add_board( #topic_listener tune_control ver + ntest # needs to be last EXAMPLES #bottle_drop # OBC challenge diff --git a/boards/px4/fmu-v3/default.cmake b/boards/px4/fmu-v3/default.cmake index 7bc87cb65531..0d524c1d3f94 100644 --- a/boards/px4/fmu-v3/default.cmake +++ b/boards/px4/fmu-v3/default.cmake @@ -114,6 +114,7 @@ px4_add_board( tune_control usb_connected ver + ntest # needs to be last EXAMPLES bottle_drop # OBC challenge diff --git a/boards/px4/fmu-v4/default.cmake b/boards/px4/fmu-v4/default.cmake index d7a564dccfc1..579e71bdbc91 100644 --- a/boards/px4/fmu-v4/default.cmake +++ b/boards/px4/fmu-v4/default.cmake @@ -98,6 +98,7 @@ px4_add_board( tune_control usb_connected ver + ntest # needs to be last EXAMPLES bottle_drop # OBC challenge diff --git a/boards/px4/fmu-v5/default.cmake b/boards/px4/fmu-v5/default.cmake index 5dfd7d46714c..5be5810e6d25 100644 --- a/boards/px4/fmu-v5/default.cmake +++ b/boards/px4/fmu-v5/default.cmake @@ -114,6 +114,7 @@ px4_add_board( tune_control usb_connected ver + ntest # needs to be last EXAMPLES bottle_drop # OBC challenge diff --git a/cmake/gtest/px4_add_gtest.cmake b/cmake/gtest/px4_add_gtest.cmake index 4ae03387db20..c335b9d1f05d 100644 --- a/cmake/gtest/px4_add_gtest.cmake +++ b/cmake/gtest/px4_add_gtest.cmake @@ -40,31 +40,46 @@ include(px4_base) # Adds a googletest unit test to the test_results target. # function(px4_add_gtest) - # skip if unit testing is not configured - if(BUILD_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}) + # 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 - get_filename_component(TESTNAME ${SRC} NAME_WE) - string(REPLACE Test "" TESTNAME ${TESTNAME}) - set(TESTNAME unit-${TESTNAME}) + # infer test name from source filname + get_filename_component(TESTNAME ${SRC} NAME_WE) + string(REPLACE Test "" TESTNAME ${TESTNAME}) + + if (${PX4_PLATFORM} STREQUAL "nuttx") + get_property(sources GLOBAL PROPERTY ntest_test_sources) + list(APPEND sources ${CMAKE_CURRENT_SOURCE_DIR}/${SRC}) + set_property(GLOBAL PROPERTY ntest_test_sources ${sources}) + + get_property(dependencies GLOBAL PROPERTY ntest_test_dependencies) + foreach(dep ${dependencies}) + list(APPEND dependencies ${dep}) + endforeach() + set_property(GLOBAL PROPERTY ntest_test_dependencies ${dependencies}) + + # Note: If the systemcmd ntest is not build, these properties won't be + # taken into account. + + else() + set(TESTNAME unit-${TESTNAME}) # build a binary for the unit test add_executable(${TESTNAME} EXCLUDE_FROM_ALL ${SRC}) + # attach it to the unit test target + add_dependencies(test_results ${TESTNAME}) + # 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(test_results ${TESTNAME}) endif() endfunction() diff --git a/platforms/nuttx/CMakeLists.txt b/platforms/nuttx/CMakeLists.txt index 406e6432ff65..8252af832eba 100644 --- a/platforms/nuttx/CMakeLists.txt +++ b/platforms/nuttx/CMakeLists.txt @@ -108,7 +108,15 @@ target_link_libraries(px4 PRIVATE gcc ) -target_link_libraries(px4 PRIVATE ${module_libraries}) +foreach(module ${module_libraries}) + # Hack for ntest to avoid the linker from stripping the static global test registrars. + if ("${module}" STREQUAL "ntest") + target_link_libraries(px4 PRIVATE -Wl,--whole-archive ${module} -Wl,--no-whole-archive) + message(STATUS "Adding ntest") + else() + target_link_libraries(px4 PRIVATE ${module}) + endif() +endforeach() if (config_romfs_root) add_subdirectory(${PX4_SOURCE_DIR}/ROMFS ${PX4_BINARY_DIR}/ROMFS) diff --git a/platforms/posix/src/lockstep_scheduler/test/src/lockstep_scheduler_test.cpp b/platforms/posix/src/lockstep_scheduler/test/src/lockstep_scheduler_test.cpp index de53351fe8af..236581634aba 100644 --- a/platforms/posix/src/lockstep_scheduler/test/src/lockstep_scheduler_test.cpp +++ b/platforms/posix/src/lockstep_scheduler/test/src/lockstep_scheduler_test.cpp @@ -1,5 +1,5 @@ #include -#include +#include #include #include #include diff --git a/src/lib/hysteresis/HysteresisTest.cpp b/src/lib/hysteresis/HysteresisTest.cpp index 5426c21a916d..23f2e708f6da 100644 --- a/src/lib/hysteresis/HysteresisTest.cpp +++ b/src/lib/hysteresis/HysteresisTest.cpp @@ -36,7 +36,7 @@ * Tests for system timing hysteresis. */ -#include +#include #include "hysteresis.h" diff --git a/src/modules/mc_att_control/AttitudeControl/AttitudeControlTest.cpp b/src/modules/mc_att_control/AttitudeControl/AttitudeControlTest.cpp index ad07cb9d866f..64ffbe12975c 100644 --- a/src/modules/mc_att_control/AttitudeControl/AttitudeControlTest.cpp +++ b/src/modules/mc_att_control/AttitudeControl/AttitudeControlTest.cpp @@ -31,8 +31,9 @@ * ****************************************************************************/ -#include -#include +#include + +#include "AttitudeControl.hpp" using namespace matrix; diff --git a/src/modules/mc_pos_control/Takeoff/TakeoffTest.cpp b/src/modules/mc_pos_control/Takeoff/TakeoffTest.cpp index 9d8b77699d71..4098b74c5aba 100644 --- a/src/modules/mc_pos_control/Takeoff/TakeoffTest.cpp +++ b/src/modules/mc_pos_control/Takeoff/TakeoffTest.cpp @@ -31,8 +31,9 @@ * ****************************************************************************/ -#include -#include +#include + +#include "Takeoff.hpp" TEST(TakeoffTest, Initialization) { diff --git a/src/platforms/px4_unittesting.h b/src/platforms/px4_unittesting.h new file mode 100644 index 000000000000..693017a16a3e --- /dev/null +++ b/src/platforms/px4_unittesting.h @@ -0,0 +1,47 @@ +/**************************************************************************** + * + * 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. + * + ****************************************************************************/ + +/** + * @file px4_unittesting.h + * + * File to include the appropriate header files for unit testing on the + * respective platform. + */ + +#pragma once + +#ifdef __PX4_NUTTX +#include +#else +#include +#endif