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

Is there a built-in mock object to pass in to Python unit test?

I commonly instantiate a Mock object during unit tests. I am sick of: Having to type from unittest.mock import Mock And then instantiate a Mock object via mock = Mock() I am wondering, does pytest, unittest.mock, pytest-mock, etc. have a built in…
0
votes
1 answer

Unit testing in Python: Nesting mocked objects

I am new to unit testing in Python. I am trying to unit test a method that does something like this: MyClass: client : Client def __init__(client): self.client=client def method_to_test(self): images =…
0
votes
1 answer

Python mock.patch is not mocking my function?

So I wrote a pretty simple test case and function. I can't seem to figure out the problem. Anyone have an idea? I feel like it should work. import pandas import unittest from unittest import mock from unittest.mock import MagicMock, Mock def…
0
votes
1 answer

Confused about exception testing for my python function

I have a function process_payment that I want to do exception testing for: def process_payment(event, context): try: payDictionary= json.loads(event["body"]) if "payment_method_id" in payDictionary: intent =…
a_sid
  • 577
  • 10
  • 28
0
votes
0 answers

How to mock a function called inside a class attribute?

I have a class with an attribute that calls a function: users.mixins.py: from django.contrib import messages from ..utils.services import redirect_to_previous class AdminRightsMixin: def dispatch(self, request, *args, **kwargs): if…
alias51
  • 8,178
  • 22
  • 94
  • 166
0
votes
1 answer

Muliple patch in Python unitest not working

My test case looks like this. Following is the code: @patch('something.mysqlclient') @patch('something.esclient') def testcase1(mysql,esclient): esclient.return_value = 1 mysql.return_value = 3 assert something.modeul1.esclient == 1 …
Ankita
  • 11
  • 2
0
votes
1 answer

Override pathlib.Path.exists for a conditional test

I want to cause Path.exists() to only return True when a specific Path is being tested: from unittest import TestCase from mock import patch import pathlib def fn(names): for index, name in enumerate(names): if…
kfsone
  • 23,617
  • 2
  • 42
  • 74
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
2 answers

Expressions can only be used to update, not to insert

i got this issue when trying to create object using Factory boy and unittest.mock for mocking payment self = , field = value =…
user13834974
0
votes
1 answer

Unittest: mock a python class with init method

I am trying to test a run method in my class which has init method and takes object as parameter from another class: class ServePlate(FreeSurferStep): process_name = "FreeSurfer" step_name = "ServePlate" step_cli = "serve" cpu = 1 …
0
votes
1 answer

Patch a single class method in a unittest and assert it is called once

How do you patch a method in a class and then assert that that patched method was only called once? For example: import typing import unittest import unittest.mock class Example: def __init__(self: "Example") -> None: self._loaded :…
MT0
  • 143,790
  • 11
  • 59
  • 117
0
votes
1 answer

Python mock test array of functions

Following is my python module my_func.py def get_data(request): function_1(request) I want to test if function_1 is called with correct request argument inside get_data() function and the following test works fine. class…
DhiwaTdG
  • 748
  • 1
  • 10
  • 26
0
votes
1 answer

python mock import in imported module

I'm trying to use unittest.mock to mock an import in a module under test. What I'm seeing is that although my module calls sleep 5 times the mock object I'm interacting with in the test function isn't what I'm expecting. I'm assuming I'm not doing…
Brian
  • 848
  • 10
  • 32
0
votes
0 answers

Mock unit test for Python boto3 for AWS S3

I'm currently learning how to write unit test for boto3 I'm trying to follow the moto documentation and have written a few lines for the unit test but it gave me error: @mock_s3 def test_upload_to_s3(self): conn = boto3.resource('s3') …
wawawa
  • 2,835
  • 6
  • 44
  • 105
0
votes
1 answer

How to mock an uninitialized global variable?

I have an uninitialzed global variable in my module that is properly initialized during application startup. For typechecking I use the syntax var: Type without a value from python >= 3.6 so I do not have to complicate the typechecking for the…