magicmock

mock file open in python

大城市里の小女人 提交于 2019-12-07 03:33:48
问题 I'm trying to mock file open, and all of the examples show that I need to @patch('open', create=True) but I keep getting Need a valid target to patch. You supplied: 'open' I know patch needs the full dotted path of open , but I have no idea what it is. As a matter of fact, I'm not even sure that's the problem. 回答1: You need to include a module name; if you are testing in a script, the name of the module is __main__ : @patch('__main__.open') otherwise use the name of the module that contains

Mocking a imported function with pytest [duplicate]

此生再无相见时 提交于 2019-12-06 05:14:56
This question already has answers here : Python Mocking a function from an imported module (2 answers) Closed last year . I would like to test a email sending method I wrote. In file, format_email.py I import send_email. from cars.lib.email import send_email class CarEmails(object): def __init__(self, email_client, config): self.email_client = email_client self.config = config def send_cars_email(self, recipients, input_payload): After formatting the email content in send_cars_email() I send the email using the method I imported earlier. response_code = send_email(data, self.email_client) in

python check if a method is called without mocking it away

假如想象 提交于 2019-12-05 11:13:08
class A(): def tmp(self): print("hi") def b(a): a.tmp() To check if tmp method is called in b, the recommended way is a = A() a.tmp = MagicMock() b(a) a.tmp.assert_called() But tmp here is being mocked away and is not resulting in a "hi" getting printed. I would want my unit test to check if method tmp is called without mocking it away. Is this possible? I know this is not a standard thing to expect when writing unitests. But my use case (which is bit tricky) requires this. You can set the Mock.side_effect to be the original method. from unittest.mock import MagicMock class A(): def tmp(self):

Mock entire python class

删除回忆录丶 提交于 2019-12-04 09:12:15
问题 I'm trying to make a simple test in python, but I'm not able to figure it out how to accomplish the mocking process. This is the class and def code: class FileRemoveOp(...) @apply_defaults def __init__( self, source_conn_keys, source_conn_id='conn_default', *args, **kwargs): super(v4FileRemoveOperator, self).__init__(*args, **kwargs) self.source_conn_keys = source_conn_keys self.source_conn_id = source_conn_id def execute (self, context) source_conn = Connection(conn_id) try: for source_conn

Assert mocked function called with json string in python

混江龙づ霸主 提交于 2019-12-04 06:57:57
Writing some unit tests in python and using MagicMock to mock out a method that accepts a JSON string as input. In my unit test, I want to assert that it is called with given arguments, however I run into issues with the assert statement, since the ordering of objects within the dict doesn't matter, besides in the assert statement for the string. Simplified example of what I am trying to achieve below. mock_funct = MagicMock() # mocked function called elsewhere expected = {"a":"a", "b":"b"} mock_funct.assert_called_once_with(json.dumps(expected)) The above may pass or may fail due to the

Python mock patch doesn't work as expected for public method

北城以北 提交于 2019-11-29 09:55:45
I'm trying to patch a public method for my flask application but it doesn't seem to work. Here's my code in mrss.feed_burner def get_feed(env=os.environ): return 'something' And this is how I use it @app.route("/feed") def feed(): mrss_feed = get_feed(env=os.environ) response = make_response(mrss_feed) response.headers["Content-Type"] = "application/xml" return response And this is my test which it's not parsing. def test_feed(self): with patch('mrss.feed_burner.get_feed', new=lambda: '<xml></xml>'): response = self.app.get('/feed') self.assertEquals('<xml></xml>', response.data) I believe