Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integration test template #2726

Merged
Merged
Show file tree
Hide file tree
Changes from 85 commits
Commits
Show all changes
129 commits
Select commit Hold shift + click to select a range
6b64c6d
Added testcases:
PrasRsRos Mar 30, 2023
bbab11b
Update main.yml
PrasRsRos Mar 31, 2023
3d0b668
Added copyright message
PrasRsRos Mar 31, 2023
b8817d7
Merge branch 'integration_test_template' of https://github.com/PrasRs…
PrasRsRos Mar 31, 2023
4e5c222
One more fix...
PrasRsRos Mar 31, 2023
5e8968e
Update main.yml
PrasRsRos Mar 31, 2023
d016a75
py file copyrights
PrasRsRos Mar 31, 2023
9662e89
Merge branch 'integration_test_template' of https://github.com/PrasRs…
PrasRsRos Mar 31, 2023
52562ab
Update main.yml
PrasRsRos Mar 31, 2023
bc6b036
Update main.yml
PrasRsRos Mar 31, 2023
fd95068
Update main.yml
PrasRsRos Mar 31, 2023
0fe1456
Added fix for the pipe
PrasRsRos Mar 31, 2023
3cf4598
pytest_integration_fn changed
PrasRsRos Apr 3, 2023
8824b2c
Added comments to the tests
PrasRsRos Apr 3, 2023
e293e7d
fix comments in py test
PrasRsRos Apr 3, 2023
d40d758
Added a pytest util file for sharing common functions
Apr 4, 2023
62ed2f7
Moved launch description also to utils
PrasRsRos Apr 10, 2023
ddc330b
Merge branch 'IntelRealSense:ros2-development' into integration_test_…
PrasRsRos Apr 10, 2023
577b77f
moved to single instance of camera
PrasRsRos Apr 10, 2023
1f57524
moved common class files to utils
PrasRsRos Apr 11, 2023
d19d1cd
Added one more test and moved basic test class to utils
PrasRsRos Apr 11, 2023
fc7cd19
added check for the data content
PrasRsRos Apr 11, 2023
e0439ed
Added two streams, depth and color
PrasRsRos Apr 11, 2023
ed7dd72
Modified CMake to automate detaction of test files and added readme
PrasRsRos Apr 12, 2023
4ac938a
updated Readme
PrasRsRos Apr 12, 2023
ebdf0ec
updated Readme for formatting
PrasRsRos Apr 12, 2023
0f77e33
updated Readme for formatting 2
PrasRsRos Apr 12, 2023
d92d1d2
updated Readme for formatting 3
PrasRsRos Apr 12, 2023
669ed01
updated Readme for formatting 4
PrasRsRos Apr 12, 2023
354b631
fixed issue in template that checks the data from all topics
PrasRsRos Apr 12, 2023
68185f7
fixed the order of frame pop
PrasRsRos Apr 12, 2023
ff631b3
Merge branch 'IntelRealSense:ros2-development' into integration_test_…
PrasRsRos Apr 12, 2023
d53e951
added prerequisites for running the tests
PrasRsRos Apr 12, 2023
48f1fab
Merge branch 'integration_test_template' of https://github.com/PrasRs…
PrasRsRos Apr 12, 2023
aa06059
added prerequisites for running the tests + format
PrasRsRos Apr 12, 2023
87c1eb2
added prerequisites for running the tests + format
PrasRsRos Apr 12, 2023
7067e63
Aligned the message type with the old tests
PrasRsRos Apr 13, 2023
c6944de
Updated readme
PrasRsRos Apr 13, 2023
1820041
added parameterized tests
PrasRsRos Apr 13, 2023
dcacfe0
parameterized template for test added
PrasRsRos Apr 13, 2023
58714eb
added two paramters as two tests
PrasRsRos Apr 13, 2023
aa24cf3
split the steps in utils
PrasRsRos May 3, 2023
a051422
Merge branch 'IntelRealSense:ros2-development' into integration_test_…
PrasRsRos May 3, 2023
63cd6a7
moved test files to directories
PrasRsRos May 5, 2023
fea7d8a
working for both pytest and colcon test
PrasRsRos May 5, 2023
12fa03e
Clear comments
PrasRsRos May 24, 2023
a8b9baa
added paramter usage in functions
PrasRsRos May 29, 2023
4b45356
ported rosbag tests accel_up_1 and vis_avg_2 from rs2_test.py
PrasRsRos May 31, 2023
6d01b7b
cleared instability issues due to same node names and context initial…
PrasRsRos Jun 1, 2023
4ec9828
Integration test failed: fixed
PrasRsRos Jun 1, 2023
1c90912
updated rs2_test to align to the param name change
PrasRsRos Jun 2, 2023
6cb80f5
Added point cloud test
PrasRsRos Jun 7, 2023
7fe35d9
rs2_test updated with points_cloud test
PrasRsRos Jun 8, 2023
5fdd7f8
added delayed RS node creation and test for depth and points cloud to…
PrasRsRos Jun 9, 2023
470b4d8
Fixed and added static_tf1 test
PrasRsRos Jun 9, 2023
5f9b5e1
Ported non-existent rosbag file test
PrasRsRos Jun 9, 2023
169c74d
removed debug prints
PrasRsRos Jun 14, 2023
eff2fbf
Added and skipped depth aligned to Infra1. Was tested with modified r…
PrasRsRos Jun 15, 2023
aecd6d3
Merge branch 'IntelRealSense:ros2-development' into integration_test_…
PrasRsRos Jun 15, 2023
594b4c4
removed pointscloud tests for compatibility with other ROS2 versions
PrasRsRos Jun 15, 2023
101e1e2
Adding camera_info tests
PrasRsRos Jun 19, 2023
3bd0f7a
Adding camera_info tests
PrasRsRos Jun 19, 2023
56cf1a0
Extrinsics test
PrasRsRos Jun 26, 2023
2a139c6
Added metadata test
PrasRsRos Jun 26, 2023
b1b0cc6
imu test, but without data check
PrasRsRos Jun 27, 2023
1cffbdf
Merge branch 'IntelRealSense:ros2-development' into integration_test_…
PrasRsRos Jun 28, 2023
15b90f9
added tests, fixed issues in data checks
PrasRsRos Jun 28, 2023
2505972
Fixed CameraInfo comparison
PrasRsRos Jun 30, 2023
4fe106e
Merge branch 'IntelRealSense:ros2-development' into integration_test_…
PrasRsRos Jun 30, 2023
ab7aa01
removed camera info check for aligned
PrasRsRos Jun 30, 2023
7826e1f
Merge branch 'IntelRealSense:ros2-development' into integration_test_…
PrasRsRos Jul 3, 2023
5821056
For stability?
PrasRsRos Jul 3, 2023
39be88e
reduce the no of tests in one py file
PrasRsRos Jul 5, 2023
3c17972
inconsistency in the metadata check
PrasRsRos Jul 5, 2023
38df406
Update main.yml
PrasRsRos Jul 5, 2023
4f4d7ea
dividing parallel execution further
PrasRsRos Jul 5, 2023
a238460
all topics test is too fast
PrasRsRos Jul 5, 2023
1251511
all topics testing 1
PrasRsRos Jul 5, 2023
9f79f4d
all topics testing 2
PrasRsRos Jul 5, 2023
c7ee653
all topics testing 3
PrasRsRos Jul 5, 2023
1449ffc
all topics testing 4
PrasRsRos Jul 6, 2023
5442450
testing for more timeout
PrasRsRos Jul 6, 2023
9cdbc84
Update pytest_rs_utils.py
PrasRsRos Jul 6, 2023
bf173fa
all_topics test 5
PrasRsRos Jul 7, 2023
9285dfa
all_topics test 6
PrasRsRos Jul 7, 2023
e371850
Add launch_pytest dependency in package.xml
PrasRsRos Jul 10, 2023
4163535
sensor_msgs_py package added for testing
PrasRsRos Jul 10, 2023
feabe3c
Update package with quaternion
PrasRsRos Jul 10, 2023
8e661ed
Update numpy-quaternion
PrasRsRos Jul 10, 2023
c8b67a8
revert quarternion
PrasRsRos Jul 10, 2023
331c145
pre-release testing 1
PrasRsRos Jul 10, 2023
3ed567c
package testing 2
PrasRsRos Jul 10, 2023
3820d50
package testing 3
PrasRsRos Jul 10, 2023
62952e7
package testing 4
PrasRsRos Jul 10, 2023
ad52a21
package testing 6
PrasRsRos Jul 10, 2023
e9f90e0
package testing 9
PrasRsRos Jul 10, 2023
12bb55b
package testing 10
PrasRsRos Jul 10, 2023
6197029
package testing 11
PrasRsRos Jul 10, 2023
e61569b
Update .travis.yml for jammy
PrasRsRos Jul 10, 2023
b15acff
testing pre-release 12
PrasRsRos Jul 10, 2023
6da939a
pre-release testing 13
PrasRsRos Jul 10, 2023
8a6491c
pre-release testing 15
PrasRsRos Jul 10, 2023
1d9b674
pre-release testing 16
PrasRsRos Jul 10, 2023
f4486be
pre-release testing 17
PrasRsRos Jul 11, 2023
47cca7b
pre-release testing 18
PrasRsRos Jul 11, 2023
67c97cf
pre-release 19
PrasRsRos Jul 11, 2023
261ac0f
pre-release testing 20
PrasRsRos Jul 11, 2023
ae81fe6
pre-release testing 21
PrasRsRos Jul 11, 2023
61abdd3
pre-release testing 22
PrasRsRos Jul 11, 2023
44d3dfe
pre-release testing 23
PrasRsRos Jul 11, 2023
70a9774
prerelease testing 23
PrasRsRos Jul 11, 2023
1db6a31
pre-release testing 24
PrasRsRos Jul 11, 2023
7a3814f
pre-release testing 24
PrasRsRos Jul 11, 2023
22639a3
pre-release testing 26
PrasRsRos Jul 11, 2023
66aac2f
pre-release testing 28
PrasRsRos Jul 11, 2023
65e8dee
pre-release testing 29
PrasRsRos Jul 11, 2023
12762d6
prerelease testing 30
PrasRsRos Jul 11, 2023
a85f335
prerelease testing 31
PrasRsRos Jul 11, 2023
2243242
package testing 32
PrasRsRos Jul 14, 2023
ec1c664
prerelease testing 33
PrasRsRos Jul 19, 2023
b9fa729
package testing 34
PrasRsRos Jul 19, 2023
019c3ea
tqdm dependency added to package
PrasRsRos Jul 19, 2023
f3a2f21
Added paths for the dependent scripts
PrasRsRos Jul 19, 2023
6d87de3
moved path dependency to CMakeLists.txt
PrasRsRos Jul 19, 2023
1fb46d6
Merge branch 'IntelRealSense:ros2-development' into integration_test_…
PrasRsRos Jul 20, 2023
22d450f
rosbag moved to a temp location
PrasRsRos Jul 24, 2023
6bc6744
cleanup of prerelease
PrasRsRos Jul 24, 2023
1ec085e
removing the download step
PrasRsRos Jul 25, 2023
2971766
Merge branch 'IntelRealSense:ros2-development' into integration_test_…
PrasRsRos Jul 25, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 21 additions & 10 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -80,24 +80,35 @@ jobs:

