Best Python code snippet using autotest_python
mock.py
Source:mock.py
...1641 if side_effector is not None:1642 method.side_effect = side_effector(mock)1643class MagicMixin(Base):1644 def __init__(self, /, *args, **kw):1645 self._mock_set_magics() # make magic work for kwargs in init1646 _safe_super(MagicMixin, self).__init__(*args, **kw)1647 self._mock_set_magics() # fix magic broken by upper level init1648 def _mock_set_magics(self):1649 orig_magics = _magics | _async_method_magics1650 these_magics = orig_magics1651 if getattr(self, "_mock_methods", None) is not None:1652 these_magics = orig_magics.intersection(self._mock_methods)1653 remove_magics = set()1654 remove_magics = orig_magics - these_magics1655 for entry in remove_magics:1656 if entry in type(self).__dict__:1657 # remove unneeded magic methods1658 delattr(self, entry)1659 # don't overwrite existing attributes if called a second time1660 these_magics = these_magics - set(type(self).__dict__)1661 _type = type(self)1662 for entry in these_magics:1663 setattr(_type, entry, MagicProxy(entry, self))1664class NonCallableMagicMock(MagicMixin, NonCallableMock):1665 """A version of `MagicMock` that isn't callable."""1666 def mock_add_spec(self, spec, spec_set=False):1667 """Add a spec to a mock. `spec` can either be an object or a1668 list of strings. Only attributes on the `spec` can be fetched as1669 attributes from the mock.1670 If `spec_set` is True then only attributes on the spec can be set."""1671 self._mock_add_spec(spec, spec_set)1672 self._mock_set_magics()1673class AsyncMagicMixin(MagicMixin):1674 def __init__(self, /, *args, **kw):1675 self._mock_set_magics() # make magic work for kwargs in init1676 _safe_super(AsyncMagicMixin, self).__init__(*args, **kw)1677 self._mock_set_magics() # fix magic broken by upper level init1678class MagicMock(MagicMixin, Mock):1679 """1680 MagicMock is a subclass of Mock with default implementations1681 of most of the magic methods. You can use MagicMock without having to1682 configure the magic methods yourself.1683 If you use the `spec` or `spec_set` arguments then *only* magic1684 methods that exist in the spec will be created.1685 Attributes and the return value of a `MagicMock` will also be `MagicMocks`.1686 """1687 def mock_add_spec(self, spec, spec_set=False):1688 """Add a spec to a mock. `spec` can either be an object or a1689 list of strings. Only attributes on the `spec` can be fetched as1690 attributes from the mock.1691 If `spec_set` is True then only attributes on the spec can be set."""1692 self._mock_add_spec(spec, spec_set)1693 self._mock_set_magics()1694class MagicProxy(Base):1695 def __init__(self, name, parent):1696 self.name = name1697 self.parent = parent1698 def create_mock(self):1699 entry = self.name1700 parent = self.parent1701 m = parent._get_child_mock(name=entry, _new_name=entry,1702 _new_parent=parent)1703 setattr(parent, entry, m)1704 _set_return_value(parent, m, entry)1705 return m1706 def __get__(self, obj, _type=None):1707 return self.create_mock()...
mock_open.py
Source:mock_open.py
...47 list of strings. Only attributes on the `spec` can be fetched as48 attributes from the mock.49 If `spec_set` is True then only attributes on the spec can be set."""50 self._mock_add_spec(spec, spec_set)51 self._mock_set_magics()52def mock_open(mock=None, read_data=''):53 """54 A helper function to create a mock to replace the use of `open`. It works55 for `open` called directly or used as a context manager.56 The `mock` argument is the mock object to configure. If `None` (the57 default) then a `MagicMock` will be created for you, with the API limited58 to methods or attributes available on standard file handles.59 `read_data` is a string for the `read` methoddline`, and `readlines` of the60 file handle to return. This is an empty string by default.61 """62 def _readlines_side_effect(*args, **kwargs):63 if handle.readlines.return_value is not None:64 return handle.readlines.return_value65 return list(_state[0])...
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!