ros2 / launch

Tools for launching multiple processes and for writing tests involving multiple processes.
Apache License 2.0
124 stars 139 forks source link

`launch_pytest` Test does not terminate if an error occurs in launch fixture #732

Open PatrickSowinski opened 11 months ago

PatrickSowinski commented 11 months ago

Bug report

Required Info:

Steps to reproduce issue

Have any launch-related runtime error in your launch_pytest.fixture. If any test uses this fixture, it will get stuck because the fixture fails and the test never starts. If you run pytest verbosely with -s, it will print the error from the fixture and then run indefinitely.

Example code that will cause this indefinite running:

import launch_pytest
import pytest
import rclpy.node
from ament_index_python.packages import get_package_share_directory
from launch.actions import IncludeLaunchDescription
from launch.launch_description import LaunchDescription
from launch.launch_description_sources import PythonLaunchDescriptionSource
from launch.substitutions import PathJoinSubstitution

# A launch fixture with an error (can be any error that happens at runtime on the LaunchDescription level)
# (not a Python error)
# (In this case we use a launch file path that does not exist)
@launch_pytest.fixture
def launch_description() -> LaunchDescription:
    package_launch = IncludeLaunchDescription(
        PythonLaunchDescriptionSource(
            PathJoinSubstitution(
                [
                    get_package_share_directory("package_name"),
                    "thisIsAWrongPathThatWillLeadToNoSuchFileAndThrowAnError",
                    "package.launch.py",
                ]
            )
        )
    )
    return LaunchDescription(
        [
            package_launch,
            launch_pytest.actions.ReadyToTest(),
        ]
    )

# test that uses the failing fixture
@pytest.mark.launch(fixture=launch_description)
def test_anything() -> None:
    rclpy.init()
    try:
        # does not matter what we do here, the test will get stuck setting up the fixture before this
        assert True
    finally:
        rclpy.shutdown()

Expected behavior

Error from fixture setup leads to test failing.

Actual behavior

Fixture error is printed, when pytest is run with -s, but the test keeps running indefinitely, unless terminated from the outside by an interrupt or by using an external tool like pytest-timeout.

JustusBraun commented 5 months ago

Did you find a solution? I am having the same problem. I have a fixture which launches a Node which then crashes. When the Node crashes the Testcase just hangs indefinitely. The README says that Test can fail if a Node dies unexpectedly, is there an argument that needs to be passed to enable this behaviour?