Skip to content

Commit

Permalink
added the first gtest
Browse files Browse the repository at this point in the history
  • Loading branch information
klaxalk committed Feb 5, 2024
1 parent 9595a6d commit 297a9af
Show file tree
Hide file tree
Showing 8 changed files with 200 additions and 0 deletions.
11 changes: 11 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ set(CATKIN_DEPENDENCIES
roscpp
sensor_msgs
std_msgs
mrs_uav_testing
)

set(LIBRARIES
Expand Down Expand Up @@ -446,6 +447,16 @@ target_link_libraries(uvdar_led_manager_node
${catkin_LIBRARIES}
)

## --------------------------------------------------------------
## | Testing |
## --------------------------------------------------------------

if(CATKIN_ENABLE_TESTING)

add_subdirectory(test)

endif()

## --------------------------------------------------------------
## | Install |
## --------------------------------------------------------------
Expand Down
1 change: 1 addition & 0 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<depend>sensor_msgs</depend>
<depend>std_msgs</depend>
<depend>libgbm-dev</depend>
<depend>mrs_uav_testing</depend>

<export>
<nodelet plugin="${prefix}/nodelets.xml" />
Expand Down
3 changes: 3 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
find_package(rostest REQUIRED)

add_subdirectory(test_example)
34 changes: 34 additions & 0 deletions test/all_tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/bin/bash

set -e

trap 'last_command=$current_command; current_command=$BASH_COMMAND' DEBUG
trap 'echo "$0: \"${last_command}\" command failed with exit code $?"' ERR

ORIGINAL_PATH=`pwd`

while [ ! -e ".catkin_tools" ]; do
cd ..
if [[ `pwd` == "/" ]]; then
# we reached the root and didn't find the build/COLCON_IGNORE file - that's a fail!
echo "$0: could not find the root of the current workspace".
return 1
fi
done

cd build

OLD_FILES=$(find . -name "*.gcda")

for FILE in $OLD_FILES; do
echo "$0: removing old coverage file '$FILE'"
rm $FILE
done

cd $ORIGINAL_PATH

# build the package
catkin build --this # it has to be fully built normally before building with --catkin-make-args tests
catkin build --this --no-deps --catkin-make-args tests

catkin test --this -i -p 1 -s
10 changes: 10 additions & 0 deletions test/compile_tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash

set -e

trap 'last_command=$current_command; current_command=$BASH_COMMAND' DEBUG
trap 'echo "$0: \"${last_command}\" command failed with exit code $?"' ERR

# build the package
catkin build --this # it has to be fully built normally before building with --catkin-make-args tests
catkin build --this --no-deps --catkin-make-args tests
16 changes: 16 additions & 0 deletions test/test_example/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
get_filename_component(TEST_NAME "${CMAKE_CURRENT_SOURCE_DIR}" NAME)

catkin_add_executable_with_gtest(test_${TEST_NAME}
test.cpp
)

target_link_libraries(test_${TEST_NAME}
${catkin_LIBRARIES}
)

add_dependencies(test_${TEST_NAME}
${${PROJECT_NAME}_EXPORTED_TARGETS}
${catkin_EXPORTED_TARGETS}
)

add_rostest(${TEST_NAME}.test)
90 changes: 90 additions & 0 deletions test/test_example/test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#include <gtest/gtest.h>

#include <mrs_uav_testing/test_generic.h>

class Tester : public mrs_uav_testing::TestGeneric {

public:
Tester();

bool test();
};

Tester::Tester() : mrs_uav_testing::TestGeneric() {

// create your own subscribers, publishers, etc here.
//
// check the mrs_uav_testing::TestGeneric for existing stuff
// this->nh_ is available
}

bool Tester::test() {

{
auto [success, message] = spawnGazeboUav();

if (!success) {
ROS_ERROR("[%s]: gazebo UAV spawning failed with message: '%s'", ros::this_node::getName().c_str(), message.c_str());
return false;
}
}

// wait for the MRS system
while (true) {

ROS_INFO_THROTTLE(1.0, "[%s]: waiting for the MRS system", ros::this_node::getName().c_str());

if (!ros::ok()) {
ROS_ERROR("[%s]: killed from outside", ros::this_node::getName().c_str());
return false;
}

if (this->mrsSystemReady()) {
break;
}
}

{
auto [success, message] = this->takeoff();

if (!success) {
ROS_ERROR("[%s]: takeoff failed with message: '%s'", ros::this_node::getName().c_str(), message.c_str());
return false;
}
}

this->sleep(5.0);

if (this->isFlyingNormally()) {
return true;
} else {
ROS_ERROR("[%s]: not flying normally", ros::this_node::getName().c_str());
return false;
}
}

// --------------------------------------------------------------
// | DO NOT MODIFY BELOW |
// --------------------------------------------------------------

TEST(TESTSuite, test) {

Tester tester;

bool result = tester.test();

if (result) {
GTEST_SUCCEED();
} else {
GTEST_FAIL();
}
}

int main([[maybe_unused]] int argc, [[maybe_unused]] char** argv) {

ros::init(argc, argv, "test");

testing::InitGoogleTest(&argc, argv);

return RUN_ALL_TESTS();
}
35 changes: 35 additions & 0 deletions test/test_example/test_example.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<launch>

<arg name="this_path" default="$(dirname)" />

<arg name="UAV_NAME" default="uav1" />
<arg name="UAV_TYPE" default="x500" />

<!-- automatically deduce the test name -->
<arg name="test_name" default="$(eval arg('this_path').split('/')[-1])" />

<!-- automatically deduce the package name -->
<arg name="import_eval" default="eval('_' + '_import_' + '_')"/>
<arg name="package_eval" default="eval(arg('import_eval') + '(\'rospkg\')').get_package_name(arg('this_path'))" />
<arg name="package" default="$(eval eval(arg('package_eval')))" />

<include file="$(find mrs_uav_testing)/launch/gazebo_simulator.launch" />

<include file="$(find mrs_uav_px4_api)/launch/api.launch">
<arg name="UAV_NAME" default="$(arg UAV_NAME)" />
<arg name="RUN_TYPE" default="simulation" />
</include>

<include file="$(find mrs_uav_testing)/launch/mrs_uav_system.launch">
<arg name="automatic_start" default="true" />
<arg name="platform_config" default="$(find mrs_uav_gazebo_simulation)/config/mrs_uav_system/$(arg UAV_TYPE).yaml" />
<arg name="UAV_NAME" default="$(arg UAV_NAME)" />
</include>

<test pkg="$(arg package)" type="test_$(arg test_name)" test-name="$(arg test_name)" time-limit="120.0">
<param name="test_name" value="$(arg test_name)" />
<param name="uav_name" value="$(arg UAV_NAME)" />
<param name="gazebo_spawner_params" value="1 --$(arg UAV_TYPE) --enable-rangefinder" />
</test>

</launch>

0 comments on commit 297a9af

Please sign in to comment.