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

Python unit testing that a method does not call logging.info with specific messages

I am trying to unit test method that calls logging.info three times – first time it's logging local variables for the method, another two times it's logging messages that get triggered in case a certain condition is fulfilled. In one of my test…
Myklebost
  • 59
  • 8
0
votes
0 answers

AttributeError: 'str' object has no attribute 'as_plain_ordered_dict'

I am trying to mock "ConfigFactory.parse_file" for unit testing purpose and getting the following error. AttributeError: 'str' object has no attribute 'as_plain_ordered_dict' I tried mocking the attribute as_plain_ordered_dict to resolve the issue,…
0
votes
1 answer

Cannot mock python function called during import

I am trying to mock a sqlalchemy create_engine method called during import of a python script. But I am not able to properly do so. Here is my file structure: File main.py from database_wrapper import update_database def do_something(): # do…
DarioB
  • 1,349
  • 2
  • 21
  • 44
0
votes
1 answer

Python, Github: Get all branches

I would like to get all the GitHub branches of a certain repository. Using the GitHub API as documented here, https://developer.github.com/v3/repos/branches/#list-branches I try to use the GET request documented. However, when I try unit testing,…
user19251203
  • 322
  • 4
  • 13
0
votes
0 answers

Check if a mocked function is being called from within a class method

I have the following files - summer.py def sum(a, b): return a+b base.py class Base(): def work(self, a, b): self.add(a, b) calculator.py from summer import sum from base import Base class Calculator(Base): def work(self, a,…
m0bi5
  • 8,900
  • 7
  • 33
  • 44
0
votes
1 answer

Using mock patch decorator twice with autospec=True

How do I create two different mocked objects with the same spec using the patch decorator? I have a test that needs two mocked selenium WebElements. I could just use two patch…
Marcel Wilson
  • 3,842
  • 1
  • 26
  • 55
0
votes
1 answer

Python `unittest.mock` assertion on multiple method calls with mutated arguments?

Today I spent pretty much time on some tricky unit test issue, where I tried to properly assert two calls on the same method and getting very strange behavior from unittest.mock's assert_has_calls method. Here there is very simplified example how I…
Wolf
  • 98
  • 7
0
votes
2 answers

Django unittest: required mock patch dotted path varies depending on how one calls/runs the tests

It took me hours to figure out how to patch the below code. The path to it was very much unexpected. Depending on how I run the tests, and which dir I am in, I find the dotted path to the module to patch changes. This is really bad for unittesting.…
run_the_race
  • 1,344
  • 2
  • 36
  • 62
0
votes
1 answer

How to run exception Unit test for one function that calls another function inside of it

I am trying to do exception unit test for check_name() function but create_list() is also called. Is there any way I can mock the output of create_list() instead of executing it? def create_list(token): return service_list def check_name(token,…
0
votes
1 answer

FastApi Test Client executing the internal api call

This is the first time I'm trying to write test cases. I've got a simple FastAPI application and I'm trying to create tests with unittest module. My goal is to test how app behaves for success case I've got a simple route in my app: from fastapi…
praveen jp
  • 65
  • 10
0
votes
1 answer

How do you use googles python api mocks?

I need a bit of help using google's api mocks. I am new to using mocks and google's api. Here is the api mock Here is my code I want to test: #add_entry_to_calendar.py #... try: service = build("calendar", "v3",…
0
votes
0 answers

Mocking method from another class with python unittest mock

I have a class and within that class there are used methods from another classes. Let's say it looks like that (just an example): from jobs.fruit import Fruit from jobs.veggie import Veggie class Healthy: def start(self, arg): …
0
votes
3 answers

Pydantic (BaseModel) - How to mock (pytest/unittest/mockito)?

I'm doing unit tests (using pytest/unittest/mockito, basically) and I need to mock the instantiation of a class implemented using Pydantic (BaseModel). Apparently it's not possible to mock a class in these circumstances without passing effectively…
0
votes
0 answers

Python assert_called_with custom assertion logic or retrieve values of call parameters

Consider the following test method: @patch("api.views.my_views.MyViewSet.send_customer_request") def test_process_customers(self, send_customer_request_mock): process_customers(123, 456) # The above call eventually results in a call to…
JoeMjr2
  • 3,804
  • 4
  • 34
  • 62
0
votes
1 answer

Why python unittest fails to mock some static methods

I have some static methods written in util files. When I try to patch them and add a side_effect to return a specific value, they just execute the original method content. It is not getting mocked. Why is this happening? Is there any way to overcome…
udani
  • 1,243
  • 2
  • 11
  • 33