Questions tagged [python-unittest.mock]

mock object library (part of the Python Standard Library)

unittest.mock allows you to replace parts of your system under test with mock objects and make assertions about how they have been used.

It provides a core Mock class removing the need to create a host of stubs throughout your test suite.

330 questions
3
votes
2 answers

Child Class from MagicMock object has weird spec='str' and can't use or mock methods of the class

When a class is created deriving from a MagicMock() object it has an unwanted spec='str'. Does anyone know why this happens? Does anyone know any operations that could be done to the MagicMock() object in this case such that it doesn't have the…
3
votes
0 answers

Mocking disk-out-of-space in python unittests

I'm trying to write a unittest to test the behaviour of a function when the disk is full. I need file access functions to behave normally while most of the test runs, so that the file I'm creating is actually created, then at one point I need the…
3
votes
0 answers

Python unittest returns MagicMock object instead of return value

I have a python which has a class A. Class A object has a method that takes a list as input and sends a post request to a client endpoint to create new sources in the database and returns a tuple of successful and unsuccessful lists class A(): def…
3
votes
1 answer

Mock.assert_has_calls() not working as expected

I have this unit test where I want to ensure my Django custom management job is writing specific messages to its log. from unittest.mock import Mock, patch ... …
Red Cricket
  • 9,762
  • 21
  • 81
  • 166
3
votes
1 answer

Python unittest.mock google storage - how to achieve exceptions.NotFound as side effect

I've read a few tutorials on mocking in Python, but I'm still struggling :-/ For example, I have a function wrapping a call to google storage to write a blob. I'd like to mock the google.storage.Client().bucket(bucket_name) method to return an…
3
votes
0 answers

How to mock celery send_task function in django Unit test

I want to write a unit test in Django for a function which has a celery send_task function. How do I write its UT in Django and mock celery? def push_to_queue(self, data): """ function to add task in the backend queue Args: …
3
votes
1 answer

How do I attach the return value of mock.patch(...).start() to a parent mock?

The mock examples list this example: >>> manager = MagicMock() >>> with patch('mymodule.Class1') as MockClass1: ... with patch('mymodule.Class2') as MockClass2: ... manager.attach_mock(MockClass1, 'MockClass1') ... …
2
votes
1 answer

How to fool issubclass checks with a MagicMock?

I have something like this: from unittest.mock import MagicMock class A: pass class B(A): pass mock_B = MagicMock(spec_set=B) assert issubclass(mock_B, A) # TypeError: issubclass() arg 1 must be a class How can I get this to…
Intrastellar Explorer
  • 3,005
  • 9
  • 52
  • 119
2
votes
2 answers

Mock class in Python with decorator patch

I would like to patch a class in Python in unit testing. The main code is this (mymath.py): class MyMath: def my_add(self, a, b): return a + b def add_three_and_two(): my_math = MyMath() return my_math.my_add(3, 2) The test…
2
votes
1 answer

When testing code with pyspark dataframe: how to mock up .repartition() chained function?

I have code using pyspark library and I want to test it with pytest However, I want to mock up .repartition() method on dataframes when running tests Suppose that code I want to test is a pyspark chained function like below def transform(df:…
2
votes
1 answer

Python unit test case for mocking directory and generating test files

Can we mock test directory and couple of files in python unit test case? scan.py: import re from pathlib import Path class Scan(): def scan_files(self, dir_path, filter_regex=None): for item in Path(dir_path).iterdir(): if…
2
votes
0 answers

mock assert_called_with treat argument as unordered list

I have some mocked function and I try to test that the arguments I pass there are correct. One of the arguments is a list generated based on DB queryset, lets say [{"id": 1}, {"id": 13}] The function doesn't care about the order of dicts in a list,…
Bohdan
  • 424
  • 1
  • 4
  • 15
2
votes
1 answer

Mock a function with parameters that is indirectly called with a mock function instead

I have been trying to test a function that calls another function with some parameters. I am trying to mock the latest so that it won't actually run and instead executes a mock function that returns some mock values. What I have -simplified- looks…
2
votes
1 answer

Python unittest Mock an object to not have an attribute

I have a function that runs some code if the object has a certain attribute, and in rare cases if if the object does not have the attribute, it runs different code. It is hard for me to create the object without the attribute for testing. I tried…
run_the_race
  • 1,344
  • 2
  • 36
  • 62
2
votes
1 answer

side_effect function of PropertyMock gets called only once

I have two questions regarding the mocking of a property with unittest.mock.PropertyMock (code will follow below): Why does the test test_prop output "text_0, text_0" and not "text_0, text_1" as the test test_func? The output indicates, that the…
user2606240
  • 641
  • 1
  • 6
  • 20