Python fixtures.Fixture() Examples

The following are 2 code examples of fixtures.Fixture(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module fixtures , or try the search function .
Example #1
Source File: test_fixtures.py    From pypowervm with Apache License 2.0 6 votes vote down vote up
def __init__(self, fx, name, path, patch_object=False, return_value=None):
        """Create the logging patcher.

        :param fx: The fixtures.Fixture (subclass) on which to register the
                   patcher.  Must be a fixture providing a .log(msg) method.
        :param name: String name for the patcher.
        :param path: String python path of the object being mocked.
        :param patch_object: If True, the path parameter is parsed to create a
                             mock.patch.object with autospec=True instead of a
                             regular mock.patch.  For example,
                         patch='foo.bar.Baz.meth'
                             would result in
                         mock.patch.object(foo.bar.Baz, 'meth', autospec=True)
                             Note that this means the mock call will include
                             the instance through which it was invoked.
        :param return_value: The return value for the mocked method.
        """
        def _log(*a, **k):
            self.fx.log(self.name)
            return a[0] if self.ret is self.FIRST_ARG and len(a) != 0 \
                else self.ret
        # This ignores/overrides the superclass's return_value semantic.
        self.ret = return_value
        super(LoggingPatcher, self).__init__(
            fx, name, path, patch_object=patch_object, side_effect=_log) 
Example #2
Source File: test_fixtures.py    From pypowervm with Apache License 2.0 5 votes vote down vote up
def __init__(self, fx, name, path, patch_object=False, side_effect=None,
                 return_value=None):
        """Create a patcher on a given fixture.

        :param fx: The fixtures.Fixture (subclass) on which to register the
                   patcher.
        :param name: String name for the patcher.
        :param path: String python path of the object being mocked.
        :param patch_object: If True, the path parameter is parsed to create a
                             mock.patch.object with autospec=True instead of a
                             regular mock.patch.  For example,
                         patch='foo.bar.Baz.meth'
                             would result in
                         mock.patch.object(foo.bar.Baz, 'meth', autospec=True)
                             Note that this means the mock call will include
                             the instance through which it was invoked.
        :param side_effect: Side effect for the mock created by this patcher.
                            If side_effect is supplied, return_value is
                            ignored.
        :param return_value: Return value for the mock created by this patcher.
                             If side_effect is supplied, return_value is
                             ignored.
        """
        self.fx = fx
        self.name = name
        if patch_object:
            modname, klassname, methname = path.rsplit('.', 2)
            module = importlib.import_module(modname)
            klass = getattr(module, klassname)
            self.patcher = mock.patch.object(klass, methname, autospec=True)
        else:
            self.patcher = mock.patch(path)
        self.return_value = return_value
        self.side_effect = side_effect
        self.mock = None