Questions tagged [python-mock]

A test utility that patches objects with user-defined code or data.

Mock is a Python tool for unit testing. It can patch some functions or objects with mock code with some specified properties like return values. It records and provides information on how the mock object is called.

470 questions
0
votes
1 answer

mock global object in method namespace

Is it possible to mock a global object, such as an imported module, in a specific method from a different module? Example: import some_module class MyClass(): def a_method(self): some_module.do_something(1) def b_method(self): …
Jonathan Livni
  • 101,334
  • 104
  • 266
  • 359
0
votes
0 answers

How to mock two functions that are called from inside try/except block?

I have a function foo which is something like this: class SomeClass(object): def foo(self, url): try: r = requests.get(url) buffer = StringIO.StringIO(r.content) except Exception as e: pass I…
Rafay
  • 6,108
  • 11
  • 51
  • 71
0
votes
1 answer

mocking a return of class object method in python

I would like to mock a method to return a test value in one of my mocked classes. The issue I am running into is rather then returning the test value, the mocked method returns an object of type MagicMock. The code is pretty contrived but…
IUnknown
  • 2,596
  • 4
  • 35
  • 52
0
votes
1 answer

How to mock chained methods on Django models

I am trying to do something like this to mock methods on a Django model using the Python mock library: # file: tasks.py def delete_ads(user): # works fine and return a list of 4 MagicMock objects ads = Classifieds.objects.filter( …
Rafay
  • 6,108
  • 11
  • 51
  • 71
0
votes
1 answer

How can you override a function with a lambda using python mock?

I have some code that uses Greenlet.spawn to call some code a little later. I just found out there was an exception that is getting raised in that code. It would have been caught by our tests but the spawn makes it run after the tests succeed. I'm…
jasongregori
  • 11,451
  • 5
  • 43
  • 41
0
votes
1 answer

How can I patch a function stored in a variable?

How can I make the following test to work? mymodule.py import requests http_methods = { "GET": requests.get, "POST": requests.post, "PUT": requests.put, "DELETE": requests.delete } def foo(method): r =…
borges
  • 3,627
  • 5
  • 29
  • 44
-1
votes
1 answer

Mocker.patch a function when unit testing a Flask blueprint

I've got a blueprint file /views/index.py: from flask import Blueprint, render_template index = Blueprint('index', __name__) def auth(): return "dog" @index.route('/') def index_view(): return render_template( 'index.html',…
penitent_tangent
  • 762
  • 2
  • 8
  • 18
-1
votes
1 answer

How to write a pytest or unittest on my code?

I am new to unit testing. I am trying to test if function is called and if the schedule time is correct on the below. The python filename is #schedule.py def screenshot(): ob=Screenshot_Clipping.Screenshot() driver.get(url) …
-1
votes
1 answer

How to check if mock has any side effects left?

In python, is it possible to check if there are any and how many side effects are remaining? For example: mocked_foo = patch.object(Foo, 'method', side_effects=[1, 2, 3])
drum
  • 5,416
  • 7
  • 57
  • 91
-1
votes
1 answer

How do you mock a "from ... import" in Python unittest?

My function uses a non-existent library: from a.b import c def func(): d = c() How do I mock c? So far I tried these methods, but not sure why it doesn't work: mock_a = Mock(name="a") mock_a.b = Mock(name="b") mock_d = Mock(name="d") mock_a.b.c…
JobHunter69
  • 1,706
  • 5
  • 25
  • 49
-1
votes
1 answer

Mock global function call while importing

Suppose I have a file called a.py with code like import mod1 mod1.a() def b(): print("hi") Now if I want to mock fun b() then unittest.py while have import statement at top like from a import b at time of import mod1.a() will be called. How can…
shivams
  • 2,597
  • 6
  • 25
  • 47
-1
votes
1 answer

How to patch a function imported from another module

Not able to patch the function. I am trying to patch a function from another module in my test function. But I am not able to do that. response.py import boto3 lambda = boto3.client('lambda') def response(event,context): s =…
Stan11
  • 274
  • 3
  • 11
-1
votes
1 answer

python mock different instances

I would like to know the difference between pg.connect and pg.connect() statements. pg is a psycopg2 mock object and when I display the dir, it shows connect as its method. class TestMock(unittest.TestCase): def setUp(self): pass …
user1050619
  • 19,822
  • 85
  • 237
  • 413
-1
votes
1 answer

Assertion not executing in Python Mock

I'm trying to implement some unit testing on some code I am building but I'm seeing this weird behavior where even though I set the return value of a function call to be False, the related code does not execute and thus the assertion…
1up
  • 81
  • 7
-1
votes
1 answer

How to test a function call using Python and unittest library

I just had a question on how you write a test for the following function? Here is my test for the part that is covered, but I am not sure how I would change the test to cover the print statement and calling the get_employee_name function again. Any…
Brian
  • 421
  • 3
  • 20
1 2 3
31
32