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

Mocking Google PubSub instance for testing publish and subscribe method

I have a module 'initialize' which returns a pub sub instance based on users input currently returns 2(Google PubSub and RabbitMQ pub sub). def create_pubsub( required input parameters ) -> VAPubSub: if env == "gcp": return…
0
votes
0 answers

Python untitest mock : side_effect function not getting invoked

I have an application that uses python-redis. I'm trying to replace the redis functions with a mock. app.py: import redis class MyApp: def readData(self): db = redis.Redis(decode_responses=True) return db.get('myKey') This is…
m.divya.mohan
  • 2,261
  • 4
  • 24
  • 34
0
votes
0 answers

Python mock.patch inside context manager is not restoring class after exiting scope

I have several interacting classes. In a test file for one of these classes, I first patch an object inside a context manager, change some of its returns, and run a test. This causes a different test, outside that context manager, to fail, and fail…
0
votes
1 answer

What's the difference beween using @patch above the test function or with patch within the test function?

What's the difference between using @patch above the testing function or with patch within the testing function (example below). For context, I'm testing a function where patching using a with statement works as expected, but the @patch…
Roma V
  • 31
  • 2
0
votes
1 answer

Constrict a complex MagicMock in Python from code with nested calls

I would like to build a MagicMock for a nested call. Not quite sure how to do this. Please could you advise? Many Thanks Here is my code : def kms_aliases(ids_include): client = boto3.client('kms') paginator =…
Banoona
  • 1,470
  • 3
  • 18
  • 32
0
votes
0 answers

Replacing functions during testing

I'm trying to write test functions for a project I'm working on. I using oracle as my database in the project and in the testing I'm using the in memory sqlite3 my problem is that some queries syntax are different. This isn't a problem when I'm unit…
0
votes
0 answers

How to use python's unittest to 1) generate a csv file from the main code to be tested and 2) test that the csv file was generated

I am learning to use Python's unittest (first time) and having a bit of trouble. I have code that queries a database, sorts the data, and outputs the sorted data to a CSV file. It works just fine. However, when I try to generate the CSV from the…
0
votes
1 answer

How to patch Boto3.resource DynamoDb Table within a pytest test method

I'm trying to use this answer as an example to test that a dynamo table is not written to. Because I am using moto to test my reads & writes, I have to do the import inside the test case. I cannot get patch to take, keep getting SSL errors I've…
MikeF
  • 764
  • 9
  • 26
0
votes
0 answers

Problem with unittest mock and object from another module

I'm currently developing some code in our repository which goes thru test and sonarqube. Said code connects to a postgres database but in the test instance doesn't connect, problem that from other teams was solved with mocking. So I'm trying to mock…
0
votes
0 answers

How can we properly mock/stub async methods of a mocked class?

I want to mock/stub the async methods of the Connect class returned by websockets.client.connect; such as send and recv. I succeed in testing the instantiation of the class, but I can't seem to setup the methods? The complete code is in this gist,…
0
votes
0 answers

The return type must be a string, dict, tuple, Response instance, or WSGI callable, but it was a MagicMock

I am writing a unittest for a flask app route that uses a class method. I imported the class into the flask app route file. i wrote the unittest code below and am getting error The view function did not return a valid response. The return type must…
obi
  • 1
  • 2
0
votes
0 answers

Patch works on individual pytest file, does not call code under test as test suite,

I've seen the documentation and other questions, but I am having trouble. my code file: index.py from paho.mqtt,client import Client mqtt_connected = False mqtt_client = Client("my-client") TOPIC = "topic" def _send_message(msg, client): …
MikeF
  • 764
  • 9
  • 26
0
votes
0 answers

Module import for unit test is not linking

I have a Python AWS Lambda function located in a directory structure like this: |-lambda | |-lambda_function.py | |-tests | |-test.py …
0
votes
0 answers

Mocking Partially Response with Unittest

I would like to mock partially the class requests.Response on Python. Like this: from unittest.mock import patch @patch("requests.Response.status_code") @patch("requests.Response.content") def test_my_method(self, mock_status_code, mock_content): …
HouKaide
  • 127
  • 1
  • 7
0
votes
1 answer

Patching child TestCase class affects parent TestCase class

I need to test a function which uses feature toggles to turn some functionality on and off. Say, a function like this: def func_to_test(hello_value: str): if toggle_is_active('only_hello_and_hi'): if hello_value not in ('hello', 'hi'): …
Artem Ilin
  • 353
  • 2
  • 19