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

How to uniitest logging messages in python3?

Below is my test code. In it i planed to write multiple test cases. from unittest import TestCase, main, mock ... from src import method class TestIndexingService(TestCase): def test_start_indexing(self): valid_files=[...] invalid_files =…
ahkam
  • 607
  • 7
  • 24
1
vote
1 answer

How to mock a custom exception in python3?

I'm doing some unit test works in python using unittest module. When i try to unittest for the custom exception, it seems like it's not working. Below is my code # src.py from exceptions import ClusterException, IndexingException from utils import…
ahkam
  • 607
  • 7
  • 24
1
vote
1 answer

Python Mock Patch Two Functions that are similar

Let's say I have a function that has two similar function calls: def foo(): test = one_func("SELECT * from Users;") test1 = one_func("SELECT * from Addresses;") return test, test1 How do I patch each of these function contents? Here's my…
1
vote
0 answers

Python unittest mock patch.multiple decorator problem

I need to mock 2 external API responses in 1 unit test. I simulate the responses from a paginated API call. I am trying to make it with unittest mock patch.multiple decorator. But patch always failed. How should I use patch.multiple? my unit test…
1
vote
0 answers

django test method mock persists with call_command

SSCCE example here: first_app/tests/test_file.py from unittest.mock import patch from django.core.management import call_command from django.test import TestCase class TestWierdo(TestCase): @patch('first_app.class_a.method_to_import') …
zcahfg2
  • 861
  • 1
  • 12
  • 27
1
vote
1 answer

magic_mock_db.connect.assert_called() fails even though it's called

I am using vscode-python's extension with python's unittest to test if peewee successfully connects to the database. This is the class that is being tested: import logging from datetime import (datetime, timezone) # 3rd party libraries import…
1
vote
0 answers

How to avoid lint error when setting global variables for python unittest

I have a lint error "Module level import not at top of file" which I'm not sure how to treat corretly and in the most elegant manner. I have a python module which I want to test. This module can run in simulation mode or real mode, the mode is set…
1
vote
0 answers

Python how to patch entire object with patch.object

Python's unittest.mock built-in library provides patch.object, which let's you: patch the named member (attribute) on an object (target) with a mock object. With regular patch, you can easily replace an entire object with a MagicMock. Is it…
1
vote
0 answers

Why can't I patch multiprocessing.Process?

So I have these files: transmitter.py import multiprocessing as MP from common import BaseWorker class TxWorker(BaseWorker): ... code ... common.py import multiprocessing as MP class BaseWorker(MP.Process): ... code ... Now I have this…
1
vote
0 answers

python - mock module AND the method

I'm writing a unit test for aws serverless using lambda layers. I need to run test in a CI/CD. This means, the layer - utils.py module is NOT available during the test. I need to: mock MODULE mock METHOD calls on module level mock METHOD calls on…
1
vote
1 answer

How to mock a context manager and replace one or more of its methods

I am trying to mock a class which is used as a context manager that makes network calls. Specifically, it's Read method returns the value of a PLC's tag over the network. Here is an example of the function I am trying to test in main.py: from…
cstrutton
  • 5,667
  • 3
  • 25
  • 32
1
vote
1 answer

how to programmatically change a variable in a python package to run test

I have a project structure something like this. I want to write a test which programmatically change version variable in myPackage/version.py and write a test in tests/unit/test_release.py. ├── myPackage │   ├── __init__.py │   ├── root.py │   └──…
1
vote
1 answer

Mock not overriding the return of a function in Python

I am implementing unit test on one of the classes of my project. The method that I want to test is queryCfsNoteVariations: class PdfRaportDaoImpl: def queryCfsNoteVariations(self, reportId): sql = """ select v.* from…
unitSphere
  • 241
  • 3
  • 17
1
vote
1 answer

unittest.mock: Set custom attribute (variable) on specced mock object

I am trying to mock an existing object from another library's class for unit tests with pytest. However, the attributes (not methods) from the other library are mostly set during runtime. What I want to achieve Get all the benefits of mocking the…
1
vote
1 answer

Mock a function passed as default parameter

I have a python file (a.py) which defines a function, and a class which uses it as a default parameter in it's init method and initializes another imported class. This is my a.py import OtherClass def useful_default_func(): //do something…
saurbh
  • 431
  • 5
  • 13