robotframework-mock 0.6.0__tar.gz → 0.7.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (16) hide show
  1. {robotframework_mock-0.6.0/src/robotframework_mock.egg-info → robotframework_mock-0.7.1}/PKG-INFO +24 -1
  2. {robotframework_mock-0.6.0 → robotframework_mock-0.7.1}/README.md +23 -0
  3. {robotframework_mock-0.6.0 → robotframework_mock-0.7.1}/setup.cfg +1 -1
  4. {robotframework_mock-0.6.0 → robotframework_mock-0.7.1}/src/MockResource/__init__.py +40 -6
  5. {robotframework_mock-0.6.0 → robotframework_mock-0.7.1/src/robotframework_mock.egg-info}/PKG-INFO +24 -1
  6. {robotframework_mock-0.6.0 → robotframework_mock-0.7.1}/LICENSE +0 -0
  7. {robotframework_mock-0.6.0 → robotframework_mock-0.7.1}/MANIFEST.in +0 -0
  8. {robotframework_mock-0.6.0 → robotframework_mock-0.7.1}/pyproject.toml +0 -0
  9. {robotframework_mock-0.6.0 → robotframework_mock-0.7.1}/setup.py +0 -0
  10. {robotframework_mock-0.6.0 → robotframework_mock-0.7.1}/src/MockCoverage/__init__.py +0 -0
  11. {robotframework_mock-0.6.0 → robotframework_mock-0.7.1}/src/MockLibrary/__init__.py +0 -0
  12. {robotframework_mock-0.6.0 → robotframework_mock-0.7.1}/src/_mock_core/__init__.py +0 -0
  13. {robotframework_mock-0.6.0 → robotframework_mock-0.7.1}/src/robotframework_mock.egg-info/SOURCES.txt +0 -0
  14. {robotframework_mock-0.6.0 → robotframework_mock-0.7.1}/src/robotframework_mock.egg-info/dependency_links.txt +0 -0
  15. {robotframework_mock-0.6.0 → robotframework_mock-0.7.1}/src/robotframework_mock.egg-info/requires.txt +0 -0
  16. {robotframework_mock-0.6.0 → robotframework_mock-0.7.1}/src/robotframework_mock.egg-info/top_level.txt +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: robotframework-mock
3
- Version: 0.6.0
3
+ Version: 0.7.1
4
4
  Summary: A Robot Framework library for mocking keywords in unit tests
5
5
  Home-page: https://github.com/olesz/robotframework-mock
6
6
  Author: Lajos Olah
@@ -328,6 +328,29 @@ ${rows}= Evaluate [['Europe/Budapest']]
328
328
  MockRes.Mock Keyword Run Query return_value=${rows}
329
329
  ```
330
330
 
331
+ ### Mocked keyword setup and teardown
332
+
333
+ `MockResource` replaces only a keyword's **body**. Its `[Setup]` and `[Teardown]`
334
+ still run, which keeps them observable — a test can assert that a teardown
335
+ released a lock, for instance.
336
+
337
+ That is a problem when the teardown cleans up state the replaced body would have
338
+ created, because it then fails on the mocked call. Suppress it per mock:
339
+
340
+ ```robot
341
+ # Init Client acquires a lock in its body and releases it in its teardown.
342
+ # Without the body there is no lock to release, so skip the teardown.
343
+ MockRes.Mock Keyword Init Client return_value=alias skip_teardown=${True}
344
+ MockRes.Mock Keyword Init Client return_value=alias skip_setup=${True}
345
+ ```
346
+
347
+ Both default to `False`, so existing tests are unaffected. `Reset Mocks`
348
+ restores a suppressed setup or teardown along with the body.
349
+
350
+ Prefer the default when the setup or teardown is part of the contract you are
351
+ testing, and skip it when it is an implementation detail of a keyword the test
352
+ is only standing in for.
353
+
331
354
  ### Reset Mocks
332
355
 
333
356
  Restore all mocked keywords to their original implementations.
@@ -304,6 +304,29 @@ ${rows}= Evaluate [['Europe/Budapest']]
304
304
  MockRes.Mock Keyword Run Query return_value=${rows}
305
305
  ```
