|
| 1 | +# Copyright 2021 Open Source Robotics Foundation, Inc. |
| 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 | +import unittest |
| 16 | + |
| 17 | +import launch |
| 18 | +import launch.actions |
| 19 | +import launch_testing.actions |
| 20 | +import launch_testing.markers |
| 21 | +import pytest |
| 22 | + |
| 23 | + |
| 24 | +# This function specifies the processes to be run for our test |
| 25 | +@pytest.mark.launch_test |
| 26 | +@launch_testing.markers.keep_alive |
| 27 | +def generate_test_description(): |
| 28 | + """Launch a simple process to print 'hello_world'.""" |
| 29 | + return launch.LaunchDescription([ |
| 30 | + # Launch a process to test |
| 31 | + launch.actions.ExecuteProcess( |
| 32 | + cmd=['echo', 'hello_world'], |
| 33 | + shell=True |
| 34 | + ), |
| 35 | + # Tell launch to start the test |
| 36 | + launch_testing.actions.ReadyToTest() |
| 37 | + ]) |
| 38 | + |
| 39 | + |
| 40 | +# This is our test fixture. Each method is a test case. |
| 41 | +# These run alongside the processes specified in generate_test_description() |
| 42 | +class TestHelloWorldProcess(unittest.TestCase): |
| 43 | + |
| 44 | + def test_read_stdout(self, proc_output): |
| 45 | + """Check if 'hello_world' was found in the stdout.""" |
| 46 | + # 'proc_output' is an object added automatically by the launch_testing framework. |
| 47 | + # It captures the outputs of the processes launched in generate_test_description() |
| 48 | + # Refer to the documentation for further details. |
| 49 | + proc_output.assertWaitFor('hello_world', timeout=10, stream='stdout') |
| 50 | + |
| 51 | + |
| 52 | +# These tests are run after the processes in generate_test_description() have shutdown. |
| 53 | +@launch_testing.post_shutdown_test() |
| 54 | +class TestHelloWorldShutdown(unittest.TestCase): |
| 55 | + |
| 56 | + def test_exit_codes(self, proc_info): |
| 57 | + """Check if the processes exited normally.""" |
| 58 | + launch_testing.asserts.assertExitCodes(proc_info) |
0 commit comments