Questions tagged [magicmock]

MagicMock is a subclass of Mock with default implementations of most of the magic methods.

MagicMock is a subclass of Mock with default implementations of most of the magic methods.

You can use MagicMock without having to configure the magic methods yourself.

163 questions
159
votes
3 answers

Python returns MagicMock object instead of return_value

I have a python file a.py which contains two classes A and B. class A(object): def method_a(self): return "Class A method a" class B(object): def method_b(self): a = A() print a.method_a() I would like to unittest…
Mehdi Jafarnia Jahromi
  • 2,017
  • 3
  • 15
  • 14
30
votes
2 answers

Mock entire python class

I'm trying to make a simple test in python, but I'm not able to figure it out how to accomplish the mocking process. This is the class and def code: class FileRemoveOp(...) @apply_defaults def __init__( self, …
AnaF
  • 323
  • 1
  • 3
  • 8
29
votes
3 answers

python check if a method is called without mocking it away

class A(): def tmp(self): print("hi") def b(a): a.tmp() To check if tmp method is called in b, the recommended way is a = A() a.tmp = MagicMock() b(a) a.tmp.assert_called() But tmp here is being mocked away and is not resulting in…
claudius
  • 1,112
  • 1
  • 10
  • 23
25
votes
2 answers

mock file open in python

I'm trying to mock file open, and all of the examples show that I need to @patch('open', create=True) but I keep getting Need a valid target to patch. You supplied: 'open' I know patch needs the full dotted path of open, but I have no idea what…
TzurEl
  • 792
  • 3
  • 9
  • 15
22
votes
1 answer

Python mock patch doesn't work as expected for public method

I'm trying to patch a public method for my flask application but it doesn't seem to work. Here's my code in mrss.feed_burner def get_feed(env=os.environ): return 'something' And this is how I use it @app.route("/feed") def feed(): …
toy
  • 11,711
  • 24
  • 93
  • 176
13
votes
1 answer

Magic mock assert_called_once vs assert_called_once_with weird behaviour

I am noticing a weird behavior with assert_called_once and assert_called_once_with in python. This is my real simple test: File module/a.py from .b import B class A(object): def __init__(self): self.b = B("hi") def…
asgarro
  • 131
  • 1
  • 1
  • 8
9
votes
1 answer

Comparing integer to MagicMock not working inside unittest in python

I have a class, which use a class variable to choose which logic to execute. #in file1: class SomeHelper(): def __init__(self): self.my_var = 0 #in file2: import file1 class MyClass(): ... ... def calculate(): inst…
RebeccaK375
  • 871
  • 3
  • 17
  • 28
9
votes
3 answers

Mocking an imported function with pytest

I would like to test a email sending method I wrote. In file, format_email.py I import send_email. from cars.lib.email import send_email class CarEmails(object): def __init__(self, email_client, config): self.email_client =…
user7692855
  • 1,582
  • 5
  • 19
  • 39
9
votes
2 answers

Throw exception after first call

I have a loop where I handle adding records to a zip file. I have mocked my zipfile object and I want to raise a exception to verify that my logic for handling large zipfiles will work correctly. is there a way with MagicMocks or plain mocks to…
Nathan Tregillus
  • 6,006
  • 3
  • 52
  • 91
7
votes
1 answer

Can `MagicMock` compare with int in python3?

I am migrating python's version (2->3) of my project. The tests works fine for python2, but complains for python3, the error is like TypeError: '>' not supported between instances of 'MagicMock' and 'int' here is a minimal case # test_mock.py try: …
John Smith
  • 153
  • 1
  • 7
7
votes
2 answers

Assert mocked function called with json string in python

Writing some unit tests in python and using MagicMock to mock out a method that accepts a JSON string as input. In my unit test, I want to assert that it is called with given arguments, however I run into issues with the assert statement, since the…
Brent Hronik
  • 2,357
  • 1
  • 27
  • 43
6
votes
1 answer

How can I mock a property to raise an exception?

I'm using MagicMock with Python 2.7 to mock objects. One of the classes that I'm mocking has properties, one of which can raise a TypeError in some cases. I would like to mock that behavior, but I can't figure out how: del my_mock.my_property will…
zneak
  • 134,922
  • 42
  • 253
  • 328
6
votes
1 answer

Python Mocking db connection/unknown type in unit test

Newby to python here. My class uses a database connection to wrap some functions. I have figured out some basic examples successfully. For the more complex library that I am working with, I cannot find close examples of mocking the database…
Dave McNulla
  • 2,006
  • 16
  • 23
5
votes
1 answer

AttributeError: _mock_methods when extending MagicMock class

Is it okay to extend a MagicMock class to create mocked objects and use it in unit tests? If i don't have any code in init function the extending the class seems to work well class MockAPI(MagicMock): def __init__(self): self.x = 20 …
alegria
  • 1,290
  • 14
  • 23
5
votes
2 answers

How to get python's MagicMock to return a value the first time it is called and raise an exception the second time it is called?

I have the following python program that uses mocking. #!/usr/bin/env python import mock def my_func1(): return "Hello" my_func = mock.MagicMock() my_func.return_value = "Goodbye" print my_func() print my_func() Output: Goodbye Goodbye All…
Saqib Ali
  • 11,931
  • 41
  • 133
  • 272
1
2 3
10 11