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

How to mock an import within an import

So I am trying to test an api lookup with my mock data. I am testing a method within transform.py that imports a module lookup import lookup col = lookup.ColFinder() url = "xyz" if col.is_present(url): do this lookup.py import secdata class…
Shivangi Singh
  • 1,001
  • 2
  • 11
  • 20
0
votes
0 answers

Mocking output of generator called inside a function in Python

We are unit testing an Orchestrator in Azure durable function in Python where we want to assert that records created by context.call_activity("GetRecords", reader_activity_input) are passed to context.call_activity("ProcessRecords", records) in the…
Harmeet
  • 193
  • 2
  • 9
0
votes
1 answer

How to mock a Python system attribute like os.pathsep?

I am trying to modify a test to handle both Linux and Windows. The code being tested uses os.pathsep. Example: def path_split(pth): if os.pathsep in pth: return pth.split(os.pathsep) else: return [pth] If I run the following…
Gord Thompson
  • 116,920
  • 32
  • 215
  • 418
0
votes
2 answers

Mock a function present inside a list in pytest

I want to mock a function present inside a list and check whether it has been called at least once. Below is a similar implementation I tried:- In fun_list.py (funA and funB are two functions in other_module) import other_module FUN_LIST = [ …
0
votes
1 answer

Mocking a Python object without method calls

Imagine a class like so: class Foo(): def method_1(self): bar = Bar() bazz = Bazz(bar) return bazz.method_2() For unit testing, how can we mock the Bar object when we never call any methods on it, we're just passing it…
Boon
  • 1,073
  • 1
  • 16
  • 42
0
votes
1 answer

How to mock subsequent function calls in python?

I'm new to testing and testing in python. I have a python class that looks like this : File name : my_hive.py from pyhive import hive class Hive: def __init__(self, hive_ip): self.cursor = hive.connect(hive_ip).cursor() def…
Russ
  • 5
  • 1
  • 4
0
votes
1 answer

Python mock patch local function

I want to check if a local function (declared on the test itself) is called. For example: def test_handle_action(): action = "test" user = "uid" room = "room" content = "test" data = {} def test_this(user, room, content,…
Rodrigo
  • 135
  • 4
  • 45
  • 107
0
votes
1 answer

How can I check the logging message and the method called my unittests?

I'm using Django 1.3 and need to check the output and number of interactions in my logging system. For logging I'm using Django-Sentry though it appears that it's working just like the regular Python logger. I'm using python-mockito for mocking and…
Kit Sunde
  • 35,972
  • 25
  • 125
  • 179
0
votes
2 answers

mock method with exact arguments in python

In Python 3+ def some_method(): href = browser.find_element_by_css_selector('div').get_attribute('href') title = browser.find_element_by_css_selector('div > a').get_attribute('title') text = browser.find_element_by_css_selector('div > p >…
0
votes
1 answer

freeze_time doesn't work inside invoked functions

# test.py from freezegun import freeze_time from actual import DummyClass class DummyClassMock(DummyClass): def some_function(self): # does something class TestClass(): def setUp(self): self.dummy = DummyClassMock() …
mounika
  • 39
  • 8
0
votes
1 answer

Mocking a database in python unittest

I am trying to mock the value returned by get_count function in unittest mock by mocking sql connection. But I got this error. AssertionError: 12939 !=
0
votes
2 answers

Mock a class imported in a different module

Let's say I have those modules: a.py class C: ... b.py from a import C ... def foo(): c = C() ... ... now I want to write a test for module b test_b.py import unittest import mockito from b import foo class TestB(unittest.TestCase): …
Vito De Tullio
  • 2,332
  • 3
  • 33
  • 52
0
votes
1 answer

patch does not work with different import statement

My questions is related to the following code snippets. In the first one, I am importing time.sleep with the "from ... import ..."-style: from time import sleep class Tests( unittest.TestCase ): def test_sleep( self ): with patch(…
Blackbriar
  • 477
  • 4
  • 14
0
votes
1 answer

With unittest.mock, how do you mock a method of a member field?

I'm using Python 3.8. I have a class with a member field ... class AbcServiceBus: def __init__(self, ...): ... …
Dave
  • 15,639
  • 133
  • 442
  • 830
0
votes
1 answer

Django-channels, how to use mocks

I build this chat website, and I'd like to test this functionnality: if you send a message and the user is online, send message via websocket: Tested if you send a message and the user is offline, send a push notification (it's a REST…
Arnaud Fouchet
  • 933
  • 1
  • 7
  • 14