pytest - patched method of a class does not return the mock value

2024/11/15 15:15:46

My code is fairly simple but i don't understand what is going on :

class MyDb :def some_func( arg ) :....

while my test code is :

@mock.patch(mypkg.mydb)
@pytest.mark.parametrize( func_dummy_value ) :( [ { "id" : 1234 } ] )
def test_simple ( mock_db , dummy_value ) :mock_db.some_func.return_value = dummy_value

So from where I call it I do have sth like :

db = MyDb()
print db
print db.some_func()

I was expecting the last one to print the dummy value I have given when running the test. Instead I get :

<MagicMock name='MyDb()' id='140018446236496'>
<MagicMock name='MyDb().some_func()' id='140018443991696'>

what am I missing?

Answer

some_func is a instance method, so it is called on an instance of MyDb, not the class itself. An instance is a return value from calling a class. So you need to bear that in mind when you patch.

mock_db.return_value.some_func.return_value = dummy_value
https://en.xdnf.cn/q/119000.html

Related Q&A

New instance of toplevel classes make overlapping widgets

Im generally new to python and tkinter. Ive been programming maybe about a year or so, and Ive just started to try to make each tkinter toplevel window its own class because Ive heard that its the righ…

Regex End of Line and Specific Chracters

So Im writing a Python program that reads lines of serial data, and compares them to a dictionary of line codes to figure out which specific lines are being transmitted. I am attempting to use a Regul…

Is it possible to scrape webpage without using third-party libraries in python?

I am trying to understand how beautiful soup works in python. I used beautiful soup,lxml in my past but now trying to implement one script which can read data from given webpage without any third-party…

Different model performance evaluations by statsmodels and scikit-learn

I am trying to fit a multivariable linear regression on a dataset to find out how well the model explains the data. My predictors have 120 dimensions and I have 177 samples:X.shape=(177,120), y.shape=(…

Python to search CSV file and return relevant info

I have a csv file with information about some computers on our network. Id like to be able to type from the command line a quick line to bring me back the relevant items from the csv. In the format:$…

Remove all elements matching a predicate from a list in-place

How can I remove all elements matching a predicate from a list IN-PLACE? T = TypeVar("T")def remove_if(a: list[T], predicate: Callable[[T], bool]):# TODO: Fill this in.# Test: a = [1, 2, 3, …

Python-scriptlines required to make upload-files from JSON-Call

Lacking experience & routine for Python-programming. Borrowing examples from forums have made a script which successfully makes a JSON-cal (to Domoticz) and generates & prints the derived JSON-…

python pygame mask collision [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 4 years ago.The com…

How to find max average of values by converting list of tuples to dictionary?

I want to take average for all players with same name. I wrote following code. its showing index error whats the issue?Input: l = [(Kohli, 73), (Ashwin, 33), (Kohli, 7), (Pujara, 122),(Ashwin, 90)]Out…

Cannot pass returned values from function to another function in python

My goal is to have a small program which checks if a customer is approved for a bank loan. It requires the customer to earn > 30k per year and to have atleast 2 years of experience on his/her curren…