Python Patch Function

Stop all active patches. Only stops patches started with

start

TEST_PREFIX В

All of the patchers can be used as class decorators. When used in this way they wrap every test method on the class. The patchers recognise methods that start with

test

as being test methods. This is the same way that the

unittest.TestLoader

finds test methods by default.

It is possible that you want to use a different prefix for your tests. You can inform the patchers of the different prefix by setting

patch.TEST_PREFIX

:

patch.TEST_PREFIX foo value 3 patch __main__.value, not three class Thing object : def foo_one self : print value def foo_two self : print value Thing. foo_one not three Thing. foo_two not three value 3

Nesting Patch Decorators В

If you want to perform multiple patches then you can simply stack up the decorators.

You can stack up multiple patch decorators using this pattern:

patch.object SomeClass, class_method patch.object SomeClass, static_method def test mock1, mock2 : assert SomeClass.static_method is mock1 assert SomeClass.class_method is mock2 SomeClass.static_method foo SomeClass.class_method bar return mock1, mock2 mock1, mock2 test mock1.assert_called_once_with foo mock2.assert_called_once_with bar

Note that the decorators are applied from the bottom upwards. This is the standard way that Python applies decorators. The order of the created mocks passed into your test function matches this order.

Like all context-managers patches can be nested using contextlib s nested function; every patching will appear in the tuple after as :

from contextlib import nested with nested patch package.module.ClassName1, patch package.module.ClassName2 as MockClass1, MockClass2 : assert package.module.ClassName1 is MockClass1 assert package.module.ClassName2 is MockClass2

Where to patch В

patch

works by temporarily changing the object that a name points to with another one. There can be many names pointing to any individual object, so for patching to work you must ensure that you patch the name used by the system under test.

The basic principle is that you patch where an object is looked up, which is not necessarily the same place as where it is defined. A couple of examples will help to clarify this.

Imagine we have a project that we want to test with the following structure:

a.py - Defines SomeClass b.py - from a import SomeClass - some_function instantiates SomeClass

Now we want to test

some_function

but we want to mock out

SomeClass

using

. The problem is that when we import module b, which we will have to do then it imports

from module a. If we use

to mock out

a.SomeClass

then it will have no effect on our test; module b already has a reference to the real

and it looks like our patching had no effect.

The key is to patch out

where it is used or where it is looked up . In this case

some_function.

Tour Start here for a quick overview of the site Help Center Detailed answers to any.

So, there is no parser written in python for unified diff files. I m asking since I ve searched for it myself for quite some time. I need it to write a tool to.

Patch as function decorator, creating the mock for you and passing it into the decorated function: This is the standard way that Python applies decorators.

How. The standard library function inspect.getsource is used to retrieve the source code of the function, the patch is applied with the commandline utility patch.

python patch function

Patching decorators

Mock is a Python mocking and testing library. It has become a de facto standard and is now included in the Python standard library.

Lately I†ve had the chance to work on a fairly large code base built upon a service-oriented architecture. In order to test each service in isolation, we make extensive use of Mock to simulate services that the code under test depends on.

During this process, we noticed that each member our team seemed to be hitting the same pitfalls, which I†m going to present in this article in the hope that it might help other developers.

Patching in the wrong place

The first thing that puzzles people is when the patch method seem to have no effect.

Let†s consider a very simple example where you have some kind of domain model object that uses a data source module to get data and return it to the client. In a real world situation, the data source module would query a database or an external service. For the sake of simplicity, here the data source just returns hard-coded values:

data_source.py def get_name : return Alice

The Person class exposes a method that fetches data from the data source:

person.py from data_source import get_name class Person object : def name self : return get_name

One might then start to write a test case that looks like this:

test_person.py from mock import patch from person import Person mock the get_name function patch data_source.get_name This won t work as expected. def test_name mock_get_name : set a return value for our mock object mock_get_name.return_value Bob person Person name person.name assert name Bob

Unfortunately, the example above doesn†t work as expected. If you run it with a testing tool such as nose, you†ll get a result similar to:

nosetests test_person.py F FAIL: test_person.test_name ---------------------------------------------------------------------- return func args, keywargs File /home/al/essais/python/test_person.py, line 13, in test_name assert name Bob AssertionError ---------------------------------------------------------------------- Ran 1 test in 0.002s FAILED failures 1

