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

unittest - Mock a class instance return is impossible?

I've been trying for days to mock the instance of a certain class. I've tried dozens of approaches... I'm starting to think that this is not possible. The code below is the only one, so far, that I've tested that can patch the SomeClassToMock class…
2
votes
0 answers

Mocking and testing the python io.BufferedWriter

I'm trying to mock the io.BufferedWrite.write method with @patch using pytest/unittest.mock. The questions arises withing the error cited bellow, saying that the write method is not mutable. That can cannot be override. For the following…
lukaswilkeer
  • 305
  • 4
  • 13
2
votes
0 answers

using both debugger and Mock results in AttributeError

I have a piece of unit tests code where at some place unittest.mock.Mock is used and I'd like to go through it step by step using debugger. I am using pdbpp, but it does not matter in this situation, standard pdb and ipdb fail on same issue. When…
Dmytro O
  • 406
  • 5
  • 11
2
votes
1 answer

How to mock the import of a module in python?

Consider the following project structure: /project - folder/ - some_config_module.py - func.py - test/ - __init__.py - test_config.py - test.py Assume the following code # func.py import folder.some_config_module as config def…
2
votes
1 answer

How to mock config variables in python3?

This is a AWS lambda function #service.py from configs import SENDER, DESTINATIONS from constants import LOG_FORMAT import logging def send_mail(body, SENDER, DESTINATIONS): ... ... In config files it's retrieving data from AWS param…
2
votes
1 answer

Async Patching issue with static methods in Python

I'm having a problem trying to patch a static method, no matter what i've tried the original method is still being ran. I'll illustrate an example below. file A import B from unittest.mock import patch from unittest import mock import…
2
votes
1 answer

How do I patch mock multiple calls from os in python?

I have a method that does the following: import os ... if not os.path.exists(dirpath): os.makedirs(dirpath) I'm trying to mock the makedirs and path.exists but when I do this with patch the mocks…
2
votes
1 answer

Mocking Thread module in python

I am trying to mock a function that uses multithreading to run another function with different parameters and saves the return results to the queue. I tried using pytest and unitest to mock it but it seems to still execute the thread when called…
2
votes
2 answers

Python unittest: Mock an external library function called from a class object

Hello I have the following code; I am trying to test the load function inside file_a; download is function in an external module I imported file_a.py from foo import download class Bar() __init__(self, arg_1): self.var = arg_1 def…
2
votes
0 answers

Python unittest fails assertion when running with discover. When running alone it passes

I try to unittest if a file gets saved correctly. The unittest works if I run it directly with python -m unittest test.test_save But if I try to run all unittests at once with python -m unittest discover every test runs perfecty except of the save…
2
votes
0 answers

Unittest mock.patch.object(autospec=True) broken for staticmethod?

I want to ensure that my Class' staticmethod is called with the correct arguments without actually calling it, therefore I am mocking it. E.g.: import unittest from unittest.mock import patch class FooStatic: @staticmethod def…
Celmar Y.
  • 301
  • 3
  • 13
2
votes
0 answers

Test Python Tkinter GUI application without displaying elements

How to test tkinter gui without displaying elements? This should make a test quicker. Code below works, but one has to close tkinter windows after runing test to make them ends, is there a possibility to test Next class without running tkinter app…
weis_ss
  • 61
  • 5
2
votes
0 answers

Mock Boto3 AWS calls where client is global

I have some code that I am attempting to write unit tests for. I have looked at other answers here e.g. here although I have the problem that the Boto3 client reference is global and outside of the function I am testing. A simplified version of the…
Alex Harvey
  • 14,494
  • 5
  • 61
  • 97
2
votes
0 answers

Extract unittest.mock call arguments agnostically w.r.t whether they have been passes as positional arguments or keyword arguments

This question is similar to check unittest.mock call arguments agnostically w.r.t. whether they have been passed as positional arguments or keyword arguments, but with an extension. Given a mocked function with a given spec, it is possible to check…
Emil Bode
  • 1,784
  • 8
  • 16
2
votes
1 answer

Mock a subset of a Python class's methods and properties

I'm using the mock Python module for performing my tests. There are times when I'm mocking a class, however I just want to mock some of its methods and properties, and not all of them. Suppose the following scenario: # module.py class SomeClass: …
Felipe Ferri
  • 3,488
  • 2
  • 33
  • 48