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

What went wrong for unittest in my project

I have an email class for which I need to write a unit test class. I have written that unittest class, but it's not working as expected. actual class: def _get_email_status(self, settings, data): # it's a dictionary email_param =…
Rhea
  • 381
  • 1
  • 7
  • 22
0
votes
2 answers

How to test if another class method is called within a method in python?

I have the following structure: ClassB(): def foo(): Class A(): def __init__(self, B): #some initialisations def method1(): #complex logic B.foo() I am trying to write a unit test method1 and would like to test if foo…
0
votes
2 answers

Cannot Seem to Patch Class & Method In Another File

I've been banging my head against the wall on a small mockup like this: Here's the tree: src ├── __init__.py ├── file_a.py ├── file_b.py test ├── test_a.py In file_a: class qaz(object): def __init__(self): print("\n\nin qaz") def…
aronchick
  • 6,786
  • 9
  • 48
  • 75
0
votes
1 answer

Iterate over Mock returned value

I'm trying to Mock method that returns a list. After setting return_value, it still return a Mock object instead of list, so I can't iterate over that object. Example of function that I want to test: def func(db_engine): query = f"SELECT * FROM…
demo
  • 421
  • 5
  • 22
0
votes
2 answers

Python unittest: Unable to mock imported functions so that conditional evaluates to False

I'm encountering a problem with unit testing in Python. Specifically, when I try to mock a function my code imports, variables assigned to the output of that function get assigned to a MagicMock object instead of the mock-function's return_value.…
0
votes
1 answer

Facing issues while using MagicMock in Python

I am new in Python. I am trying to write a UT using MagicMock as follows: db_obj = MagicMock(DBFactory.get_db_for_env) db_obj.fetch_cursor_records.return_value = self.get_success_csv() …
Joy
  • 4,197
  • 14
  • 61
  • 131
0
votes
2 answers

How to mock multiprocessing.Event.is_set() - AttributeError

I'm trying to mock a multiprocessing.Event object so that a call to event.is_set() returns False on the first iteration and true on the second iteration. I've failed at the following attempt: import unittest import unittest.mock as mock import…
David Parks
  • 30,789
  • 47
  • 185
  • 328
0
votes
1 answer

Mocking django Form cleaned_data field

I have a simple django form like this: class ContactForm(forms.Form): subject = forms.CharField(max_length=100) message = forms.CharField(widget=forms.Textarea) My view uses it like this: def my_view(request): form =…
Krimson
  • 7,386
  • 11
  • 60
  • 97
0
votes
1 answer

magic mock set value for attribute only get the first value

I am using pytest3.7 to test. I would like to mock res,which is return value from get_res_function. res.property1[key1][keyN].property2 is the value I want to mock. Here is my test: @mock.patch("mypkg.get_res_function") def test_function(mock_res): …
coder
  • 19
  • 8
0
votes
1 answer

Access multiple calls in mock_post

I have a function that fires off two post requests, e.g: def send_posts(): first_response = requests.post( url="google.com", json={'records': [{'value': message}]}, ) second_response = requests.post( …
cdm
  • 719
  • 2
  • 10
  • 22
0
votes
1 answer

How to MagicMock the function within function python

I have a module A, which contains below two functions: def my_func(): my_dict = {"key1":100, "key2": 100} send_result(dicts=my_dict) def send_result(dicts): print(dicts) I have written unit test case as below: from unittest.mock…
user15051990
  • 1,835
  • 2
  • 28
  • 42
0
votes
1 answer

Using SQLAlchemy `and_` with MagicMock

I have a SQLAlchemy call that I am trying to mock. Model.query.filter(and_(Model.id.in_(some_ids), Model.other_id != None)).all() I am using MagicMock to mock this call and give it a return value. The issue I am having is when I add the…
Kevin Welch
  • 1,488
  • 1
  • 9
  • 18
0
votes
1 answer

Unable to access mock attributes that were set while running

I running some unit tests for a method with mock objects. In the method, attributes are set, but I can't seem to access them in the unit test. When I try I get back a mock object, not the string I am trying to access Here is my unit test …
Vayelin
  • 75
  • 1
  • 2
  • 10
0
votes
1 answer

MagicMocking a pandas DataFrame causes a segfault

The following code produces a segmentation fault. import pandas as pd from mock import MagicMock df = pd.DataFrame(0, [[1, 2], [3, 4]], ['col']) df['col'] += MagicMock() Naively, I expected this would work. Is this the correct way to mock a…
RoachLord
  • 993
  • 1
  • 14
  • 28
0
votes
1 answer

Python Unit Testing - Mock Patch at Runtime?

I am writing unit tests in python. My code uses redis frequently and I want to mock this. I'd like to do this globally and not worry about mocking it in each test, but I don't think this is possible using the @patch decorator. Example of working…
micah
  • 7,596
  • 10
  • 49
  • 90