即使期望匹配,Python单元测试也会失败

发布于 2025-01-30 14:32:33 字数 1621 浏览 2 评论 0原文

我敢肯定,当写这篇文章时,我在这里缺少一些非常基本的东西...

我有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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦在深巷 2025-02-06 14:32:33

我用json.dumps更改了。

      mock0.assert_called_with("http://localhost:8080/portable_mid/connect", data=json.dumps({"portable_mid_ip": "127.0.0.1"}), headers=None).return_value.status_code = 200

I changed jsonify with json.dumps and it worked.

      mock0.assert_called_with("http://localhost:8080/portable_mid/connect", data=json.dumps({"portable_mid_ip": "127.0.0.1"}), headers=None).return_value.status_code = 200

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文