Debian Codegraph @

tests/unit/test_context_cleanups.py

raw ◀ ▶
Source reconstructed at patch state 1 of 2 — not the installed (fully patched) source.
2 Debian patches touch this file
1 # -*- coding: UTF-8 -*-
2 """
3 Test the cleanup-funcs functionality provided via
4 :meth:`behave.runner.Context.add_cleanup()` method.
5
6 OPEN ISSUES:
7 * Should cleanup_func use context args, like: cleanup_func(context) ?
8 * Should formatters be somehow informed about cleanup-errors ?
9 * Should context._pop() calls be protected against raised exceptions
10 from on_cleanup_error() implementations ?
11 """
12
13 from __future__ import print_function
14 from behave.runner import Context, scoped_context_layer
15 from contextlib import contextmanager
16 from mock import Mock, NonCallableMock
17 import pytest
18
19
20 # ------------------------------------------------------------------------------
21 # TEST SUPPORT:
22 # ------------------------------------------------------------------------------
23 def cleanup_func():
24 pass
25
26 class CleanupFunction(object):
27 def __init__(self, name="CLEANUP-FUNC", listener=None):
28 self.name = name
29 self.listener = listener
30
31 def __call__(self, *args, **kwargs):
32 if self.listener:
33 message = "called:%s" % self.name
34 self.listener(message)
35
36
37 class CallListener(object):
38 def __init__(self):
39 self.collected = []
40
41 def __call__(self, message):
42 self.collected.append(message)
43
44
45
46 # ------------------------------------------------------------------------------
47 # TESTS:
48 # ------------------------------------------------------------------------------
49 class TestContextCleanup(object):
50
51 def test_cleanup_func_is_called_when_context_frame_is_popped(self):
52 my_cleanup = Mock(spec=cleanup_func)
53 context = Context(runner=Mock())
54 with scoped_context_layer(context): # CALLS-HERE: context._push()
55 context.add_cleanup(my_cleanup)
56 # -- ENSURE: Not called before context._pop()
57 my_cleanup.assert_not_called()
58 # CALLS-HERE: context._pop()
59 my_cleanup.assert_called_once()
60 # MAYBE: my_cleanup2.assert_called_with(context)
61
62 def test_cleanup_funcs_are_called_when_context_frame_is_popped(self):
63 my_cleanup1 = Mock(spec=cleanup_func)
64 my_cleanup2 = Mock(spec=cleanup_func)
65
66 # -- SETUP:
67 context = Context(runner=Mock())
68 with scoped_context_layer(context):
69 context.add_cleanup(my_cleanup1)
70 context.add_cleanup(my_cleanup2)
71
72 # -- ENSURE: Not called before context._pop()
73 my_cleanup1.assert_not_called()
74 my_cleanup2.assert_not_called()
75 # -- CALLS-HERE: context._pop()
76 my_cleanup1.assert_called_once()
77 my_cleanup2.assert_called_once()
78
79 def test_cleanup_funcs_are_called_in_reversed_order(self):
80 call_listener = CallListener()
81 my_cleanup1A = CleanupFunction("CLEANUP1", listener=call_listener)
82 my_cleanup2A = CleanupFunction("CLEANUP2", listener=call_listener)
83 my_cleanup1 = Mock(side_effect=my_cleanup1A)
84 my_cleanup2 = Mock(side_effect=my_cleanup2A)
85
86 # -- SETUP:
87 context = Context(runner=Mock())
88 with scoped_context_layer(context):
89 context.add_cleanup(my_cleanup1)
90 context.add_cleanup(my_cleanup2)
91 my_cleanup1.assert_not_called()
92 my_cleanup2.assert_not_called()
93
94 # -- ENSURE: Reversed order of cleanup calls
95 expected_call_order = ["called:CLEANUP2", "called:CLEANUP1"]
96 assert call_listener.collected == expected_call_order
97 my_cleanup1.assert_called_once()
98 my_cleanup2.assert_called_once()
99
100 def test_cleanup_funcs_on_two_context_frames(self):
101 call_listener = CallListener()
102 my_cleanup_A1 = CleanupFunction("CLEANUP_A1", listener=call_listener)
103 my_cleanup_A2 = CleanupFunction("CLEANUP_A2", listener=call_listener)
104 my_cleanup_B1 = CleanupFunction("CLEANUP_B1", listener=call_listener)
105 my_cleanup_B2 = CleanupFunction("CLEANUP_B2", listener=call_listener)
106 my_cleanup_B3 = CleanupFunction("CLEANUP_B3", listener=call_listener)
107 my_cleanup_A1M = Mock(side_effect=my_cleanup_A1)
108 my_cleanup_A2M = Mock(side_effect=my_cleanup_A2)
109 my_cleanup_B1M = Mock(side_effect=my_cleanup_B1)
110 my_cleanup_B2M = Mock(side_effect=my_cleanup_B2)
111 my_cleanup_B3M = Mock(side_effect=my_cleanup_B3)
112
113 # -- SETUP:
114 context = Context(runner=Mock())
115 with scoped_context_layer(context): # -- LAYER A:
116 context.add_cleanup(my_cleanup_A1M)
117 context.add_cleanup(my_cleanup_A2M)
118
119 with scoped_context_layer(context): # -- LAYER B:
120 context.add_cleanup(my_cleanup_B1M)
121 context.add_cleanup(my_cleanup_B2M)
122 context.add_cleanup(my_cleanup_B3M)
123 my_cleanup_B1M.assert_not_called()
124 my_cleanup_B2M.assert_not_called()
125 my_cleanup_B3M.assert_not_called()
126 # -- context.pop(LAYER_B): Call cleanups for Bx
127 expected_call_order = [
128 "called:CLEANUP_B3", "called:CLEANUP_B2", "called:CLEANUP_B1",
129 ]
130 assert call_listener.collected == expected_call_order
131 my_cleanup_A1M.assert_not_called()
132 my_cleanup_A2M.assert_not_called()
133 my_cleanup_B1M.assert_called_once()
134 my_cleanup_B2M.assert_called_once()
135 my_cleanup_B3M.assert_called_once()
136 # -- context.pop(LAYER_A): Call cleanups for Ax
137 expected_call_order = [
138 "called:CLEANUP_B3", "called:CLEANUP_B2", "called:CLEANUP_B1",
139 "called:CLEANUP_A2", "called:CLEANUP_A1",
140 ]
141 assert call_listener.collected == expected_call_order
142 my_cleanup_A1M.assert_called_once()
143 my_cleanup_A2M.assert_called_once()
144 my_cleanup_B1M.assert_called_once()
145 my_cleanup_B2M.assert_called_once()
146 my_cleanup_B3M.assert_called_once()
147
148 def test_add_cleanup__rejects_noncallable_cleanup_func(self):
149 class NonCallable(object): pass
150 non_callable = NonCallable()
151 context = Context(runner=Mock())
152
153 with pytest.raises(AssertionError) as e:
154 with scoped_context_layer(context):
155 context.add_cleanup(non_callable)
156 assert "REQUIRES: callable(cleanup_func)" in str(e.value)
157
158 def test_on_cleanup_error__prints_error_by_default(self, capsys):
159 def bad_cleanup_func():
160 raise RuntimeError("in CLEANUP call")
161 bad_cleanup = Mock(side_effect=bad_cleanup_func)
162
163 context = Context(runner=Mock())
164 with pytest.raises(RuntimeError):
165 with scoped_context_layer(context):
166 context.add_cleanup(bad_cleanup)
167
168 captured_output, _ = capsys.readouterr()
169 bad_cleanup.assert_called()
170 assert "CLEANUP-ERROR in " in captured_output
171 assert "RuntimeError: in CLEANUP call" in captured_output
172 # -- FOR DIAGNOSTICS:
173 print(captured_output)
174
175 def test_on_cleanup_error__is_called_if_defined(self):
176 def bad_cleanup():
177 raise RuntimeError("in CLEANUP call")
178 def handle_cleanup_error(context, cleanup_func, exception):
179 print("CALLED: handle_cleanup_error")
180
181 context = Context(runner=Mock())
182 handle_cleanup_error_func = Mock(spec=handle_cleanup_error)
183 with pytest.raises(RuntimeError):
184 with scoped_context_layer(context):
185 context.on_cleanup_error = handle_cleanup_error_func
186 context.add_cleanup(bad_cleanup)
187
188 handle_cleanup_error_func.assert_called_once()
189 # expected_args = (context, handle_cleanup_error_func,
190 # RuntimeError("in CLEANUP call"))
191 # handle_cleanup_error_func.assert_called_with(*expected_args)
192
193 def test_on_cleanup_error__may_be_called_several_times_per_cleanup(self):
194 def bad_cleanup1():
195 raise RuntimeError("CLEANUP_1")
196 def bad_cleanup2():
197 raise RuntimeError("CLEANUP_2")
198
199 class CleanupErrorCollector(object):
200 def __init__(self):
201 self.collected = []
202 def __call__(self, context, cleanup_func, exception):
203 self.collected.append((context, cleanup_func, exception))
204
205 context = Context(runner=Mock())
206 collect_cleanup_error = CleanupErrorCollector()
207 with pytest.raises(RuntimeError):
208 with scoped_context_layer(context):
209 context.on_cleanup_error = collect_cleanup_error
210 context.add_cleanup(bad_cleanup1)
211 context.add_cleanup(bad_cleanup2)
212
213 expected = [
214 (context, bad_cleanup2, RuntimeError("CLEANUP_2")),
215 (context, bad_cleanup1, RuntimeError("CLEANUP_1")),
216 ]
217 assert len(collect_cleanup_error.collected) == 2
218 assert collect_cleanup_error.collected[0][:-1] == expected[0][:-1]
219 assert collect_cleanup_error.collected[1][:-1] == expected[1][:-1]
220