forked from felixdivo/ros2-easy-test
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhypothesis_test.py
64 lines (49 loc) · 2.09 KB
/
hypothesis_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# Standard library
import unittest
# Hypothesis and interfaces
from hypothesis import given, settings
from hypothesis.strategies import DrawFn, characters, composite, text
from std_msgs.msg import String
# Testing
from ros2_easy_test import ROS2TestEnvironment, with_single_node
# Module under test
from ..example_nodes.well_behaved import EchoNode
@composite
def ros2_preserved_string(draw: DrawFn) -> str:
"""We need to exclude NULL characters, because they are not preserved by ROS2."""
return draw(
text(
alphabet=characters(
blacklist_categories=[
"Cs",
],
min_codepoint=1,
)
)
)
@with_single_node(EchoNode, watch_topics={"/mouth": String})
@given(some_message=ros2_preserved_string())
@settings(max_examples=10, deadline=None) # Remember that these tests are costly
def test_on_same_node(some_message: str, env: ROS2TestEnvironment) -> None:
"""This creates a single node and tests it with Hypothesis against many values.
Note:
You'll want to turn of the deadline, because these tests are costly.
"""
env.publish("/ear", String(data=some_message))
response: str = env.assert_message_published("/mouth").data
assert response == some_message, (response, some_message)
@given(some_message=ros2_preserved_string())
@settings(max_examples=10, deadline=None) # Remember that these tests are costly
@with_single_node(EchoNode, watch_topics={"/mouth": String})
def test_on_new_node_each(*, env: ROS2TestEnvironment, some_message: str) -> None:
"""This creates a single node and tests it with Hypothesis against many values.
Note:
This also switches the order of the arguments.
To make that work, we need to use the ``*`` in the function signature
to make ``env`` and ``some_message`` keyword-only arguments.
"""
env.publish("/ear", String(data=some_message))
response: str = env.assert_message_published("/mouth").data
assert response == some_message, (response, some_message)
if __name__ == "__main__":
unittest.main()