即使期望匹配,Python单元测试也会失败
我敢肯定,当写这篇文章时,我在这里缺少一些非常基本的东西...
我有python函数
## views.py
from flask import request, jsonify
log_obj = EdgeLogger(name='edge_api')
def notifyMobile(ip):
r = requests.post(MobileToPortableMid.NOTIFY_MOBILE_URL, data=jsonify({"portable_mid_ip": ip}), headers=None)
if r.status_code == requests.codes.ok:
log_obj.debug("ip address sent to mobile edge")
else:
log_obj.error("error sending ip address resulted in following status code: {}".format(r.status_code))
,我想测试。.Unistest
看起来像这样。
from unittest.mock import patch, call, Mock, MagicMock
import json
import unittest
@patch('views.log_obj')
@patch('requests.post')
def test_notify_mobile(self, mock0, mock1):
from views import notifyMobile
from flask import jsonify
mock1.debug = MagicMock(return_value=None)
testapp = app
with testapp.app_context():
notifyMobile('127.0.0.1')
mock0.assert_called_with("http://localhost:8080/portable_mid/connect", data=jsonify({"portable_mid_ip": "127.0.0.1"}), headers=None).return_value.status_code = 200
mock1.debug.assert_called_with("ip address sent to mobile edge")
当我运行测试案例时,我会看到期望和实际结果匹配,但是测试案例仍然失败了,
E AssertionError: expected call not found.
E Expected: post('http://localhost:8080/portable_mid/connect', data=<Response 32 bytes [200 OK]>, headers=None)
E Actual: post('http://localhost:8080/portable_mid/connect', data=<Response 32 bytes [200 OK]>, headers=None)
我不知道足够的python来了解这是什么原因。
I'm sure as of writing this there is something very basic I'm missing here...
I have following python function
## views.py
from flask import request, jsonify
log_obj = EdgeLogger(name='edge_api')
def notifyMobile(ip):
r = requests.post(MobileToPortableMid.NOTIFY_MOBILE_URL, data=jsonify({"portable_mid_ip": ip}), headers=None)
if r.status_code == requests.codes.ok:
log_obj.debug("ip address sent to mobile edge")
else:
log_obj.error("error sending ip address resulted in following status code: {}".format(r.status_code))
which I'm looking to test..
unittest for it look something like this.
from unittest.mock import patch, call, Mock, MagicMock
import json
import unittest
@patch('views.log_obj')
@patch('requests.post')
def test_notify_mobile(self, mock0, mock1):
from views import notifyMobile
from flask import jsonify
mock1.debug = MagicMock(return_value=None)
testapp = app
with testapp.app_context():
notifyMobile('127.0.0.1')
mock0.assert_called_with("http://localhost:8080/portable_mid/connect", data=jsonify({"portable_mid_ip": "127.0.0.1"}), headers=None).return_value.status_code = 200
mock1.debug.assert_called_with("ip address sent to mobile edge")
When I run the test case I see the expectation and actual result matched but the test case still fails
E AssertionError: expected call not found.
E Expected: post('http://localhost:8080/portable_mid/connect', data=<Response 32 bytes [200 OK]>, headers=None)
E Actual: post('http://localhost:8080/portable_mid/connect', data=<Response 32 bytes [200 OK]>, headers=None)
I don't know enough python to understand what is reason for this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我用
用
json.dumps
更改了。I changed
jsonify
withjson.dumps
and it worked.