Skip to main content

Writing tests in pytest

Pytest is the canonical way to write Python tests for Roboticks. The roboticks package adds four decorators and a set of rclpy-aware assertion helpers on top of stock pytest. Nothing else changes — your fixtures, your conftest.py, your parametrize idioms keep working.

The four decorators

All four stack and can decorate the same test in any order. See the SDK decorator reference for full semantics.
Decorators are inert without the platform. Locally they only mark functions. The pytest plugin reads them at collection time and writes them into JUnit XML on session-end. See Pytest plugin.

Rclpy assertion helpers

The SDK ships ROS2-aware assertions in roboticks.assertions. They spin a node briefly, wait for the predicate, and raise an informative AssertionError on timeout. The helpers are guarded: importing the module on a host without rclpy installed raises a clear RuntimeError instead of an opaque ImportError.
Full signatures live in the SDK assertion reference.

Subscribe-and-assert on /cmd_vel

Service call response

Action result

Conftest pattern

Spin up rclpy once per test session, and once per test give every test a clean executor. This pattern works for unit-scope ROS tests; for system tests use launch_testing instead.
For tests that need a node-under-test running, layer it on:

Parametrize and @confirms

@confirms is per-test-function, not per-test-id. Parametrized variants all confirm the same requirement set — which is usually what you want:
If a parametrized branch should confirm a different requirement, split it into two test functions. Decorators are deliberately not parametrize-aware — that ambiguity is what got teams in trouble with older tools.

What the plugin emits

Per test, in the JUnit XML:
See Wire contract for the full schema and version handshake.

Next

Fault injection

Drop topics, delay messages, kill nodes — under a context manager.

MCAP capture

Record bag files per test for failure forensics.

Launch testing

System tests that bring up multiple nodes.

Decorator reference

Full signatures and edge cases.