python-unittest.mock

check unittest.mock call arguments agnostically w.r.t. whether they have been passed as positional arguments or keyword arguments

折月煮酒 提交于 2021-02-18 11:09:40
问题 When a unittest.mock.Mock object has been called, I can check for the argument values with the exact signature of the call: from unittest.mock import Mock m = Mock() # creation of mock m('foo', bar='baz') # call to the mock m.assert_called_once_with('foo', bar='baz') # check call arguments Checking for a different signature with the same values will fail. E.g., if we check with 'baz' as a positional argument instead of a named argument, the assertion will fail: m.assert_called_once_with('foo'

check unittest.mock call arguments agnostically w.r.t. whether they have been passed as positional arguments or keyword arguments

我是研究僧i 提交于 2021-02-18 11:08:05
问题 When a unittest.mock.Mock object has been called, I can check for the argument values with the exact signature of the call: from unittest.mock import Mock m = Mock() # creation of mock m('foo', bar='baz') # call to the mock m.assert_called_once_with('foo', bar='baz') # check call arguments Checking for a different signature with the same values will fail. E.g., if we check with 'baz' as a positional argument instead of a named argument, the assertion will fail: m.assert_called_once_with('foo'

check unittest.mock call arguments agnostically w.r.t. whether they have been passed as positional arguments or keyword arguments

浪子不回头ぞ 提交于 2021-02-18 11:06:50
问题 When a unittest.mock.Mock object has been called, I can check for the argument values with the exact signature of the call: from unittest.mock import Mock m = Mock() # creation of mock m('foo', bar='baz') # call to the mock m.assert_called_once_with('foo', bar='baz') # check call arguments Checking for a different signature with the same values will fail. E.g., if we check with 'baz' as a positional argument instead of a named argument, the assertion will fail: m.assert_called_once_with('foo'

check unittest.mock call arguments agnostically w.r.t. whether they have been passed as positional arguments or keyword arguments

丶灬走出姿态 提交于 2021-02-18 11:06:29
问题 When a unittest.mock.Mock object has been called, I can check for the argument values with the exact signature of the call: from unittest.mock import Mock m = Mock() # creation of mock m('foo', bar='baz') # call to the mock m.assert_called_once_with('foo', bar='baz') # check call arguments Checking for a different signature with the same values will fail. E.g., if we check with 'baz' as a positional argument instead of a named argument, the assertion will fail: m.assert_called_once_with('foo'

What happens when a python mock has both a return value and a list of side effects?

一笑奈何 提交于 2020-12-13 04:16:34
问题 I'm having trouble understanding what's happening in some test code. It looks like this: import pytest from unittest.mock import MagicMock from my_module import MyClass confusing_mock = MagicMock( return_value=b"", side_effect=[ ConnectionError(), b"another_return_value?", b"another_another_return_value?" ]) mocked_class = MyClass() monkeypatch.setattr(mocked_class, "method_to_call_thrice", confusing_mock) I know that: side_effect is a function to be called whenever the mock is called but if

What happens when a python mock has both a return value and a list of side effects?

回眸只為那壹抹淺笑 提交于 2020-12-13 04:16:25
问题 I'm having trouble understanding what's happening in some test code. It looks like this: import pytest from unittest.mock import MagicMock from my_module import MyClass confusing_mock = MagicMock( return_value=b"", side_effect=[ ConnectionError(), b"another_return_value?", b"another_another_return_value?" ]) mocked_class = MyClass() monkeypatch.setattr(mocked_class, "method_to_call_thrice", confusing_mock) I know that: side_effect is a function to be called whenever the mock is called but if

How to mock data as request.Response type in python

我的梦境 提交于 2019-12-22 09:47:12
问题 I would like to write some testcase to exercise object_check in isinstance(obj, requests.Response) logic. After I create Mock data as return value for requests.post. The type for mock data is always be Mock class. In that way, how can I rewrite mock data so mock data can be type of requests.Response? so I can exercise line d = obj.json() ? from unittest.mock import patch, Mock import unittest import requests from requests.exceptions import HTTPError import pytest def object_check(obj): if

Python Unit Test : How to unit test the module which contains database operations?

这一生的挚爱 提交于 2019-12-18 17:32:08
问题 I am using pymysql client library to connect to the real database. I have a function in module, where I connect to the database using pymysql and do only database insert operations.How to unit test this function in python without hitting the real database? import pymysql def connectDB(self): # Connect to the database connection = pymysql.connect(host='localhost', user='user', password='passwd', db='db') try: with connection.cursor() as cursor: # Create a new record sql = "INSERT INTO `users`