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

Python Mock Iterable (sort of) not working

I am creating a test module for a Flask application. In this Resource's get() method, I fetch data from Mongo and then iterate through it to produce output. I am mocking the collection's find() method to return my iterable. The problem is that…
gamda
  • 580
  • 6
  • 24
0
votes
1 answer

python 2.7 - Can't figure out how to test with mock

New to using mock. on python 2.7.13. i'm building a wrapper around this library https://github.com/sendgrid/sendgrid-python/blob/master/sendgrid/sendgrid.py which in turn consumes this library for any REST…
w--
  • 6,427
  • 12
  • 54
  • 92
0
votes
1 answer

Subclassing Mock in Python 2.7

I have a set of unit tests which will repeatedly be using a certain cooperator class Rental that I want to mock, with the same arguments passed every time. To make this easier, I want to make a subclass of mock.Mock and pass the arguments on…
0
votes
1 answer

python mock unittest: 'self' parameter lacking default value

I am trying to test a class method that calls another class method. I have seen this question, but I think this is slightly different. I tried the answer to that question, but it was not working. I continue to get this error: AssertionError:…
genomics-geek
  • 195
  • 4
  • 14
0
votes
1 answer

Is there any value I can use for method assert_called_once_with, which would match anything?

I have this function def my_function(param1, param2): ... my_other_function(param1, param2, ) ... I want to test that my_other_function is being called with param1 and param2, and I don't care about the rest I wrote a…
vlad-ardelean
  • 7,480
  • 15
  • 80
  • 124
0
votes
1 answer

Mocking many but not all functions in a class using python mock

I'm trying to test a python class. I want to mock some of the class's methods but not all of them. This can be achieved well enough with mock.patch. My problem is that: There are numerous functions to patch so this means many nested calls to…
Andrew Parker
  • 1,425
  • 2
  • 20
  • 28
0
votes
0 answers

Access to the class instance and original method when using mock.patch

I would like to use mock.patch to intercept a method in a class. Here is my current approach: class Test( object ): def foo( self, *args, **kwargs ): print( 'Test.foo: %s %s' % ( repr( self ), repr( ( args, kwargs ) ) ) ) def…
TTimo
  • 1,276
  • 1
  • 13
  • 20
0
votes
0 answers

Python: How to test the path which handles the exception?

Given this code: def main(b): try: a = 2 / b return "Hello World" except Exception: return "Exception" How to test the path which handles the exception using patching? Would it possible to make a = 2 / b as a valid…
Shravan
  • 2,553
  • 2
  • 16
  • 19
0
votes
1 answer

How to mock/replace a class with a list

Basically I'm trying to do this: class SeaLion(object): def __iter__(self): return self def next(self): ... def train(): sea_lion = SeaLion(...) for tooth in sea_lion: .. do something Then in my test file I…
Greg
  • 45,306
  • 89
  • 231
  • 297
0
votes
1 answer

Replacing / Mocking all new instances of class

I have the following situation. class Class1(): def __init__(self, param1): self.param = param1 def my_func_1(self): return "Hello " + self.param1 class Class2(): def __init__(self): self.instance_of_class_1 =…
Mr T.
  • 4,278
  • 9
  • 44
  • 61
0
votes
0 answers

Third argument to patch decorator, how does this work?

This is something I discovered and am quite please with but would to understand what is happening. The use case is I want to patch and object in a test using a decorator but I'm not really interested in the mock object. My old way of doing…
Dan
  • 1,874
  • 1
  • 16
  • 21
0
votes
0 answers

How to mock a default list in python

I have something like this: abc.py DEFAULT_PATTERN = ['abc','dfg'] def.py checkName(name): if not [pattern for pattern in DEFAULT_PATTERN if pattern in name]: print 'Hello' How will it ensure that I write a test case for my function by…
Vish
  • 73
  • 1
  • 1
  • 5
0
votes
0 answers

Python patch.object decorator can't access method of class

I have a test case below: class Foo(unittest.TestCase): @staticmethod def my_method(): ... def test_01(self): with patch.object(bar, 'method_to_be_mocked', side_effect=Foo.my_method): test_stuff() def…
Arch1tect
  • 4,128
  • 10
  • 48
  • 69
0
votes
0 answers

Python set side_effect for multiple calls to same mock

How would I set the side effect to test a function that makes multiple calls to the same function? The following shows and example, where call_it first calls subprocess.check_call is called first to check if the cmd exists, and if it exists it is…
0
votes
1 answer

how to write tests for a function using py3.5 os.scandir()?

How to write tests to a function using the newly added to build-ins of python 3.5 os.scandir()? Is there a helper to mock a DirEntry objects? Any suggestions on how to mock os.scandir() of an empty folder and a folder with few 2 files for example?
vmenezes
  • 1,076
  • 2
  • 12
  • 21