【问题标题】:Mocking in Behave and Flask在行为和烧瓶中模拟
【发布时间】:2020-01-17 09:34:10
【问题描述】:

我正在尝试使用 mock 模拟 HTTP 请求调用,因为我不想 Behave 实际调用它。

所以我在matches.py 文件中有这个代码场景:

import request

def get_match():
   response = request.get("https://example.com")
   return response

在我的步骤定义中match_steps.py 为表现我有这个:

def logoport_matches_response(context):
   mock_response = context.text # this is the payload will come from feature file
   with patch ('match') as mock_match:
      mock_match.get_match.return_value = {"status": "success"}

但这似乎不起作用,因为它仍在请求实际的 HTTP 请求。

我需要模拟 get_match 方法以返回 {"status": "success"} 结果

【问题讨论】:

    标签: python flask bdd python-mock python-behave


    【解决方案1】:

    好吧,我明白了,你需要把你的初始化放在 mock 中,所以:

    from mock import patch
    from matches import get_match
    
    
    with patch ('match') as mock_match:
          mock_match.retun_value = {"status": "success"}
          get_match()
    

    【讨论】:

      猜你喜欢
      • 2018-04-27
      • 2020-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-28
      • 1970-01-01
      • 1970-01-01
      • 2021-10-06
      相关资源
      最近更新 更多