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
1
vote
1 answer

Unit Test Failed : ModuleNotFound - Class inheritance from the other script in the same folder

Trying to write a unit test on a Class with a class inheritance from a Class in another script ( both in the same directory ). Patch to sys.module, verify it's in the sys.module but still getting ModuleNotFound error. Wonder if I'm doing anything…
1
vote
1 answer

Mock the fetchone() method of the Mysql Cursor class and set its return value to None

I'm trying to make a MagicMock instance of the mysql connector, but I need for the method fetchone() to return None. This is what I've done so far: with mock.patch('mysql.connector.cursor') as dbmock, \ mock.patch('mysql.connector.connect',…
1
vote
1 answer

Python Unit Test Mock for Pandas max variable

I have a function which throws an exception when the max of column A is equal to a number (say 5). I want to unittest this function to check if it throws the Exception. main.py import pandas as pd class DuplicateRunError(Exception): def…
1
vote
3 answers

Python mock: replace function with another function if sys.argv argument is set

I have a snippet of code that looks like this: if args.module == 'omega': with mock.patch.object(quantum_entangler, 'sort_by_dirac', sort_by_heisenberg): quantum_entangler.main(atom_no) else: quantum_entangler.main(atom_no) Which…
1
vote
0 answers

Why won't this unittest patch work in the other method

My unittest patch isn't working for a method I wrote that is called in another method that I'm currently trying to unit test. I'm not sure why the patch isn't working but I think it is because the method I'm trying to patch is in the same file as…
1
vote
0 answers

Mocking sparkSession.table in Python

I have a function that is in main_file.py def transform_table(table_name, start, end): return sparkSession.table(table_name).filter(column.isBetween(start,end)) I wonder if it's possible to mock the sparkSession.table function call. This is what…
user3746406
  • 81
  • 10
1
vote
0 answers

How to add args for mocked functions with MagicMock

I'm using MagicMock from unittest to mock an object in a class for unit tests. I am able to use the return_value to set return values for function calls but want to make it dynamic so that the response changes based on the args passed. Here's the…
Nik
  • 11
  • 1
1
vote
1 answer

Monkeypatching `__call__` on MagicMock

Let's say I define a helper method to monkeypatch a simple modification into the __call__ behavior of an existing object: def and_print_on_call(instance): class AndPrintOnCall(type(instance)): def __call__(self, *args, **kwarg): …
tlayton
  • 13
  • 3
1
vote
0 answers

How to serialize a Mock object?

In my unit-tests there are some (nested) unittest.mock.Mock objects. At some point, these Mock objects need to be serialized using json.dumps. As expected, this raises a TypeError: Object of type Mock is not JSON serializable There are many…
djvg
  • 11,722
  • 5
  • 72
  • 103
1
vote
1 answer

Why is an autospecced mock of Enum not iterable (has no __iter__ method)?

Why is an autospecced mock of Enum not iterable (has no __iter__ method)? from unittest.mock import create_autospec from enum import IntEnum class IE(IntEnum): Q = 1 W = 2 m = create_autospec(spec=IE, instance=False) print(hasattr(IE,…
salius
  • 918
  • 1
  • 14
  • 30
1
vote
0 answers

How do I use a pytest fixture to mock a child class's inherited methods with classes as properties while maintaining the API contract using autospec?

How it started I'm testing a class, ClassToTest, that makes API calls using atlassian-python-api. The tests are going to ensure that ClassToTest performs correctly with the data it gets back from the API. Many of the atlassian-python-api API calls…
1
vote
0 answers

Can one write an assertion on `self` with `patch.object`?

Say I patch a member function of a class with unittest.mock.patch.object Can one write an assertion on another member variable of the object via self at the time of the mock's call? Consider the following example from unittest.mock import…
Hakan Baba
  • 1,897
  • 4
  • 21
  • 37
1
vote
2 answers

Mocking class attribute that is a class imported from another module unittest python

I have following project structure: ├───pa │ |───__init__.py | |─── a1.py | |─── test_a1.py | ├───pb │ |───__init__.py | |─── b1.py With following code: b1.py: class B1: def __init__(self): self.text = "Unmocked_B1" def…
1
vote
0 answers

How to unit-test a infinite while loop in python

class A: def exe(self): x= threading.Event() y= threading.Thread(name=A, target=self.infi, args=(x,), daemon=True) y.start() def infi(self,x): while True: ...... start=True if start: …
Sanjay
  • 11
  • 2
1
vote
0 answers

My AWS lambda in python. It downloads a file from S3 and reads that file. Now I need to write a unittest mock test for that

I have an AWS lambda written in python. The lambda downloads a file from S3 to the folder /tmp/records. Then the lambda reads that file. Now I need to write a unit test for that. I need to mock the S3 call. I am wondering how to do that. Here is my…