|
1 | 1 | import os
|
2 | 2 | import json
|
| 3 | +from types import FunctionType |
3 | 4 |
|
4 | 5 | import pytest
|
5 | 6 | import jsonschema
|
@@ -36,6 +37,11 @@ def benchmark():
|
36 | 37 | else:
|
37 | 38 | del pytest_benchmark
|
38 | 39 |
|
| 40 | +try: |
| 41 | + from unittest import mock # python 3.3 and above |
| 42 | +except ImportError: |
| 43 | + import mock # python < 3.3 |
| 44 | + |
39 | 45 |
|
40 | 46 | @pytest.fixture(autouse=True)
|
41 | 47 | def internal_exceptions(request, monkeypatch):
|
@@ -327,3 +333,83 @@ def render_span(span):
|
327 | 333 | return "\n".join(render_span(root_span))
|
328 | 334 |
|
329 | 335 | return inner
|
| 336 | + |
| 337 | + |
| 338 | +@pytest.fixture(name="StringContaining") |
| 339 | +def string_containing_matcher(): |
| 340 | + """ |
| 341 | + An object which matches any string containing the substring passed to the |
| 342 | + object at instantiation time. |
| 343 | +
|
| 344 | + Useful for assert_called_with, assert_any_call, etc. |
| 345 | +
|
| 346 | + Used like this: |
| 347 | +
|
| 348 | + >>> f = mock.Mock(return_value=None) |
| 349 | + >>> f("dogs are great") |
| 350 | + >>> f.assert_any_call("dogs") # will raise AssertionError |
| 351 | + Traceback (most recent call last): |
| 352 | + ... |
| 353 | + AssertionError: mock('dogs') call not found |
| 354 | + >>> f.assert_any_call(StringContaining("dogs")) # no AssertionError |
| 355 | +
|
| 356 | + """ |
| 357 | + |
| 358 | + class StringContaining(object): |
| 359 | + def __init__(self, substring): |
| 360 | + self.substring = substring |
| 361 | + |
| 362 | + def __eq__(self, test_string): |
| 363 | + if not isinstance(test_string, str): |
| 364 | + return False |
| 365 | + |
| 366 | + return self.substring in test_string |
| 367 | + |
| 368 | + return StringContaining |
| 369 | + |
| 370 | + |
| 371 | +@pytest.fixture(name="DictionaryContaining") |
| 372 | +def dictionary_containing_matcher(): |
| 373 | + """ |
| 374 | + An object which matches any dictionary containing all key-value pairs from |
| 375 | + the dictionary passed to the object at instantiation time. |
| 376 | +
|
| 377 | + Useful for assert_called_with, assert_any_call, etc. |
| 378 | +
|
| 379 | + Used like this: |
| 380 | +
|
| 381 | + >>> f = mock.Mock(return_value=None) |
| 382 | + >>> f({"dogs": "yes", "cats": "maybe"}) |
| 383 | + >>> f.assert_any_call({"dogs": "yes"}) # will raise AssertionError |
| 384 | + Traceback (most recent call last): |
| 385 | + ... |
| 386 | + AssertionError: mock({'dogs': 'yes'}) call not found |
| 387 | + >>> f.assert_any_call(DictionaryContaining({"dogs": "yes"})) # no AssertionError |
| 388 | + """ |
| 389 | + |
| 390 | + class DictionaryContaining(object): |
| 391 | + def __init__(self, subdict): |
| 392 | + self.subdict = subdict |
| 393 | + |
| 394 | + def __eq__(self, test_dict): |
| 395 | + if not isinstance(test_dict, dict): |
| 396 | + return False |
| 397 | + |
| 398 | + return all(test_dict.get(key) == self.subdict[key] for key in self.subdict) |
| 399 | + |
| 400 | + return DictionaryContaining |
| 401 | + |
| 402 | + |
| 403 | +@pytest.fixture(name="FunctionMock") |
| 404 | +def function_mock(): |
| 405 | + """ |
| 406 | + Just like a mock.Mock object, but one which always passes an isfunction |
| 407 | + test. |
| 408 | + """ |
| 409 | + |
| 410 | + class FunctionMock(mock.Mock): |
| 411 | + def __init__(self, *args, **kwargs): |
| 412 | + super(FunctionMock, self).__init__(*args, **kwargs) |
| 413 | + self.__class__ = FunctionType |
| 414 | + |
| 415 | + return FunctionMock |
0 commit comments