306
306
 
307
+ ### Mocked keyword setup and teardown
308
+
309
+ `MockResource` replaces only a keyword's **body**. Its `[Setup]` and `[Teardown]`
310
+ still run, which keeps them observable — a test can assert that a teardown
311
+ released a lock, for instance.
312
+
313
+ That is a problem when the teardown cleans up state the replaced body would have
314
+ created, because it then fails on the mocked call. Suppress it per mock:
315
+
316
+ ```robot
317
+ # Init Client acquires a lock in its body and releases it in its teardown.
318
+ # Without the body there is no lock to release, so skip the teardown.
319
+ MockRes.Mock Keyword Init Client return_value=alias skip_teardown=${True}
320
+ MockRes.Mock Keyword Init Client return_value=alias skip_setup=${True}
321
+ ```
322
+
323
+ Both default to `False`, so existing tests are unaffected. `Reset Mocks`
324
+ restores a suppressed setup or teardown along with the body.
325
+
326
+ Prefer the default when the setup or teardown is part of the contract you are
327
+ testing, and skip it when it is an implementation detail of a keyword the test
328
+ is only standing in for.
329
+
307
330
  ### Reset Mocks
308
331
 
309
332
  Restore all mocked keywords to their original implementations.
@@ -1,6 +1,6 @@
1
1
  [metadata]
2
2
  name = robotframework-mock
3
- version = 0.6.0
3
+ version = 0.7.1
4
4
  author = Lajos Olah
5
5
  author_email = lajos.olah.jr@gmail.com
6
6
  description = A Robot Framework library for mocking keywords in unit tests
@@ -74,6 +74,8 @@ class MockResource(CallInspectionMixin):
74
74
  self._source = source
75
75
  self._original_get_runner = Namespace.get_runner
76
76
  self._original_items = {}
77
+ self._original_setups = {}
78
+ self._original_teardowns = {}
77
79
  self._mocks = {}
78
80
  self._install_patch()
79
81
 
@@ -112,30 +114,53 @@ class MockResource(CallInspectionMixin):
112
114
  @keyword
113
115
  def mock_keyword(
114
116
  self, keyword_name: str,
115
- return_value: Any = None, side_effect: Callable = None
117
+ return_value: Any = None, side_effect: Callable = None,
118
+ skip_setup: bool = False, skip_teardown: bool = False
116
119
  ):
117
120
  """Mock a keyword from the resource file.
118
-
121
+
122
+ Only the keyword's body is replaced. Its ``[Setup]`` and ``[Teardown]``
123
+ still run by default, which keeps them observable - a test can assert
124
+ that a teardown released a lock, for example. Suppress them when the
125
+ mocked keyword's own setup or teardown is an implementation detail the
126
+ test should not depend on, typically because it cleans up state that the
127
+ replaced body would have created and therefore fails.
128
+
119
129
  Args:
120
130
  keyword_name: Name of the keyword to mock
121
- return_value: Value to return when the keyword is called
131
+ return_value: Value to return when the keyword is called. May be any
132
+ Python object.
122
133
  side_effect: Callable to execute instead of returning a value.
123
134
  Receives the call's resolved arguments, the same way a
124
135
  ``MockLibrary`` side effect does.
136
+ skip_setup: Do not run the keyword's ``[Setup]`` while mocked.
137
+ skip_teardown: Do not run the keyword's ``[Teardown]`` while mocked.
125
138
 
126
139
  Returns:
127
140
  The :class:`unittest.mock.Mock` backing the keyword.
128
141
 
142
+ Raises:
143
+ AttributeError: If the keyword is not found in the resource file.
144
+
129
145
  Example:
130
146
  | MockRes.Mock Keyword | My Keyword | return_value=test_data |
147
+ | MockRes.Mock Keyword | Init Client | skip_teardown=${True} |
131
148
  """
132
149
  keyword_runner = BuiltIn()._namespace.get_runner(keyword_name, True) # pylint: disable=protected-access
133
150
  resource_file = getattr(keyword_runner.keyword, "source", None)
134
151
 
135
152
  if self._source not in str(resource_file):
