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

Python testing: mocking a function that's imported AND used inside another funciton

Due to circular-import issues which are common with Celery tasks in Django, I'm often importing Celery tasks inside of my methods, like so: # some code omitted for brevity # accounts/models.py def refresh_library(self, queue_type="regular"): …
zerohedge
  • 3,185
  • 4
  • 28
  • 63
0
votes
0 answers

Mocking relative-path class instantiation with full-path mocker in pytest - impossible?

I have 2 modules. The first one say A: Inside module A I have: from my_lib import SomeClass def my_function: my_var = SomeClass() do something with my_var... The package my_lib is a package I created and i pip-installed it locally into my…
Caffeine
  • 445
  • 1
  • 4
  • 16
0
votes
0 answers

path a before_request method in flask unittests

I have a Flask app that I use, Im working on authenticating all traffic to the server (except for /login and one more endpoint) I implemented this using the @app.before_request decorator like so: @app.before_request def authenticate(): if…
NotSoShabby
  • 3,316
  • 9
  • 32
  • 56
0
votes
1 answer

How to mock a Python class to change just one argument

I need to wrap a class in a mock like so: Original code: class _InnerImpl(object): def __init__(self, arg1, arg2, arg3): # do stuff I would like to do something like this: from unittest.mock import patch def my_wrapper(*args, **kwargs): …
H-N
  • 101
  • 2
  • 8
0
votes
1 answer

How to mock internal function's side effect

HI I have a simple function. Which internally calls db but during local testing it can't connect DB so for that I have wrote specific exception and I want do side effect for same but it's not working. Here is my code def…
0
votes
1 answer

How to user Python Mock side effect to return different value based on function being mocked

I have a common function that I want to use as a side effect for two different functions. Based on the function being mocked , they should return a different value. The code is: def sleep_three(*args, **kwargs): logging.info('Sleeping 3…
peaxol
  • 497
  • 4
  • 13
0
votes
1 answer

mocking python libraries or wrapper method

In my code release_bundler.py file class MavenDependenciesPathsBuilderStrategy(PathsBuilderStrategy): def build_bundler_copy_paths(self, mapping_values): source = join(getcwd(),'target','dependency',mapping_values[0]) destination =…
Anadi Misra
  • 1,925
  • 4
  • 39
  • 68
0
votes
1 answer

Python 3 unittest patch doesn't return desired value

I am trying to use the unitttest mock framework (python 3.4.9) to mock one of the method in my test case. And it's failing as it doesn't return the mocked value. This is the simplest example. In my case I can't change the way method being invoked.…
Gaurang Shah
  • 11,764
  • 9
  • 74
  • 137
0
votes
1 answer

python: specify return value of attribute of mocked module

I have a module ex which has multiple attributes and another module that uses these attributes like this import ex def use_ex(attr): z = getattr(ex,attr) #Do something with z In my test file, I mock the ex as I cannot import it in the test…
0
votes
1 answer

How to convert csv into mock when csv file is specified as argument in unit test of Django command

There is a django command that gives the path of the csv file as an argument as follows. class Command(NoticeCommand): def add_arguments(self, parser): parser.add_argument( '--file', dest="file", type=str, required=True …
xKxAxKx
  • 1,044
  • 5
  • 16
  • 31
0
votes
1 answer

Python Mock - check if method was called from another class?

How can I check if method is called inside another method, when those methods are from different classes? If they are from same class, I can do this: from unittest import mock class A(): def method_a(self): pass def method_b(self): …
Andrius
  • 19,658
  • 37
  • 143
  • 243
0
votes
0 answers

When exactly patched class methods are called?

While writing some tests today I came across a confusing behaviour: Foo class definition: class Foo: def bar(self, attrs): self._baz(attrs) attrs.pop('key') def _baz(self, attrs): pass Foo class tests: from…
Ernest
  • 2,799
  • 12
  • 28
0
votes
1 answer

Mocking class vs methods

I want to mock a python object -psycopg2 - to test my classes. When I print the object - dir(conn), I see the connect, extras, etc, all being listed. conn = mock.MagicMock(psycopg2.connect) Result: ['BINARY', 'Binary', 'DATETIME', 'DataError',…
user1050619
  • 19,822
  • 85
  • 237
  • 413
0
votes
0 answers

Python mock patch a method within a class instance

I have a project assigning 2 configs in my main __init__ file since they are used frequently throughout the project. #__init__.py from config import Config config1 = Config('Email') config2 = Config('Test') My Config class within config.py has a…
staten12
  • 735
  • 3
  • 9
  • 20
0
votes
2 answers

Python - Mocking nested DB calls

I'm trying to write unit test for some_func function in some_func.py below. I do not want to connect to any database during this tests and I want to mock out any calls to DB. Since the actual db calls are somewhat nested, i'm not able to get this…
Naveen
  • 687
  • 3
  • 12
  • 24