How to use _get_child_mock method in autotest

Best Python code snippet using autotest_python

tools.py

Source:tools.py Github

copy

Full Screen

...87 def __call__(*args, **kwargs):88 value_mock = method_mock._orig___call__(*args, **kwargs)89 value_mock.__json__ = lambda: {}90 return value_mock91 def _get_child_mock(**kwargs):92 value_mock = method_mock._orig__get_child_mock(**kwargs)93 value_mock.__json__ = lambda: {}94 return value_mock95 return __call__, _get_child_mock96def make_mock_plugin_json_encodable(plugin_instance_mock):97 # NOTE(yamahata): Make return value of plugin method json encodable98 # e.g. the return value of plugin_instance.create_network() needs99 # to be json encodable100 # plugin instance -> method -> return value101 # Mock MagicMock Mock102 # plugin_instance_mock method_mock value_mock103 #104 # From v1.3 of pecan, pecan.jsonify uses json.Encoder unconditionally.105 # pecan v1.2 uses simplejson.Encoder which accidentally encodes106 # Mock as {} due to check of '_asdict' attributes....

Full Screen

Full Screen

mock_spy.py

Source:mock_spy.py Github

copy

Full Screen

...43 spy = cls.spy_for_mock(mock)44 if spy:45 spy.spy_getattribute(name)46 @classmethod47 def class_spy_get_child_mock(cls, _parent, child):48 if not cls._ACTIVE:49 return50 spy = cls.spy_for_mock(child)51 if spy:52 spy.is_child = True53 def spy_getattribute(self, name):54 if name in _CALL_ATTRIBUTES:55 self.checked_calls = True56 @property57 @nospy58 def used(self):59 if self.mock.mock_calls:60 # The mock was used since it was called.61 return True62 if self.checked_calls:63 # The mock was not called, but mock_calls was called64 # (hopefully to verify that the mock was not called...)65 # so it counts as used66 return True67 if self.is_child:68 # The mock was not called, but this mock is the child of69 # another (e.g. created as a MagicMock return value),70 # so it counts as used71 return True72 @classmethod73 def reset(cls):74 cls._ALL = []75 @classmethod76 def all(cls):77 return cls._ALL[:]78 @classmethod79 def start(cls):80 cls._ALL = []81 cls._ACTIVE = True82 mock = sys.modules.get('mock')83 setattr(mock.Mock, '__new__', cls._new_mock)84 @classmethod85 def stop(cls):86 cls._ACTIVE = False87 mock = sys.modules.get('mock')88 delattr(mock.Mock, '__new__')89 @classmethod90 def spy_for_mock(cls, mock):91 for spy in cls._ALL:92 if spy.mock is mock:93 return spy94 @classmethod95 def _new_mock(cls, mock_cls, *_args, **_kwargs):96 mock_cls = cls._mock_subclass(mock_cls)97 return object.__new__(mock_cls)98 @classmethod99 def _mock_subclass(cls, base):100 if base not in cls._SPY_CLASSES:101 cls._SPY_CLASSES[base] = cls._new_mock_subclass(base)102 return cls._SPY_CLASSES[base]103 @classmethod104 def _new_mock_subclass(cls, base):105 def __init__(self, *args, **kwargs):106 base.__init__(self, *args, **kwargs)107 cls.class_spy_init(self)108 def __getattribute__(self, name):109 try:110 return base.__getattribute__(self, name)111 finally:112 cls.class_spy_getattribute(self, name)113 def _get_child_mock(self, *args, **kwargs):114 child = base._get_child_mock(self, *args, **kwargs) # pylint: disable=protected-access115 cls.class_spy_get_child_mock(self, child)116 return child117 name = base.__name__ + 'Spy'118 klass_dict = {'__init__': __init__,119 '__getattribute__': __getattribute__,120 '_get_child_mock': _get_child_mock}121 klass = type(name, (base,), klass_dict)122 return klass123 @classmethod124 def suspended(cls, fn, *args, **kwargs):125 old_active = cls._ACTIVE126 cls._ACTIVE = False127 try:128 return fn(*args, **kwargs)129 finally:...

Full Screen

Full Screen

test_tsmock.py

Source:test_tsmock.py Github

copy

Full Screen

1"""Unittests for tsmock."""2# pylint: disable=protected-access, redefined-outer-name, unused-argument3import threading4import time5from unittest.mock import MagicMock6import pytest7from tsmock import thread_safe_mocks, thread_unsafe_mocks, MagicMock as TSMagicMock8@pytest.fixture9def gcm_patch():10 """Emulate a slow _get_child_mock function."""11 old_gcm = MagicMock._get_child_mock12 def slow_gcm(*arg, **kw):13 time.sleep(0.1)14 return old_gcm(*arg, **kw)15 MagicMock._get_child_mock = slow_gcm16 yield17 MagicMock._get_child_mock = old_gcm18@pytest.mark.parametrize("passing", [2, 1, 0])19def test_call_func(gcm_patch, passing):20 """Test calling the mock in parallel, with a slow gcm."""21 nb_threads = 1022 if passing == 1:23 thread_safe_mocks()24 mock = MagicMock()25 elif passing == 0:26 thread_unsafe_mocks()27 mock = MagicMock()28 else:29 thread_unsafe_mocks()30 mock = TSMagicMock()31 threads = []32 event = threading.Event()33 for _ in range(nb_threads):34 thread = threading.Thread(target=lambda: (event.wait(), mock.func.call_count))35 threads.append(thread)36 thread = threading.Thread(target=lambda: (event.wait(), mock.func()))37 threads.append(thread)38 for thread in threads:39 thread.start()40 event.set()41 for thread in threads:42 thread.join()43 if passing != 0:44 assert mock.func.call_count == nb_threads45 else:...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run autotest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful