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

Unit testing for python function-using post requests and get requests

I am trying to test the below function using unit test mocks but i am not able to do it since the return type from request get/post is of type . please let me know how to mock this and unit test this below…
0
votes
0 answers

Passing MagicMock return value to testable function

I'm beginning to write some unit tests for my company. I want to start by testing the connection to my database. Function get_database_connection()- which is defined in a module not specified here- connects to my database. Otherwise, an exception is…
0
votes
1 answer

How to mock a method from the same class that the method being tested?

I have the following project structure repo | |--utils |----hudi.py |----__init__.py |--tests |----test_hudi.py |----__init__.py I want to test the class in hudi.py, which is: class Partitions: def __init__(self, bucket, table) …
Javier Lopez Tomas
  • 2,072
  • 3
  • 19
  • 41
0
votes
0 answers

Python Unit test works With/without using side_effect

I need to test a python script script1.py in which it imports my another class from lib.utils.ClassA import ClassA lib = ClassA(xxxxxx) lib.main() Inside ClassA (ClassA.py) it imports python package pyspark.sql.SparkSession from pyspark.sql…
0
votes
1 answer

How to test exception part in unnitest python

I want to test the exception part of my code. def main(): try: logger.info("Ejecución planificada proceso Mae_CIL") a = ObjectA() a.method() logger.info("Success") except Exception as e: …
JDK
  • 217
  • 1
  • 10
0
votes
2 answers

mock pty / termios on windows tests

I am developing an application that it's meant to be run on linux, and relies on pty and termios modules (I don't think it's important, but I'm writing some DAGs on Airflow). I use a windows workstation. I would like to run the unit tests on my…
Vito De Tullio
  • 2,332
  • 3
  • 33
  • 52
0
votes
1 answer

Should Response be imported or mocked when using unittest.mock

I'm learning unittest and unittest.mock with the latter not quite clicking. I'm mocking the response of an end-point and had the following: import unittest from unittest.mock import patch from rest_framework.test import APIClient class…
0
votes
1 answer

Setup mock without changing `called` count

let's say I want to test this function def my_function(): my_obj = MyClass() return my_obj.my_method() I want to mock MyClass, so I use unittest.mock.patch @patch('...MyClass') def test_my_function(MyClass): …
Vito De Tullio
  • 2,332
  • 3
  • 33
  • 52
0
votes
1 answer

Pytest raise Error and return value from Mock

I have the the following method in a RedisDriver class: def get(self, key): key_str = str(key) try: master = self.connection.master_for(self.service) value = master.get(key_str) except RedisError as err: error_str…
Data Mastery
  • 1,555
  • 4
  • 18
  • 60
0
votes
1 answer

How to mock a random number generator object in python

I am writing a unit test for a function and in the real function I have: rng = default_rng() ... ... # a little while later while N<50: ... idx = rng.integers(100) How do I mock out either the variable idx or the call to rng.integers? In…
user16912364
0
votes
0 answers

How to patch same class in multiple tests?

I want to patch a class in a Python library and mock its methods independently in several tests. My approach works if I run the tests separately. However, if I run the tests using $ python -m unittest test_*.py the second test appears to use the…
Kai Roesner
  • 429
  • 3
  • 17
0
votes
1 answer

Mock an internal object call in a Flask view - with pytest

Based on the following example: app = Flask(__name__) @app.route('/users') def get_users(): return UsersAPI().get_users() And the following tests (using pytest and pytest-mock): @pytest.fixture def users(): return…
renatodamas
  • 16,555
  • 8
  • 30
  • 51
0
votes
1 answer

Writing unittest for package

I have created a package which uploads a data to some storage using Azure AD access token, now I want to write test cases for the code, as I'm not aware of writing test cases have tried few. Can anyone help here, below is the code for my…
0
votes
1 answer

How to magic mock long chained calls?

To test the following functino, I want to assert .filter() is called once with parameter filters. def get_instances(session: boto3.session.Session, filters): instances = session.resource('ec2').instances.filter(Filters=filters) return…
ca9163d9
  • 27,283
  • 64
  • 210
  • 413
0
votes
1 answer

How to mock HTTPError response in Python unit test

I would like to write a unit test case for HTTPError exception part based on the error response content I get. But I have now idea how I can mock the response so that the unit test can reach doSomething1() instead of doSomething2(). foo.py def…