Skip to content

Commit 12f4707

Browse files
authored
Adding Test Infra (#9)
* adds test package * documentation on the node * pre-commit * ci failing
1 parent 8303322 commit 12f4707

File tree

17 files changed

+1234
-1
lines changed

17 files changed

+1234
-1
lines changed

.github/actions/build-and-test/action.yml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
name: Build and Test
22
description: Common build and test logic for Deep ROS
33

4+
inputs:
5+
ros-distro:
6+
description: 'ROS distribution to use'
7+
required: true
8+
default: 'humble'
9+
410
runs:
511
using: "composite"
612
steps:
@@ -14,11 +20,14 @@ runs:
1420
1521
- name: 🧱 Build workspace
1622
shell: bash
17-
run: colcon build --merge-install
23+
run: |
24+
source /opt/ros/${{ inputs.ros-distro }}/setup.bash
25+
colcon build --merge-install
1826
1927
- name: ✅ Run tests
2028
shell: bash
2129
run: |
30+
source /opt/ros/${{ inputs.ros-distro }}/setup.bash
2231
colcon test --merge-install --event-handlers console_cohesion+
2332
colcon test-result --verbose
2433

.github/workflows/on_push.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,5 @@ jobs:
1919
with:
2020
required-ros-distributions: ${{ env.ROS_DISTRO }}
2121
- uses: ./.github/actions/build-and-test
22+
with:
23+
ros-distro: ${{ env.ROS_DISTRO }}

deep_test/CMakeLists.txt

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# Copyright (c) 2025-present WATonomous. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
# Copyright (c) 2025-present Polymath Robotics, Inc. All rights reserved
15+
# Proprietary. Any unauthorized copying, distribution, or modification of this software is strictly prohibited.
16+
cmake_minimum_required(VERSION 3.22)
17+
project(deep_test)
18+
19+
if(NOT CMAKE_CXX_STANDARD)
20+
set(CMAKE_CXX_STANDARD 17)
21+
endif()
22+
23+
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
24+
add_compile_options(-Wall -Wextra -Wpedantic)
25+
add_link_options(-Wl,-no-undefined)
26+
endif()
27+
28+
find_package(ament_cmake REQUIRED)
29+
find_package(rclcpp REQUIRED)
30+
find_package(rclcpp_lifecycle REQUIRED)
31+
32+
set(include_dir ${CMAKE_CURRENT_SOURCE_DIR}/include)
33+
34+
# deep_test library
35+
set(DEEP_TEST_LIB ${PROJECT_NAME}_lib)
36+
add_library(${DEEP_TEST_LIB} SHARED
37+
src/test_executor_fixture.cpp
38+
)
39+
target_include_directories(${DEEP_TEST_LIB} PUBLIC
40+
$<BUILD_INTERFACE:${include_dir}>
41+
$<INSTALL_INTERFACE:include>
42+
)
43+
target_link_libraries(${DEEP_TEST_LIB}
44+
PRIVATE
45+
rclcpp::rclcpp
46+
rclcpp_lifecycle::rclcpp_lifecycle
47+
)
48+
49+
install(TARGETS
50+
${DEEP_TEST_LIB}
51+
EXPORT ${PROJECT_NAME}Targets
52+
ARCHIVE DESTINATION lib
53+
LIBRARY DESTINATION lib
54+
RUNTIME DESTINATION bin
55+
)
56+
57+
install(EXPORT ${PROJECT_NAME}Targets
58+
NAMESPACE ${PROJECT_NAME}::
59+
DESTINATION share/${PROJECT_NAME}/cmake
60+
)
61+
62+
install(DIRECTORY include/
63+
DESTINATION include
64+
)
65+
66+
if(BUILD_TESTING)
67+
# Include our custom test function
68+
include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/add_deep_test.cmake)
69+
70+
# Find test dependencies
71+
find_package(std_msgs REQUIRED)
72+
find_package(std_srvs REQUIRED)
73+
74+
# Add the basic example test
75+
add_deep_test(test_basic_example
76+
test/test_basic_example.cpp
77+
LIBRARIES
78+
${DEEP_TEST_LIB}
79+
${std_msgs_TARGETS}
80+
${std_srvs_TARGETS}
81+
)
82+
83+
# Add ROS test with multiple library dependencies
84+
add_deep_test(test_with_ros
85+
test/test_with_ros.cpp
86+
LIBRARIES
87+
${DEEP_TEST_LIB}
88+
${std_msgs_TARGETS}
89+
)
90+
endif()
91+
92+
ament_export_targets(${PROJECT_NAME}Targets HAS_LIBRARY_TARGET)
93+
ament_package(
94+
CONFIG_EXTRAS "${PROJECT_NAME}-extras.cmake"
95+
)

