python-mock

Mock's autospec injects a wrong argument into a called function

╄→尐↘猪︶ㄣ 提交于 2019-12-11 02:36:27
问题 My understanding is that autospec in its simplest form as used here will check the signature of the function which is being mocked against the presented arguments. Its purpose is to raise an error if they do not match. In the code below it seems to inject an additional argument - the object itself. Why does the use of the mock module's autospec lead to the unexpected behavior shown here? For this question I've created a simplified version in the module simplebutton . When it's run as the main

Python Mock not asserting calls

人盡茶涼 提交于 2019-12-11 02:21:41
问题 I'm using the mock library to patch a class in a program that connects to a external resource and sends a dictioanry. The structure goes a litle like this... code.py def make_connection(): connection = OriginalClass(host, port) connection.connect() connection.send(param) connection.close() test.py @mock.path('code.OriginalClass') def test_connection(self, mocked_conn): code.make_connection() mocked_conn.assert_called_with(host, port) mocked_conn.connect.assert_called_once() mocked_conn.send

How to Mock a user input in Python

落花浮王杯 提交于 2019-12-11 01:07:36
问题 I am currently trying to learn how to Unit Test with Python and was introduced to the concept of Mocking, I am a beginner Python developer hoping to learn the concepts of TDD alongside my development of Python skills. I am struggling to learn the concept of mocking a class with a given input from the user for Python unittest.mock documentation. If I could get an example of how I would mock a certain function, I would be really grateful. I will use the example found here: Example Question

Mock Patches Appearing in the Wrong Order?

纵然是瞬间 提交于 2019-12-08 14:47:33
问题 I have a test module ( test.py ) which imports functions from another module ( keyboard.py ). keyboard.py def get_keys(keyList, timeStamped): return event.getKeys(keyList=keyList, timeStamped=timeStamped) def wait_keys(keyList, timeStamped): return event.waitKeys(keyList=keyList, timeStamped=timeStamped) test.py @mock.patch('keyboard.wait_keys') @mock.patch('keyboard.get_keys') def test_2(self, mock_waitKeys, mock_getKeys): mock_waitKeys.return_value = [['wait_keys!', 0.1]] mock_getKeys

python mocking third party modules

送分小仙女□ 提交于 2019-12-08 08:39:01
问题 im trying to test some classes that process tweets. Im using sixohsix twitter to deal with Twitter API. I have a class that acts as a facade for the Twitter classes, and my idea was to mock the actual sixohsix classes to simulate the arrival of tweets, by randomly generate new tweets or retrieving them from a database. My facade looks something like: from twitter import TwitterStream class TwitterFacade(object): def __init__(self, dev='soom'): self._auth = OAuth(dev_keys["ACCESS_TOKEN"], dev

“Mocking where it's defined” in python mock?

為{幸葍}努か 提交于 2019-12-08 04:37:54
问题 I want to test an evolving SQLite database application, which is in parallel used "productively". In fact I am investigating a pile of large text files by importing them to the database and fiddling around with it. I am used to develop test-driven, and I do not want to drop that for this investigation. But running tests against the "production" database feels somewhat strange. So my objective is to run the tests against a test database (a real SQLite database, not a mock) containing a

Mock function from other module

本小妞迷上赌 提交于 2019-12-08 04:00:22
问题 I have two python files: function.py: def foo (): return 20 def func (): temp = foo() return temp and mocking.py: from testing.function import * import unittest import mock class Testing(unittest.TestCase): def test_myTest(self): with mock.patch('function.func') as FuncMock: FuncMock.return_value = 'string' self.assertEqual('string', func()) I want to mock func, but with no positive result. I have AssertionError: 'string' != 20. What should I do to mock it correctly ? If I do mock.patch (

How to call mocked method in Python mock

对着背影说爱祢 提交于 2019-12-08 03:31:34
问题 I want to create a mock method that calls the underlying method being mocked. I'm imagining something like the following, but I can't find any documentation about the mock object holding a reference to the object being mocked, which I've denoted as [[wrapped_method_foo]] below: from mock import patch class Foo(object): def __init__(self, state): self.state = state def foo(self, a): print "real foo", a return a + self.state f = Foo(2000) f.foo(1) with patch.object(Foo, 'foo', autospec=True) as

How to mock the “+” operator in python (specifically datetime.date + datetime.timedelta)

微笑、不失礼 提交于 2019-12-07 07:25:33
问题 I have working through some date mocking issues in Django, and have the final hurdle (I hope) is the following situation. I have a FakeDate class, which derives from datetime.date , which it mocks out. The FakeDate class works as expected, however I get a problem when adding a datetime.timedelta to the FakeDate, in that it returns a genuine datetime.date , rather than the mock. This is important as elsewhere in a third party library there is an isinstance(value, datetime.date) check, which

Mocking ftplib.FTP for unit testing Python code

断了今生、忘了曾经 提交于 2019-12-06 20:18:55
问题 I don't know why I'm just not getting this, but I want to use mock in Python to test that my functions are calling functions in ftplib.FTP correctly. I've simplified everything down and still am not wrapping my head around how it works. Here is a simple example: import unittest import ftplib from unittest.mock import patch def download_file(hostname, file_path, file_name): ftp = ftplib.FTP(hostname) ftp.login() ftp.cwd(file_path) class TestDownloader(unittest.TestCase): @patch('ftplib.FTP')