## This step is commented out since we don't use rosbag files in "Run Tests" step below.
## Please uncomment when "Run Tests" step is fixed to run all tests.
#- name: Download Data For Tests
# run: |
# cd ${{github.workspace}}/ros2
# bag_filename="https://librealsense.intel.com/rs-tests/TestData/outdoors_1color.bag";
# wget $bag_filename -P "records/"
# bag_filename="https://librealsense.intel.com/rs-tests/D435i_Depth_and_IMU_Stands_still.bag";
# wget $bag_filename -P "records/"

- name: Download Data For Tests
if: ${{ matrix.ros_distro != 'rolling'}}
run: |
cd ${{github.workspace}}/ros2
bag_filename="https://librealsense.intel.com/rs-tests/TestData/outdoors_1color.bag";
wget $bag_filename -P "records/"
bag_filename="https://librealsense.intel.com/rs-tests/D435i_Depth_and_IMU_Stands_still.bag";
wget $bag_filename -P "records/"
sudo apt install ros-${{ matrix.ros_distro}}-launch-pytest

- name: Install Packages For Tests
run: |
sudo apt-get install python3-pip
pip3 install numpy --upgrade
pip3 install numpy-quaternion tqdm

- name: Run Tests
run: |
cd ${{github.workspace}}/ros2
source ${{github.workspace}}/.bashrc
. install/local_setup.bash
python3 src/realsense-ros/realsense2_camera/scripts/rs2_test.py non_existent_file