136
- raise AttributeError("fKeyword '{keyword_name}' not found in {self._source}")
153
+ raise AttributeError(
154
+ f"Keyword '{keyword_name}' not found in {self._source}"
155
+ )
137
156
 
138
157
  self._original_items[keyword_name] = keyword_runner.keyword.body._items # pylint: disable=protected-access
158
+ if skip_setup:
159
+ self._original_setups[keyword_name] = keyword_runner.keyword.setup
160
+ keyword_runner.keyword.setup = None
161
+ if skip_teardown:
162
+ self._original_teardowns[keyword_name] = keyword_runner.keyword.teardown
163
+ keyword_runner.keyword.teardown = None
139
164
  mock = Mock(return_value=return_value, side_effect=side_effect)
140
165
  self._mocks[keyword_name] = mock
141
166
  return mock
@@ -144,8 +169,9 @@ class MockResource(CallInspectionMixin):
144
169
  def reset_mocks(self):
145
170
  """Reset all mocks to their original implementations.
146
171
 
147
- Restores all mocked keywords to their original behavior.
148
-
172
+ Restores all mocked keywords to their original behavior, including any
173
+ ``[Setup]`` or ``[Teardown]`` that was suppressed while mocked.
174
+
149
175
  Example:
150
176
  | MockRes.Reset Mocks |
151
177
  """
@@ -153,7 +179,15 @@ class MockResource(CallInspectionMixin):
153
179
  for keyword_name, items in self._original_items.items():
154
180
  keyword_runner = BuiltIn()._namespace.get_runner(keyword_name, True) # pylint: disable=protected-access
155
181
  keyword_runner.keyword.body._items = items # pylint: disable=protected-access
182
+ for keyword_name, setup in self._original_setups.items():
183
+ keyword_runner = BuiltIn()._namespace.get_runner(keyword_name, True) # pylint: disable=protected-access
184
+ keyword_runner.keyword.setup = setup
185
+ for keyword_name, teardown in self._original_teardowns.items():
186
+ keyword_runner = BuiltIn()._namespace.get_runner(keyword_name, True) # pylint: disable=protected-access
187
+ keyword_runner.keyword.teardown = teardown
156
188
  self._original_items.clear()
189
+ self._original_setups.clear()
190
+ self._original_teardowns.clear()
157
191
 
158
192
  @keyword
159
193
  def verify_keyword_called(self, keyword_name: str, times: int = None):
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: robotframework-mock
3
- Version: 0.6.0
3
+ Version: 0.7.1
4
4
  Summary: A Robot Framework library for mocking keywords in unit tests
5
5
  Home-page: https://github.com/olesz/robotframework-mock
6
6
  Author: Lajos Olah
@@ -328,6 +328,29 @@ ${rows}= Evaluate [['Europe/Budapest']]
328
328
  MockRes.Mock Keyword Run Query return_value=${rows}
329
329
  ```
330
330
 
331
+ ### Mocked keyword setup and teardown
332
+
333
+ `MockResource` replaces only a keyword's **body**. Its `[Setup]` and `[Teardown]`
334
+ still run, which keeps them observable — a test can assert that a teardown
335
+ released a lock, for instance.
336
+
337
+ That is a problem when the teardown cleans up state the replaced body would have
338
+ created, because it then fails on the mocked call. Suppress it per mock:
339
+
340
+ ```robot
341
+ # Init Client acquires a lock in its body and releases it in its teardown.
342
+ # Without the body there is no lock to release, so skip the teardown.
343
+ MockRes.Mock Keyword Init Client return_value=alias skip_teardown=${True}
344
+ MockRes.Mock Keyword Init Client return_value=alias skip_setup=${True}
345
+ ```
346
+
347
+ Both default to `False`, so existing tests are unaffected. `Reset Mocks`
348
+ restores a suppressed setup or teardown along with the body.
349
+
350
+ Prefer the default when the setup or teardown is part of the contract you are
351
+ testing, and skip it when it is an implementation detail of a keyword the test
352
+ is only standing in for.
353
+
331
354
  ### Reset Mocks
332
355
 
333
356
  Restore all mocked keywords to their original implementations.