deep_test/DEVELOPING.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Developing deep_test
2+
3+
## Building
4+
5+
```bash
6+
colcon build --packages-select deep_test
7+
```
8+
9+
## Running Tests
10+
11+
```bash
12+
colcon test --packages-select deep_test
13+
colcon test-result --verbose
14+
```
15+
16+
## Adding New Features
17+
18+
### Test Fixtures
19+
This includes code that follows the definition of a fixture as per catch2.
20+
21+
1. Add header to `include/test_fixtures/`
22+
2. Add implementation to `src/` if needed
23+
3. Include in `include/deep_test/deep_test.hpp`
24+
25+
### Test Nodes
26+
This includes code that acts as helper ros nodes for testing. They should be inherently event-driven nodes (do not introduce arbitrary sleeps).
27+
28+
1. Add header to `include/test_nodes/`
29+
2. Include in main header
30+
3. Keep nodes header-only when possible
31+
32+
## CMake Function
33+
34+
The `add_deep_test()` function automatically:
35+
- Links Catch2::Catch2WithMain
36+
- Links ROS dependencies (rclcpp, rclcpp_lifecycle)
37+
- Configures ament test registration
38+
- Sets up JUnit XML output

deep_test/README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# deep_test
2+
3+
A ROS 2 testing framework built on Catch2 that provides test fixtures and utilities for testing ROS nodes, services, and communication patterns.
4+
5+
## Features
6+
7+
- **TestExecutorFixture**: Manages ROS executor lifecycle for tests
8+
- **Test Nodes**: Pre-built nodes for testing publishers, subscribers, services, and clients
9+
- **CMake Integration**: Simple `add_deep_test()` function for creating tests
10+
- **Catch2 Integration**: Built on Catch2 for modern C++ testing
11+
12+
## Quick Start
13+
14+
```cpp
15+
#include <catch2/catch.hpp>
16+
#include <deep_test/deep_test.hpp>
17+
18+
TEST_CASE_METHOD(deep_ros::test::TestExecutorFixture, "My ROS Test", "[ros]") {
19+
auto node = std::make_shared<rclcpp::Node>("test_node");
20+
add_node(node);
21+
start_spinning();
22+
23+
// Your test code here as sections
24+
SECTION("My test") {
25+
REQUIRE(node->get_name() == "test_node");
26+
}
27+
}
28+
```
29+
30+
## CMake Usage
31+
32+
```cmake
33+
find_package(deep_test REQUIRED)
34+
35+
add_deep_test(my_test
36+
test/my_test.cpp
37+
LIBRARIES
38+
my_package::my_library
39+
std_msgs::std_msgs
40+
)
41+
```
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Copyright (c) 2025-present WATonomous. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
#
16+
# Catch2 Unittest Function
17+
# Creates a test executable with Catch2 support
18+
#
19+
# Usage:
20+
# add_deep_test(test_name test_source.cpp
21+
# LIBRARIES lib1 lib2 lib3
22+
# )
23+
#
24+
function(add_deep_test TEST_NAME TEST_SOURCE)
25+
# Parse arguments
26+
cmake_parse_arguments(
27+
PARSE_ARGV 2 # Start parsing from the 3rd argument
28+
ARG # Prefix for parsed variables
29+
"" # Options (flags with no values)
30+
"" # One-value keywords
31+
"LIBRARIES" # Multi-value keywords
32+
)
33+
34+
# Find dependencies
35+
find_package(Catch2 2 REQUIRED)
36+
find_package(rclcpp REQUIRED)
37+
find_package(rclcpp_lifecycle REQUIRED)
38+
39+
# Create the test executable
40+
add_executable(${TEST_NAME} ${TEST_SOURCE})
41+
42+
# Link with Catch2, ROS dependencies, and specified libraries
43+
target_link_libraries(${TEST_NAME}
44+
Catch2::Catch2WithMain
45+
rclcpp::rclcpp
46+
rclcpp_lifecycle::rclcpp_lifecycle
47+
${ARG_LIBRARIES}
48+
)
49+
50+
# Add test include directory
51+
target_include_directories(${TEST_NAME} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/test)
52+
53+
# Register the test with ament
54+
ament_add_test(
55+
${TEST_NAME}
56+
GENERATE_RESULT_FOR_RETURN_CODE_ZERO
57+
COMMAND "$<TARGET_FILE:${TEST_NAME}>"
58+
-r junit
59+
-o test_results/${PROJECT_NAME}/${TEST_NAME}_output.xml
60+
ENV CATCH_CONFIG_CONSOLE_WIDTH=120
61+
WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}"
62+
)
63+
endfunction()

deep_test/deep_test-extras.cmake

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Copyright (c) 2025-present WATonomous. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# Make the add_deep_test function available to packages that depend on deep_test
16+
include("${deep_test_DIR}/add_deep_test.cmake")
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright (c) 2025-present WATonomous. All rights reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#pragma once
16+
17+
// Main header for deep_test library
18+
// Include this to access all deep_test functionality
19+
20+
// Test fixtures
21+
#include "test_fixtures/test_executor_fixture.hpp"
22+
23+
// Test nodes
24+
#include "test_nodes/client_test_node.hpp"
25+
#include "test_nodes/publisher_test_node.hpp"
26+
#include "test_nodes/service_test_node.hpp"
27+
#include "test_nodes/subscriber_test_node.hpp"

0 commit comments

Comments
 (0)