- name: Run integration tests
if: ${{ matrix.ros_distro != 'rolling'}}
run: |
cd ${{github.workspace}}/ros2
source ${{github.workspace}}/.bashrc
. install/local_setup.bash
export ROSBAG_FILE_PATH=${{github.workspace}}/ros2/records/
colcon test --packages-select realsense2_camera --event-handlers console_direct+
colcon test-result --all --test-result-base build/realsense2_camera/test_results/ --verbose
40 changes: 39 additions & 1 deletion realsense2_camera/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,45 @@ install(DIRECTORY

# Test
if(BUILD_TESTING)
# This does nothing for now, note that ROS build farm build with BUILD_TESTING=1
find_package(ament_cmake_gtest REQUIRED)
set(_gtest_folders
test
)
foreach(test_folder ${_gtest_folders})
file(GLOB files "${test_folder}/gtest_*.cpp")
foreach(file ${files})
get_filename_component(_test_name ${file} NAME_WE)
ament_add_gtest(${_test_name} ${file})
target_include_directories(${_test_name} PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>
)
ament_target_dependencies(${_test_name}
std_msgs
)
#target_link_libraries(${_test_name} name_of_local_library)
endforeach()
endforeach()


find_package(ament_cmake_pytest REQUIRED)
set(_pytest_folders
test
test/templates
test/rosbag
)
foreach(test_folder ${_pytest_folders})
file(GLOB files "${test_folder}/test_*.py")
foreach(file ${files})

get_filename_component(_test_name ${file} NAME_WE)
ament_add_pytest_test(${_test_name} ${file}
APPEND_ENV PYTHONPATH=${CMAKE_CURRENT_BINARY_DIR}:${CMAKE_SOURCE_DIR}/test/utils
TIMEOUT 60
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
)
endforeach()
endforeach()
endif()

# Ament exports
Expand Down
3 changes: 3 additions & 0 deletions realsense2_camera/package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
<depend>tf2</depend>
<depend>tf2_ros</depend>
<depend>diagnostic_updater</depend>
<test_depend>ament_cmake_gtest</test_depend>
<test_depend>launch_testing</test_depend>
<test_depend>ament_cmake_pytest</test_depend>
PrasRsRos marked this conversation as resolved.
Show resolved Hide resolved

<exec_depend>launch_ros</exec_depend>
<build_depend>ros_environment</build_depend>
Expand Down
13 changes: 8 additions & 5 deletions realsense2_camera/scripts/rs2_listener.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@
from rclpy.node import Node
from rclpy import qos
from sensor_msgs.msg import Image as msg_Image
# from sensor_msgs.msg import PointCloud2 as msg_PointCloud2
# import sensor_msgs.point_cloud2 as pc2
from sensor_msgs.msg import Imu as msg_Imu
import numpy as np
import inspect
import ctypes
Expand All @@ -29,7 +26,12 @@
import os
if (os.getenv('ROS_DISTRO') != "dashing"):
import tf2_ros

if (os.getenv('ROS_DISTRO') == "humble"):
from sensor_msgs.msg import PointCloud2 as msg_PointCloud2
from sensor_msgs_py import point_cloud2 as pc2
# from sensor_msgs.msg import PointCloud2 as msg_PointCloud2
# import sensor_msgs.point_cloud2 as pc2
from sensor_msgs.msg import Imu as msg_Imu

try:
from theora_image_transport.msg import Packet as msg_theora
Expand All @@ -38,6 +40,7 @@


def pc2_to_xyzrgb(point):
point = list(point)
# Thanks to Panos for his code used in this function.
x, y, z = point[:3]
rgb = point[3]
Expand Down Expand Up @@ -87,7 +90,7 @@ def __init__(self, params={}):

self.themes = {'depthStream': {'topic': '/camera/depth/image_rect_raw', 'callback': self.imageColorCallback, 'msg_type': msg_Image},
'colorStream': {'topic': '/camera/color/image_raw', 'callback': self.imageColorCallback, 'msg_type': msg_Image},
# 'pointscloud': {'topic': '/camera/depth/color/points', 'callback': self.pointscloudCallback, 'msg_type': msg_PointCloud2},
#'pointscloud': {'topic': '/camera/depth/color/points', 'callback': self.pointscloudCallback, 'msg_type': msg_PointCloud2},
'alignedDepthInfra1': {'topic': '/camera/aligned_depth_to_infra1/image_raw', 'callback': self.imageColorCallback, 'msg_type': msg_Image},
'alignedDepthColor': {'topic': '/camera/aligned_depth_to_color/image_raw', 'callback': self.imageColorCallback, 'msg_type': msg_Image},
'static_tf': {'topic': '/camera/color/image_raw', 'callback': self.imageColorCallback, 'msg_type': msg_Image},
Expand Down
16 changes: 9 additions & 7 deletions realsense2_camera/scripts/rs2_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,16 +369,18 @@ def main():
all_tests = [{'name': 'non_existent_file', 'type': 'no_file', 'params': {'rosbag_filename': '/home/non_existent_file.txt'}},
{'name': 'vis_avg_2', 'type': 'vis_avg', 'params': {'rosbag_filename': outdoors_filename}},
{'name': 'depth_avg_1', 'type': 'depth_avg', 'params': {'rosbag_filename': outdoors_filename}},
{'name': 'depth_w_cloud_1', 'type': 'depth_avg', 'params': {'rosbag_filename': outdoors_filename, 'enable_pointcloud': 'true'}},
# {'name': 'points_cloud_1', 'type': 'pointscloud_avg', 'params': {'rosbag_filename': outdoors_filename, 'enable_pointcloud': 'true'}},
{'name': 'align_depth_color_1', 'type': 'align_depth_color', 'params': {'rosbag_filename': outdoors_filename, 'align_depth': 'true'}},
{'name': 'align_depth_ir1_1', 'type': 'align_depth_ir1', 'params': {'rosbag_filename': outdoors_filename, 'align_depth': 'true'}},
{'name': 'depth_avg_decimation_1', 'type': 'depth_avg_decimation', 'params': {'rosbag_filename': outdoors_filename, 'filters': 'decimation'}},
{'name': 'align_depth_ir1_decimation_1', 'type': 'align_depth_ir1_decimation', 'params': {'rosbag_filename': outdoors_filename, 'filters': 'decimation', 'align_depth': 'true'}},
#{'name': 'points_cloud_1', 'type': 'pointscloud_avg', 'params': {'rosbag_filename': outdoors_filename, 'pointcloud.enable': 'true'}},
{'name': 'depth_w_cloud_1', 'type': 'depth_avg', 'params': {'rosbag_filename': outdoors_filename, 'pointcloud.enable': 'true'}},
{'name': 'align_depth_color_1', 'type': 'align_depth_color', 'params': {'rosbag_filename': outdoors_filename, 'align_depth.enable':'true'}},
{'name': 'align_depth_ir1_1', 'type': 'align_depth_ir1', 'params': {'rosbag_filename': outdoors_filename, 'align_depth.enable': 'true',
'enable_infra1':'true', 'enable_infra2':'true'}},
{'name': 'depth_avg_decimation_1', 'type': 'depth_avg_decimation', 'params': {'rosbag_filename': outdoors_filename, 'decimation_filter.enable':'true'}},
{'name': 'align_depth_ir1_decimation_1', 'type': 'align_depth_ir1_decimation', 'params': {'rosbag_filename': outdoors_filename, 'align_depth.enable':'true', 'decimation_filter.enable':'true'}},
]
if (os.getenv('ROS_DISTRO') != "dashing"):
all_tests.extend([
{'name': 'static_tf_1', 'type': 'static_tf', 'params': {'rosbag_filename': outdoors_filename}}, # Not working in Travis...
{'name': 'static_tf_1', 'type': 'static_tf', 'params': {'rosbag_filename': outdoors_filename,
'enable_infra1':'true', 'enable_infra2':'true'}},
{'name': 'accel_up_1', 'type': 'accel_up', 'params': {'rosbag_filename': './records/D435i_Depth_and_IMU_Stands_still.bag', 'enable_accel': 'true', 'accel_fps': '0.0'}},
])

Expand Down
152 changes: 152 additions & 0 deletions realsense2_camera/test/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
# Testing realsense2_camera
The test infra for realsense2_camera uses both gtest and pytest. gtest is typically used here for testing at the unit level and pytest for integration level testing. Please be aware that the README assumes that the ROS2 version used is Humble or later, as the launch_pytest package used here is not available in prior versions

## Test using gtest
The default folder for the test cpp files is realsense2_camera/test. A test template gtest_template.cpp is available in the same folder.
### Adding a new test
If the user wants to add a new test, a copy of gtest_template.cpp as the starting point. Please name the file as gtest_`testname`.cpp format so that the CMake detects the file and add as a new test. Please be aware that, multiple tests can be added into one file, the gtest_template.cpp has 2 tests, test1 and test2.

### Adding a new test folder
It is recommended to use the test folder itself for storing all the cpp tests. However, if the user wants to add a different folder for a set of tests, please ensure that the file name format mentioned above is followed. The folder path is added to realsense_camera/CMakeLists.txt as below for the build to detect the tests within.

```
find_package(ament_cmake_gtest REQUIRED)
set(_gtest_folders
test #<-- default folder for the gtest sources
new_folder_for_test_but_why #<-- new folder name is added
)
```

## Test using pytest
The default folder for the test py files is realsense2_camera/test. Two test template files test_launch_template.py and test_integration_template.py are available in the same folder for reference.
### Add a new test
To add a new test, the user can create a copy of the test_launch_template.py or test_integration_template.py and start from there. Please name the file in the format test_`testname`.py so that the CMake detects the file and add as a new test. Please be aware that, multiple tests can be added into one file, the test_integration_template.py itself has more than one test.i The marker `@pytest.mark.launch` is used to specify the test entry point.

The test_launch_template.py uses the rs_launch.py to start the camera node, so this template can be used for testing the rs_launch.py together with the rs node.

The test_integration_template.py gives a better control for testing, it uses few util functions and base test class from pytest_rs_utils.py located at the same location. However, it doesn't use the rs_launch.py, it creates the node directly instead.

The test_integration_template.py has two types of tests, one has a function "test_using_function". If the user wants to have a better control over the launch context for any specific test scenarios, this can be used. Both the function based test and class based tests use a default launch configuration from the utils. It's recommended to modify the camera name to a unique one in the parameters itself so that there are not clashes between tests.

It is expected that the class based test is used as the test format for most of the usecases. The class based test inherits from pytest_rs_utils.RsTestBaseClass and it has three steps, namely: init, run_test and process_data. Unless for the basic tests, the user will have to override the process_data function and check if the data received from the topics are as expected. Also, if the user doesn't want the base class to modify the data, use 'store_raw_data':True in the theme definition. Please see the test_integration_template.py for reference.

An assert command can be used to indicate if the test failed or passed. Please see the template for more info.

### Adding a new test folder
It is recommended to use the test folder itself for storing all the pytests. However, if the user wants to add a different folder for a set of tests, please ensure that the file name format mentioned above is followed. The folder path should be added to realsense_camera/CMakeLists.txt as below for the infra to detect the new test folder and the tests within.

```
find_package(ament_cmake_pytest REQUIRED)
set(_pytest_folders
test #default test folder
test/templates
test/rosbag
new_folder_for_pytest #<-- new folder #but please be aware that the utils functions are in test/utils folder,
#so if the template is used, change the include path also accordingly
)
```

### Grouping of tests
The pytests can be grouped using markers. These markers can be used to run a group of tests. However, "colcon test" command doesn't pass a custom marker using (--pytest-args -m `marker_name`) to the pytest internally. This is because, the ament_cmake that works as a bridge between colcon and pytest doesn't pass the pytest arguments to pytest. So till this is fixed, pytest command has to be used directly for running a group of tests. Please see the next session for the commands to run a group py tests.

The grouping is specified by adding a marker just before the test declaration. In the test_integration_template.py `rosbag` is specified as a marker specify tests that use rosbag file. This is achieved by adding "@pytest.mark.rosbag" to the begining of the test. So when the pytest parses for test, it detects the marker for the test. If this marker is selected or none of the markers are specified, the test will be added to the list, else will be listed as a deselected test.

It is recommended to use markers such as ds457, rosbag, ds415 etc to differentiate the tests so that it's easier to run a group of tests in a machine that has the required hardware.

## Building and running tests

### Build steps

The command used for building the tests along with the node:

colcon build

The test statements in CMakeLists.txt are protected by BUILD_TESTING macro. So in case, the tests are not being built, then it could be that the macro are disabled by default.

Note: The below command helps view the steps taken by the build command.

colcon build --event-handlers console_direct+

### Prerequisites for running the tests

1. The template tests require the rosbag files from librealsense.intel.comi, the following commands download them:
```
bag_filename="https://librealsense.intel.com/rs-tests/TestData/outdoors_1color.bag";
wget $bag_filename -P "records/"
bag_filename="https://librealsense.intel.com/rs-tests/D435i_Depth_and_IMU_Stands_still.bag";
wget $bag_filename -P "records/"
```
2. The tests use the environment variable ROSBAG_FILE_PATH as the directory that contains the rosbag files
```
export ROSBAG_FILE_PATH=/path/to/directory/of/rosbag
```
3. Install launch_pytest package. For humble:
```
sudo apt install ros-$ROS_DISTRO-launch-pytest
```
4. As in the case of all the packages, the install script of realsesnse2_camera has to be run.
```
. install/local_setup.bash
```
5. If the tests are run on a machine that has the RS board connected or the tests are using rosbag files, then its better to let the ROS search for the nodes in the local machine, this will be faster and less prone to interference and hence unexpected errors. It can be achieved using the following environment variable.
```
export ROS_DOMAIN_ID=1
```

So, all put together:

```
sudo apt install ros-$ROS_DISTRO-launch-pytest
bag_filename="https://librealsense.intel.com/rs-tests/TestData/outdoors_1color.bag";
wget $bag_filename -P "records/"
bag_filename="https://librealsense.intel.com/rs-tests/D435i_Depth_and_IMU_Stands_still.bag";
wget $bag_filename -P "records/"
export ROSBAG_FILE_PATH=$PWD/records
. install/local_setup.bash
export ROS_DOMAIN_ID=1
```

### Running the tests using colcon

All the tests can be run using the below command:

colcon test --packages-select realsense2_camera

This command will invoke both gtest and pytest infra and run all the tests specified in the files mentioned above. Since the test results are stored in build/realsense2_camera/test_results folder, it's good to clean this up after running the tests with a new test added/removed.

The same command with console_direct can be used for more info on failing tests, as below:

colcon test --packages-select realsense2_camera --event-handlers console_direct+

The test results can be viewed using the command:

colcon test-result --all --test-result-base build/realsense2_camera/test_results/

The xml files mentioned by the command can be directly opened also.

### Running pytests directly


User can run all the tests in a pytest file directly using the below command:

pytest-3 -s realsense2_camera/test/test_integration_template.py

All the pytests in a test folder can be directly run using the below command:

pytest-3 -s realsense2_camera/test/

### Running a group of pytests
As mentioned above, a set of pytests that are grouped using markers can be run using the pytest command. The below command runs all the pytests in realsense2_camera/test folder that has the marker rosbag:

pytest-3 -s -m rosbag realsense2_camera/test/


### Running a single pytest
The below command finds the test with the name test_static_tf_1 in realsense2_camera/test folder run:

pytest-3 -s -k test_static_tf_1 realsense2_camera/test/

### Points to be noted while writing pytests
The tests that are in one file are nromally run in parallel. So if there are multiple tests in one file, the system capacity can influence the test execution. It's recomended to have 3-4 tests in file, more than that can affect the test results due to delays.


33 changes: 33 additions & 0 deletions realsense2_camera/test/gtest_template.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright 2023 Intel Corporation. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <gtest/gtest.h>

TEST(realsense2_camera, test1)
{
std::cout << "Running test1...";
ASSERT_EQ(4, 2 + 2);
}
TEST(realsense2_camera, test2)
{
std::cout << "Running test2...";
ASSERT_EQ(4, 2 + 2);
}

int main(int argc, char** argv)
{
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

Loading