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
1 answer

Mocking a function with namespace confusion

I am trying to mock a function that should get called when calling a class method: # SomeClass.py from some_module import some_function class SomeClass: def some_method(self, *a, **kw): ... some_function() ... #…
Algebra8
  • 1,115
  • 1
  • 10
  • 23
0
votes
1 answer

mocking multi-parameter function, how to specify spec?

I believe my problem may be that I am not giving a spec parameter to the patch method. And you can see that autospec is not working either. Both the commented and un-commnented lines give the same result. And searching on "python mock parameter…
Ray Kiddy
  • 3,521
  • 3
  • 25
  • 32
0
votes
1 answer

Python Mock Test

I am trying to mock test an endpoint that gets the time and date. I have viewed several tutorials and python docs, but I am still getting stumped by the mock test. Any help is appreciated greatly from flask import Flask, redirect, url_for import…
dan
  • 21
  • 2
0
votes
1 answer

Mock MysqlConnection with unittest under a contextManager

I'm trying to test a method which retrieves a mysql connection inside a with block but when i try to get a mocked cursor from it and also the result of the execute from the cursor is not properly mocked. Here is the method to get the connection def…
Acampoh
  • 589
  • 1
  • 8
  • 23
0
votes
1 answer

python function not mocked if called from list

I am trying to mock a function when the function is called from a list of functions. The below works: # demo_module.py import demo_module_b def run_me(): run_me_too() # demo_module_b def run_me_too(): pass # test.py from demo_module…
sam
  • 1,005
  • 1
  • 11
  • 24
0
votes
1 answer

How to stop execution of python program using mock?

I'm using unittest and mock for testing a script which looks like this class Hi: def call_other(self): perform some operation sys.exit(1) def f(self): try: res = self.do_something() a =…
sans0909
  • 395
  • 7
  • 20
0
votes
0 answers

how to mock reader/writing a file

I have a function which looks like this def my_fun(input_csv, output_log, pubusb_project): f_in = open(input_csv) rows = csv.reader(f_in) f_out = open(output_log, 'w') # rest of the code including publishing to pubsub topic f_in.close() …
Em Ae
  • 8,167
  • 27
  • 95
  • 162
0
votes
0 answers

Is it possible to mock builtins.hasattr?

Is it possible to mock builtins.hasattr? I try following way: from unittest.mock import patch import unittest class TestHasattr(unittest.TestCase): @patch('builtins.hasattr') def test_hasattr(self): self.assertTrue(True,…
Dmitrij Kultasev
  • 5,447
  • 5
  • 44
  • 88
0
votes
1 answer

How to mock a method on an object in a class?

I'm trying to figure out how to mock with one additional level of indirection - a method on an object in a class. import serial class MyClass(object): def __init__(self, com_port, baudrate): self.sp = serial.Serial(com_port, baudrate) …
tarabyte
  • 17,837
  • 15
  • 76
  • 117
0
votes
0 answers

python mock assert not called once

Mock a DB query:I am trying to mock a db query and assert that save is called but it is failing with assertion. can someone please help? models.py import peewee class A(): human = CharField() Age = CharField() app.py def get_A(): return…
immrsteel
  • 1,333
  • 4
  • 13
  • 18
0
votes
1 answer

Patching python test methods with unittest

I want to patch a 3rd party library method. In this example its uuid.uuid4().hex. I have managed to get the patching work but I wonder if there is a better way to get the same result. Current solution @mock.patch('uuid.uuid4') def test_stuff(self,…
Roland Jegorov
  • 789
  • 1
  • 11
  • 27
0
votes
1 answer

How to mock function under the main file in python

I am trying to write an integration test for my python app and having trouble coming up with a good way to mock the logger. Under my tests, I have the following assert os.system(f"cd {TEST_PATH}; python ../../happy.py apply") == 256 This is working…
oyeesh
  • 551
  • 9
  • 21
0
votes
0 answers

python patch not patching out tested class

I'm trying to set up some unit tests for gateway.py. When I call the below test code once im inside gateway.py(debugging) it doesn't seem to mock out the os.environ call os.environ environ({'PATH': '.....'}) os.environ['x'] Traceback (most recent…
Justin S
  • 1,409
  • 4
  • 22
  • 38
0
votes
0 answers

How to mock nested decorated function in Python?

I have the following python file flow.py from viewflow.flow import flow_job from celery import shared_task @shared_task @flow_job def populate_proxmox_info(activation): ... In unit test I would like to mock this populate_proxmox_info method.…
James Lin
  • 25,028
  • 36
  • 133
  • 233
0
votes
0 answers

How to test python local variables?

So I have a monitor function that looks like: def task1: // do some dummy work1 def task2: // do some dummy work2 def monitor: tasks = [task1, task2] while(true): sleep(1000) for task in tasks: task.run() if…
Rocking chief
  • 1,039
  • 3
  • 17
  • 31