The reason why it doesn†t work is explained in the documentation in a section called Where to patch. Now go and read it if you haven†t already, I†ll wait for you right here.

The problem in the example above is that we†re changing where the name get_name points to within the data_source module. However the code we†re testing doesn†t refer to get_name within the data_source module, it refers to that name within the person module.

Our patching would have worked correctly if the code under test had referred to get_name within the data_source module like this:

person.py import data_source class Person object : def name self : return data_source.get_name

If we want to leave our application code unchanged, we†ll have to change the way we use Mock within our test so that it modifies where get_name points to within the person module:

from mock import patch from person import Person patch person.get_name def test_name mock_get_name : mock_get_name.return_value Bob person Person name person.name assert name Bob

Forgetting to patch the return_value

Another common issue is patching an object instead of patching the return value of that object. It is particularly easy to get trapped into this when patching objects deep down inside a chain of class instantiations and method calls.

Let†s say Person has a relationship with Pet:

class Person object : def __init__ self : self.pet Pet other methods class Pet object : def noise self : return Woof

It might look natural to write a test like this:

patch person.Pet def test_dog_noise mock_pet : mock_pet.noise.return_value Meoow person Person assert person.pet.noise Meoow

This won†t work because a pet object is the result of instantiating the Pet class, which in Python means calling the class. We must therefore access the noise method via the return_value of the mocked Pet class:

patch person.Pet def test_dog_noise mock_pet : Here we need an extra return_value attribute in order to access the instance of the class mock_pet.return_value.noise.return_value Meoow person Person assert person.pet.noise Meoow

Patching decorators might also lead to some surprising behaviors. Let†s say we have a very simple decorator that just logs the result of decorated functions:

decorators.py def noise_logger func : def wrapped self : result func self In a real-world scenario, the decorator would access an external resource which we don t want our tests to depend on, such as a caching service. print Pet made noise: , result return result return wrapped

Our domain model makes use of that decorator:

person.py from decorators import noise_logger class Person object : def __init__ self : self.pet Pet class Pet object : noise_logger def noise self : return Woof

Now we†d like to patch our decorator to make sure it doesn†t call any external service we want to isolate from. Intuitively, we might write a test like this:

from mock import patch from person import Person patch person.noise_logger, lambda x: x def test_decorator : person Person assert person.pet.noise Woof

Unfortunately this won†t work at all because by the time we patch our decorator, it has already been applied and our noise method has already been wrapped. Here is what happens if we run our test with the -s switch, which tells nose to print stdout output:

nosetests -s test_person.py Pet made noise: Woof. ---------------------------------------------------------------------- Ran 1 test in 0.001s OK

This is because the decorator has been applied when the class definition was loaded, as soon as we imported the person module. Remember that instructions within a class definition are typically interpreted when we load the module containing the class. Consider this:

def useless_decorator func : print Hi, I m a decorator that does nothing. return func class Foo object : print Entering Foo class definition useless_decorator def bar self : return 42 print OK, we re done with that class definition.

If you execute this code with your Python interpreter, you should get this output:

Entering Foo class definition Hi, I m a decorator that does nothing. OK, we re done with that class definition.

I hope this silly example convinces you that decorators are applied when the class is created, which happens straight away when we load the module containing the class.

To deal with this you first have to make sure that your decorator is defined in a module separate from the class, otherwise you†ll never get a chance to replace it with a mock before the class definition calls it. Then you need to write your test code so that it patches the decorator before it gets applied to the methods of your class:

from mock import patch patch decorators.noise_logger, lambda x: x. start from person import Person def test_decorator : person Person assert person.pet.noise Woof

Now if you run nosetests with -s, you shouldn†t see any message from the decorator because it†s been replaced by a lambda that does nothing.

Copyright В 2010-2016 Alex Marandon

Site powered by nanoc.

Mock is now part of the Python standard library, available as unittest.mock in Python 3.3 onwards. This package contains a rolling backport of the standard library.

Os.path.basename path Return the base name of pathname path. This is the second element of the pair returned by passing path to the function split.

11.2. os.path Common pathname manipulations This module implements some useful functions on pathnames. To read or write files see open, and for accessing.

python patch function