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
0 answers

python unit test with mock

Following is the pseudo code I was about to do a python unit test: def my_method(): run asynchronous_job, which will write to output_file when it's done while True: if output_file was written (by asynchronous_job): break …
Hailiang Zhang
  • 17,604
  • 23
  • 71
  • 117
0
votes
0 answers

How does Python mock class works?

This is a example I have a class : engine/a.py from thirdlib import TaskClient class TaskWorker(object): def __init__(self): self.task_client = TaskClient(xxx, xxx, xxx, xxx) def some_task(id, app): tasks =…
bryantism
  • 167
  • 9
0
votes
0 answers

Mocking not working as expected

I have a method that calls the set_status api of the webapp2.Response class: class RequestHandler(webapp2.RequestHandler): some_method(self): self.response.set_status(200) I am writing tests for some_method() and mocking the set_status…
Shan
  • 1,138
  • 17
  • 32
0
votes
0 answers

Patching python's logging handlers

I'm writing unit tests to make sure that my log messages for syslog are limited in size. In particular, my Formatter is set like this: import logging from logging.handlers import SysLogHandler [...] default_fmt = logging.Formatter( …
lorenzog
  • 3,483
  • 4
  • 29
  • 50
0
votes
0 answers

Different results from running unit test with mocks in PyDev versus the command line

I have a Python function inside of a class that runs a Photoshop script with the subprocess module. def runPhotoshop(self): photoshop = r'C:\Program Files\Adobe\Adobe Photoshop CC 2014\Photoshop.exe' extendscript =…
Christopher Spears
  • 1,105
  • 15
  • 32
0
votes
1 answer

Having difficulty mocking object returned by patched function

I have an instance method on a Django form class that returns a Python object from a payment service if successful. The object has an id property, which I then persist on a Django model instance. I'm having some difficulty getting the mocked object…
Brandon Taylor
  • 33,823
  • 15
  • 104
  • 144
0
votes
1 answer

ImportError during unittest script

I'm trying to set some tests for my server. According to a friend's recommendation I wanna use unittest and mock. For this purpose I wrote some lines but when I try to execute the script I got an importError. Traceback (most recent call last): …
Samuel Góngora
  • 141
  • 1
  • 11
0
votes
1 answer

Python: How to check that del instruction called?

I'd like to test function: #foo_module.py def foo(*args, **kwargs): bar = SomeClass(*args, **kwargs) # actions with bar del bar I chose to test the mock library. My test looks like: @mock.patch('path.to.foo_module.SomeClass') def…
ikoverdyaev
  • 783
  • 1
  • 12
  • 17
0
votes
1 answer

Python mock, expected str instance MagicMock found

I am trying to use Mock with Django for two things: 1. Get a user (we have a custom backend that makes network calls that I want to avoid) 2. Mock out another method that gets a token for password resets over the network. Here is the…
Lee
  • 8,354
  • 14
  • 55
  • 90
0
votes
1 answer

How do I patch a method of a property object in python

I have a package that looks like this: lcl | | - lcl | - LCL.py | - lunabuild.py In the lunabuild module, there is a Lunabuild class. The Lunabuild class has a method Lunabuild.configure_jobs(). The LCL class imports lunabuild: from…
qkslvrwolf
  • 179
  • 2
  • 10
0
votes
1 answer

How to use mock.patch() for Django request object?

I am trying to overwrite HTTP_POST to certain string of a request object inside a view: with patch('django.core.handlers.wsgi.WSGIRequest') as request: request.META = {'HTTP_HOST': 'www.abc.com'} resp = self.client.get('/') But obviously…
James Lin
  • 25,028
  • 36
  • 133
  • 233
0
votes
1 answer

Python Mock with `from X import y`

I am trying to use Python's mock library in my unit testing but I am seeing inconsistent results depending on how I import the target that I am trying to patch. I would expect that both of these print statements should return False but it appears…
Alex Rothberg
  • 10,243
  • 13
  • 60
  • 120
0
votes
1 answer

How to use Python mocking in a unit test

Most advice on Python mocking is couched in short snippets outside of the unit test framework. This one works find, I'm trying to follow this advice, but it's not successful as soon as I embed it in a proper unit test. For example, this code which…
Russ Bateman
  • 18,333
  • 14
  • 49
  • 65
0
votes
1 answer

Error when trying to use `assert not mock.method.called`

I'm trying to assert that a method is not called using Python Mock. Unfortunately, I can't seem to get past this error: AttributeError: MockCallable instance has no attribute 'called' I'm running Python 2.7.1 and Python Mock 0.1.0 for my tests.…
Nathan Arthur
  • 8,287
  • 7
  • 55
  • 80
0
votes
1 answer

how to mock internal calls (xlwt)?

I have a test case like this: @mock.patch('xlwt.Workbook.Workbook.save') def test_Generic_send_test_email_address(self, workspace_mock): workspace_mock.return_value = None oi = OptinInvitesGeneric() oi.compute(...) …
Houman
  • 64,245
  • 87
  • 278
  • 460