python class method mocking failure
NickName:LuckyStarr Ask DateTime:2015-05-07T13:10:59

python class method mocking failure

Trying to understand mocking/patching and I have a restful API project with three files (FYI, I'm using flask)

  1. class1.py
  2. domain.py
  3. test_domain.py

class1.py file content:

class one:
    def addition(self):
       return 4+5

domain.py file content:

from class1 import one

class DomainClass(Resource):

    def post(self):

        test1 = one()
        val = test1.addition()

        return {'test' : val } 

test_domain.py file content:

import my_app
from flask_api import status
from mock import patch

app = my_app.app.test_client()

def test_post():
    with patch('domain.one') as mock:
        instance = mock.return_value
        instance.addition.return_value = 'yello'

    url = '/domain'
    response = app.post(url)
    print response.data

    assert status.HTTP_200_OK == response.status_code
    assert mock.called

For my test_domain.py file, I've also tried this...

@patch('domain.one')
def test_post(mock_domain):
    mock_domain.addition.return_value = 1

    url = '/domain'
    response = app.post(url)
    print response.data

    assert status.HTTP_200_OK == response.status_code

My assert for the status of 200 passes, however, the problem is that I'm not able to mock or patch the addition method to give me value of 1 in place of 9 (4+5). I also tried doing 'assert mock.called' and it failes as well. I know I should be mocking/patching where the 'one()' method is used, i.e. in domain.py not in class1.py. But I tried even mocking class1.one in place of domain.one and I still kept getting 9 and not 1. What am I doing wrong ?

******** Update I've another dilemma on the same issue, I tried doing this in the test_domain file instead of patching....

from common.class1 import one
def test_post():
    one.addition = MagicMock(return_value=40)

    url = '/domain'
    response = app.post(url)
    print response.data

    assert status.HTTP_200_OK == response.status_code

Question

  1. In update above, I did not do a mock at the place where it is used (i.e.: domain.one.addition = MagicMock(...) and it still worked !!!! It seems it may be doing a global change. Why did this work ?

  2. In the above example, 'one' is a class in the module class1.py. If I change this class 'one' to a function in class1.py, mocking does not work. It seems this function 'one' residing in module class1.py can not be mocked like this...one.return_value = 'xyz', why? Can it be mocked globally ?

Copyright Notice:Content Author:「LuckyStarr」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/30092414/python-class-method-mocking-failure

More about “python class method mocking failure” related questions

python class method mocking failure

Trying to understand mocking/patching and I have a restful API project with three files (FYI, I'm using flask) class1.py domain.py test_domain.py class1.py file content: class one: def addit...

Show Detail

Mocking a class method and changing some object attributes in Python

I am new to mock in Python. I want to know how to replace (mock) a class method while testing with another one, knowing that the original just changes some attributes of self without returning any ...

Show Detail

python mocking check if a method of an object was accessed(not called)

class A(): def tmp(self): print("hi") def b(a): a.tmp # note that a.tmp() is not being called. In the project I am working on, a.tmp is being passed as a lambda to a spark executor...

Show Detail

Mocking a method in a class dynamically

I am pretty new to Mockito and Spring. I am trying to mock many methods of many classes. I want to create a functionality where bean name and method name can given as input as string, and it will t...

Show Detail

mocking an instance method of inner object in python 3.4

I have a class in my code that makes use of a third party library. I would like to mock the instance method of the object that I obtain by calling an instance method of the library class. I am not ...

Show Detail

Mocking boolean __bool__ protocol method in Python 2.7

As part of my unittests, I am testing some responses using the fantastic Requests library. Something cool you can do with this library is test a response for a 200 OK status code by simply calling:...

Show Detail

newbiee query: mocking in python

I'm trying to learn Python and mocking infrastructure in Python at the same time (Due to requirement at my work place). I should also mention that I'm also not familiar with mocking feature in C++ ...

Show Detail

mocking react class method with intl injected

I have a react class with intl injected like below import React from 'react'; import { injectIntl } from 'react-intl'; class CanvasView extends React.Component { constructor(props){ super(p...

Show Detail

Mocking a simple method inside Service class in Grails

i was trying to mock a method (passwordComplexityCheck) which is inside UserService class. Below is what i did for mocking in my test class def userService controller.userService=new UserService()

Show Detail

Mocking grpc response message in python

Recently I have been getting my hand dirty to mock gRPC calls in python using MagicMock() but I have been not successful in mocking gRPC response message which I have got. Here is the piece of code

Show Detail