Skip to content

Commit 2eecd1a

Browse files
authored
add catch2 to roverflake (#81)
1 parent d1a18a5 commit 2eecd1a

File tree

13 files changed

+256
-5
lines changed

13 files changed

+256
-5
lines changed

.github/workflows/unit_tests.yaml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
name: Run unit tests
2+
on: push
3+
jobs:
4+
unit_tests:
5+
runs-on: ubuntu-22.04
6+
steps:
7+
- uses: actions/checkout@v4
8+
with: # added this, might fix a bug that's taken over 3 hours to find
9+
submodules: recursive
10+
- name: build
11+
run: |
12+
make test
13+
- name: Notify Discord (success)
14+
if: ${{ success() }}
15+
uses: appleboy/discord-action@v1.0.0
16+
with:
17+
webhook_id: ${{ secrets.DISCORD_BOT_WEBHOOK_ID }} # it also accepts full URL
18+
webhook_token: ${{ secrets.DISCORD_BOT_WEBHOOK_TOKEN }} # it also accepts full URL
19+
message: |
20+
*Unit tests has passed for ${{ github.actor }}.* || ${{ github.repository }} on `${{ github.ref_name }}`
21+
${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
22+
- name: Notify Discord (failure)
23+
if: ${{ failure() }}
24+
uses: appleboy/discord-action@v1.0.0
25+
with:
26+
webhook_id: ${{ secrets.DISCORD_BOT_WEBHOOK_ID }} # it also accepts full URL
27+
webhook_token: ${{ secrets.DISCORD_BOT_WEBHOOK_TOKEN }} # it also accepts full URL
28+
message: |
29+
_Unit tests have failed ${{ github.actor }}_
30+
${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
31+

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,6 @@
44
[submodule "src/external_pkgs/moteus_ros2"]
55
path = src/external_pkgs/moteus_ros2
66
url = https://github.com/mjbots/moteus_ros2
7+
[submodule "src/external_pkgs/Catch2"]
8+
path = src/external_pkgs/Catch2
9+
url = https://github.com/catchorg/Catch2.git

CMakeLists.txt

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# CMake file for roverflake unit tests
2+
cmake_minimum_required(VERSION 3.20)
3+
4+
set(CMAKE_CXX_STANDARD 17) # cpp 2017 standard
5+
set(CMAKE_CXX_STANDARD_REQUIRED True)
6+
7+
add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/src/external_pkgs/Catch2 ${CMAKE_CURRENT_BINARY_DIR}/external_pkgs/Catch2) # Include Catch2 for unit tests
8+
9+
set(BUILD_DIR ${CMAKE_CURRENT_LIST_DIR}/build)
10+
set(SRC_DIR ${CMAKE_CURRENT_LIST_DIR}/src)
11+
set(SAMPLE_PKG_DIR ${SRC_DIR}/rover_samples/rv_sample_package/)
12+
set(ROVER_UTILS_DIR ${SRC_DIR}/rover_utils/)
13+
14+
# Code under test sources + directories
15+
include_directories(
16+
${SAMPLE_PKG_DIR}/include
17+
${ROVER_UTILS_DIR}/include
18+
19+
# add more directories with needed .h headers
20+
)
21+
22+
set(ROVERFLAKE_SOURCES
23+
${SAMPLE_PKG_DIR}/src/unit_test_example_class.cpp # just an example
24+
# Add roverflake files here (code under test)
25+
26+
# Autogenerated files:
27+
# ${BUILD_DIR}/autogen/*.cpp
28+
# ${BUILD_DIR}/autogen/*.c
29+
)
30+
31+
set(TEST_SOURCES
32+
${SRC_DIR}/rover_samples/rv_sample_package/test/unit_test_example.cpp
33+
# add more unit test files
34+
)
35+
36+
add_executable(unit_tests
37+
${TEST_SOURCES}
38+
${ROVERFLAKE_SOURCES}
39+
)
40+
41+
target_link_libraries(unit_tests PRIVATE
42+
Catch2::Catch2WithMain)
43+
44+
target_compile_options(unit_tests PRIVATE
45+
-Wall
46+
-Wextra
47+
# -Werror # Uncomment to be verry strict
48+
)
49+
50+
include(CTest)
51+
include(Catch)
52+
catch_discover_tests(unit_tests)

Makefile

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Makefile for common libraries
2+
3+
BUILD_DIR:=build
4+
5+
all: clean configure build test
6+
7+
configure:
8+
@mkdir -p $(BUILD_DIR)
9+
@cd $(BUILD_DIR) && cmake ..
10+
11+
build: configure
12+
@$(MAKE) -C $(BUILD_DIR)
13+
14+
clean:
15+
@rm -rf $(BUILD_DIR)
16+
17+
test: configure build
18+
@${BUILD_DIR}/unit_tests
19+
20+
21+
.PHONY: all configure build clean test

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@ From the root of RoverFlake2:
3939

4040
If you get an error... panic, scream, and hurl insults at your computer. Then look at common issues and ask for help in the discord.
4141

42+
### Unit Tests
43+
We use Catch2 for CPP unit tests. To keep it simple, we don't integrate with colcon tests, we have a seperate CMakeLists.txt and Makefile at the root, to handle all unit tests.
44+
To build and run tests: (the -j16 is just to compile faster by specifying how many cores to use)
45+
> `make -j16 test`
46+
To add tests, open up the CMakeLists.txt and manually add paths to test files. Follow the comments within the file.
47+
Catch2 is powerful, lightweight, and easy to use. It has a lot of built in macros to help with testing but even just the basics is enough to get by.
48+
4249
### COMMON ISSUES & TROUBLESHOOTING
4350
arm_hardware_interface fails to build:
4451

setup_scripts/setup_everything_common.sh

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,6 @@ apt_packages_to_install=(
4848
"libsfml-dev"
4949
)
5050

51-
52-
5351
# Loop through the package list and install missing packages
5452
for package in "${apt_packages_to_install[@]}"; do
5553
if is_package_installed "$package"; then

src/external_pkgs/Catch2

Submodule Catch2 added at a1faad9

src/rover_samples/rv_sample_package/CMakeLists.txt

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
cmake_minimum_required(VERSION 3.8)
1+
cmake_minimum_required(VERSION 3.10)
22
project(rv_sample_package)
33

44
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
@@ -11,6 +11,9 @@ find_package(rclcpp REQUIRED)
1111
find_package(joy REQUIRED)
1212
find_package(std_msgs REQUIRED)
1313
find_package(rover_utils REQUIRED)
14+
15+
16+
1417
# uncomment the following section in order to fill in
1518
# further dependencies manually.
1619
# find_package(<dependency> REQUIRED)
@@ -20,6 +23,26 @@ include_directories(include/)
2023
add_executable(sample_node src/sample_node.cpp include/sample_node.h include/sample_node_defines.h)
2124
ament_target_dependencies(sample_node rclcpp joy rover_utils)
2225

26+
# add_executable(test_example test/unit_test_example.cpp) # add a test, its just another executable
27+
# target_link_libraries(test_example PRIVATE Catch2::Catch2WithMain)
28+
# ament_add_catch2(test_example test/unit_test_example.cpp)
29+
30+
# if(BUILD_TESTING)
31+
# include(CTest)
32+
33+
# ###############
34+
# ## UNIT TEST ##
35+
# ###############
36+
# add_executable(example_unit_test
37+
# test/unit_test_example.cpp
38+
# )
39+
# # Linking a unit test executable with catch_ros2::catch_ros2_with_main
40+
# # provides the default Catch2 main function to the executable.
41+
# target_link_libraries(example_unit_test
42+
# catch_ros2::catch_ros2_with_main
43+
# )
44+
# add_test(NAME ExampleUnitTest COMMAND example_unit_test)
45+
# endif()
2346

2447

2548
install(
@@ -34,4 +57,6 @@ install(
3457
DESTINATION lib/${PROJECT_NAME}
3558
)
3659

60+
# find_package(ament_lint_auto REQUIRED)
61+
# ament_lint_auto_find_test_dependencies()
3762
ament_package()

src/rover_samples/rv_sample_package/include/sample_node.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@ class SampleNode : public rclcpp::Node {
2929
SampleNode(); // Constructor is defined in .cpp.
3030

3131
private:
32-
// Most member structures and functions are private in a custom ros2 node.
32+
// Most member structures and functions are private in a custom ros2 node. - However, for ease of testing,
33+
// you can make some functions public... or just everything public - goes against best practices but were a student team, not a company past series C
3334

3435
// Data types and structs go first
3536
int current_gear = 0;
3637
int prev_paddleR = 0;
3738
int prev_paddleL = 0;
3839

39-
4040
// SUBS AND PUBS
4141
rclcpp::Publisher<geometry_msgs::msg::TwistStamped>::SharedPtr cmd_vel_pub;
4242

0 commit comments

Comments
 (0)