mocking

Test of the sequence of calling of some methods of a Python class

Testing Python methods call sequence in a multithreaded context Question: I need to check the sequence of calling of some methods of a class. I had this need while I was developing using TDD (test driven development), so when I was writing the test for method_1() I would like to be sure that it calls …

Total answers: 2

Unable to successfully patch functions of Azure ContainerClient

Unable to successfully patch functions of Azure ContainerClient Question: I have been trying to patch the list_blobs() function of ContainerClient, have not been able to do this successfully, this code outputs a MagicMock() function – but the function isn’t patched as I would expect it to be (Trying to patch with a list [‘Blob1’, ‘Blob2’]. …

Total answers: 2

Django unittest function with redirect based on mock return_value

Django unittest function with redirect based on mock return_value Question: I have a view function similar to def my_function(request): session = create_something(‘some_random_string’) return redirect(session.url, code=303) To test it import unittest from django.test import TestCase from unittest.mock import patch from my_app.views import my_function class TestMyFunction(TestCase): @patch(‘my_app.views.create_something’, return_value={ "url": "https://tiagoperes.eu/" }) def test_my_function(self, mock_create_something): response = self.client.get("/my-function/") …

Total answers: 1

Python – Mock requests with side_effect

Python – Mock requests with side_effect Question: I’m trying to mock out the method requests.get() of the module requests and set the attribute side_effect of the obtained instance of the Mock class. I would like to associate a different status_code for each side effect value but I didn’t succeed so far. def test_func1(mocker): side_effect = …

Total answers: 2

How can we confirm sleeping time in Loop Operation at Pytest?

How can we confirm sleeping time in Loop Operation at Pytest? Question: We’d like to confirm if the job was sure to wait during the loop operation as expected, in the Pytest. We set the sleep time as 30 sec in 3 times loop — we expect and testify the total waiting time would be …

Total answers: 1

How to define a mock object inside a mock in python?

How to define a mock object inside a mock in python? Question: I have a class that contains another class in a variable. Now I want to write a unit-test and define a mock object. Therefore I define a fixture in conftest.py and monkeypatch it with the mock object. I now get a the desired …

Total answers: 1

How to mock a class and its attributes at the same time

How to mock a class and its attributes at the same time Question: I have a class ClassA which is a list of ClassB.element objects. See below class ClassA: element = [] def __init__(self, data_object): for content in data_object.objects: element.append(ClassB(content).element) class ClassB: element = "" def __init__(self, content) … I am currently trying to create …

Total answers: 1

Test assert function order with mocks (pytest -> assert_has_calls)

Test assert function order with mocks (pytest -> assert_has_calls) Question: I’m trying to test the order of the sub-functions inside of the main function: def get_data(): pass def process_data(data): pass def notify_admin(action): pass def save_data(data): pass def main_func(): notify_admin(‘start’) data = get_data() processed_data = process_data(data) save_data(processed_data) notify_admin(‘finish’) I’m using pytest, so far I’ve come up …

Total answers: 1

Python unit-testing a complicated method

Python unit-testing a complicated method Question: I am looking for any advice on how to unit test the following method. def delete_old_files(self): """Deletes all recordings older then a predefined time period. Checks the mtime of files in _store_dir and if it is older then the predefined time period delete them. """ now = time.time() self.logger.info( …

Total answers: 1

Python mock patch not mocking the object

Python mock patch not mocking the object Question: I am using the python mocking library and I am not sure why I get this result. Why only the second one got mocked and not the first one? What’s the best way to do it? import unittest from unittest.mock import patch, Mock import requests from requests …

Total answers: 1