Questions tagged [python-mock]

A test utility that patches objects with user-defined code or data.

Mock is a Python tool for unit testing. It can patch some functions or objects with mock code with some specified properties like return values. It records and provides information on how the mock object is called.

470 questions
0
votes
0 answers

Python Mock Instantiation of Object

Could someone please let me know how to mock the DatabaseAPI object for below unit test case example. Getting error at getURL in test_load.py file. File load.py from database_api import DatabaseAPI Database_api_client = DatabaseAPI(username,…
Veds
  • 11
  • 1
  • 6
0
votes
1 answer

How to test out if a object's method is called?

Hi I am trying to test to see if a method in object A calls another object, object B's, methods. I already have separate tests which tests B's functionality so my goal is just to see if B's method has been called. I am trying to use mocks to create…
alyacode
  • 11
  • 1
0
votes
1 answer

Error while trying to mock X509 certificate in python unit test

I have a function, the job of which is to read the private key and certificate from a shared drive. def get_private_key(): with open(config.CERTIFICATE_PATH + config.PRIVATE_KEY, 'rb') as key_file, \ open(config.CERTIFICATE_PATH +…
0
votes
1 answer

Python: Assert mock function was called with a string containing another string

Here is a simplified version of the problem I am facing: Let's say I have a function that accepts a path to a directory and then removes all of its content except (optionally) a designated "keep file", import os KEEP_FILE_CONSTANT = '.gitkeep' def…
Grant Moore
  • 153
  • 1
  • 10
0
votes
0 answers

Is there a way to mock snowflake-connector-python module and mock query that will use the mocked cursor and execute the query?

I have a class that generates snowflake connection using credentials and execute a query using the generated cursor. Now I want to write unit tests that would mock that connection and run query. Is that possible ?
0
votes
1 answer

How to easily find patch target in unittest?

I have the following module structure within a Django app: subscriptions - api - views.py - serializers.py - tests test_some_view.py - models.py - signals.py In models.py I have a GooglePlayOrder model, and in…
artem
  • 16,382
  • 34
  • 113
  • 189
0
votes
1 answer

Why can't I patch a class from a different file for my unit test?

I have a file called some.py: from dataclasses import dataclass from some_folder.some_reader import SomeReader class SomeClass: def __init__(self, url): self.reader = SomeReader(url) and some_reader.py: class SomeReader: def…
jlcv
  • 1,688
  • 5
  • 21
  • 50
0
votes
1 answer

In python-mock the original function is being invoked instead of the mock

Given the following code that attempts to configure the shell_with_tag() to be mocked by `mock_shell_with_tag(): #!/usr/bin/env python3 import pytest def shell(cmdline, debug=True): return f"Original Shell command for {cmdline}.", None def…
WestCoastProjects
  • 58,982
  • 91
  • 316
  • 560
0
votes
0 answers

How can I mock a python object that is initialized with a non-existing serial port?

I am trying to unit test a python module that interacts with an object which is initialized with a serial port. At the moment the test passes just if there is something connected to that serial port on the host computer. class…
Saverio Guzzo
  • 419
  • 5
  • 16
0
votes
1 answer

Patch: AssertionError: Expected '...' to have been called once. Called 0 times

I have the following class to be tested. mod1/file1.py @dataclass class Base(metaclass=ABCMeta): account: Account ec2_id: str = "" def start(self): self.account.session.resource("ec2").instances.filter( …
ca9163d9
  • 27,283
  • 64
  • 210
  • 413
0
votes
1 answer

Get variables used by eval

Hello I am trying to get a list of the variables used by an eval expression. I managed to get something working using the mock module: import mock def get_eval_vars(expr): # Use a mock dictionary to check which items are called mock_var =…
Iñigo Moreno
  • 529
  • 2
  • 15
0
votes
1 answer

Properly mock datastore Query

I am trying to mock the datastore Client interaction in Python. Thus I patched Query: from google.cloud.datastore import Client from unittest.mock import patch def foo(): client = Client() try: ancestor = client.key("anc", "123") …
abergmeier
  • 13,224
  • 13
  • 64
  • 120
0
votes
1 answer

Python return a mocked object from method call

I have a module which makes object of dynamoDB client. The module is external and does not belong to my code. I am trying to unit test enclosingClass class enclosingClass: def enclosing_function(): ddb_client = get_ddb_client(role) …
A007
  • 107
  • 1
  • 12
0
votes
1 answer

Patch or Mock Google Cloud Logging in Python unittests

I have a program which is eventually deployed on Google Cloud Platform and I use the Logs Explorer with GCP to monitor my logs. For that, I need to use the google.cloud.logging library, which is necessary to show logs with correct severity level on…
0
votes
1 answer

How to make asyncio cancel() to actually cancel the task?

This code is stuck in an infinite loop, the self.task.cancel() seems to have no effect: import asyncio from unittest.mock import AsyncMock, patch async def loop(): while True: await asyncio.sleep(1) class AsyncSleepMock(AsyncMock): …
Velkan
  • 7,067
  • 6
  • 43
  • 87