Skip to content

Commit 026ab0e

Browse files
Add a "hello world" style example (#532)
* Added hello_world example and updated README Signed-off-by: Aditya Pande <[email protected]> * Added marker Signed-off-by: Aditya Pande <[email protected]> * Updated readme, docstrings Signed-off-by: Aditya Pande <[email protected]> * Updated readme, added comments to .py file Signed-off-by: Aditya Pande <[email protected]> * Updated comment in .py file Signed-off-by: Aditya Pande <[email protected]> * Added shell='True' for windows CI Signed-off-by: Aditya Pande <[email protected]> * typo fix Signed-off-by: Aditya Pande <[email protected]>
1 parent 90a245b commit 026ab0e

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

launch_testing/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,21 @@ add_launch_test(
201201

202202
## Examples
203203

204+
### `hello_world_launch_test.py`
205+
206+
Usage:
207+
208+
```sh
209+
launch_test test/launch_testing/examples/hello_world_launch_test.py
210+
```
211+
212+
This test is a simple example on how to use the ``launch_testing``.
213+
214+
It launches a process and asserts that it prints "hello_world" to ``stdout`` using ``proc_output.assertWaitFor()``.
215+
Finally, it checks if the process exits normally (zero exit code).
216+
217+
The ``@launch_testing.markers.keep_alive`` decorator ensures that the launch process stays alive long enough for the tests to run.
218+
204219
### `good_proc_launch_test.py`
205220

206221
Usage:
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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

Comments
 (0)