How to use _start_patch method in pytest-mock

Best Python code snippet using pytest-mock

plugin.py

Source:plugin.py Github

copy

Full Screen

...115 def __init__(self, patches, mocks, mock_module):116 self._patches = patches117 self._mocks = mocks118 self.mock_module = mock_module119 def _start_patch(self, mock_func, *args, **kwargs):120 """Patches something by calling the given function from the mock121 module, registering the patch to stop it later and returns the122 mock object resulting from the mock call.123 """124 p = mock_func(*args, **kwargs)125 mocked = p.start()126 self._patches.append(p)127 if hasattr(mocked, "reset_mock"):128 self._mocks.append(mocked)129 return mocked130 def object(self, *args, **kwargs):131 """API to mock.patch.object"""132 return self._start_patch(self.mock_module.patch.object, *args, **kwargs)133 def multiple(self, *args, **kwargs):134 """API to mock.patch.multiple"""135 return self._start_patch(self.mock_module.patch.multiple, *args, **kwargs)136 def dict(self, *args, **kwargs):137 """API to mock.patch.dict"""138 return self._start_patch(self.mock_module.patch.dict, *args, **kwargs)139 def __call__(self, *args, **kwargs):140 """API to mock.patch"""141 return self._start_patch(self.mock_module.patch, *args, **kwargs)142@pytest.yield_fixture143def mocker(pytestconfig):144 """145 return an object that has the same interface to the `mock` module, but146 takes care of automatically undoing all patches after each test method.147 """148 result = MockFixture(pytestconfig)149 yield result150 result.stopall()151@pytest.fixture152def mock(mocker):153 """154 Same as "mocker", but kept only for backward compatibility.155 """...

Full Screen

Full Screen

pytest_mock.py

Source:pytest_mock.py Github

copy

Full Screen

...101 def __init__(self, patches, mocks, mock_module):102 self._patches = patches103 self._mocks = mocks104 self.mock_module = mock_module105 def _start_patch(self, mock_func, *args, **kwargs):106 """Patches something by calling the given function from the mock107 module, registering the patch to stop it later and returns the108 mock object resulting from the mock call.109 """110 p = mock_func(*args, **kwargs)111 mocked = p.start()112 self._patches.append(p)113 if hasattr(mocked, 'reset_mock'):114 self._mocks.append(mocked)115 return mocked116 def object(self, *args, **kwargs):117 """API to mock.patch.object"""118 return self._start_patch(self.mock_module.patch.object, *args, **kwargs)119 def multiple(self, *args, **kwargs):120 """API to mock.patch.multiple"""121 return self._start_patch(self.mock_module.patch.multiple, *args,122 **kwargs)123 def dict(self, *args, **kwargs):124 """API to mock.patch.dict"""125 return self._start_patch(self.mock_module.patch.dict, *args, **kwargs)126 def __call__(self, *args, **kwargs):127 """API to mock.patch"""128 return self._start_patch(self.mock_module.patch, *args, **kwargs)129@pytest.yield_fixture130def mocker(pytestconfig):131 """132 return an object that has the same interface to the `mock` module, but133 takes care of automatically undoing all patches after each test method.134 """135 result = MockFixture(pytestconfig)136 yield result137 result.stopall()138@pytest.fixture139def mock(mocker):140 """141 Same as "mocker", but kept only for backward compatibility.142 """...

Full Screen

Full Screen

conftest.py

Source:conftest.py Github

copy

Full Screen

...9@fixture10def mocker(mocker):11 def autospec_as_default(*args, **kwargs):12 kwargs.setdefault('autospec', True)13 return mocker.patch.__class__._start_patch(mocker, *args, **kwargs)14 mocker.patch.object(mocker.patch, '_start_patch', side_effect=autospec_as_default)15 return mocker16'''17def load_variable(name, cache):18 var = environ.get(name, None)19 if var is not None:20 if cache.get(name, None) != var:21 cache.set(name, var)22 return var23 var = cache.get(name, None)24 if var is None:25 raise ValueError("Variable {!r} neither in environment nor cache".format(name))26 return var27@fixture(scope="session")...

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 pytest-mock 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