问题标签 [requests-mock]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
0 回答
85 浏览

python - 为什么在使用 requests_mock 时,覆盖率会在某些机器上报告无效信息?

以下代码路径:

运行时导致覆盖错误地为我们报告其行requests_mock

在某些机器上,该行之后的requests.post行被错误地报告(因为它们没有报告覆盖那些被覆盖的行的行)。但是,关闭 wifi 会导致这些线路用于覆盖 (?)。

测试正在使用 tox 和 repo 根目录中的 tox.ini 文件运行,该文件将测试命令设置为:

使用 setup.cfg 包含:

我缩小了范围:

  • 正在执行代码路径(否则测试将失败,必须命中代码路径)
  • 更改 requests_mock 返回未通过测试
  • 安装的所有版本的python/包(虚拟环境和全局)都是相同的
    • codecov==2.0.5,coverage==4.2,nose==1.3.7,nose-exclude==0.4.1,requests==2.11.0,requests-mock==1.3.0,
  • OSX 的版本是相同的(10.11.6 - 尽管其他版本的人也看到了这一点)
  • 验证etc/hosts正常
  • 在 macbook pro 上,视网膜(所以 wifi 禁用是通过使用菜单选项禁用)
  • 问题发生在之后requests.post-post不报告除该行之外的所有未来行
  • 对于 27、3.5 和 3.6 python,tox 中的行为相同
  • Python 已与 brew 一起安装
  • 当从 pyenv/virtualenvs 运行 tox 时,两者都显示相同的行为

对于我们大约 50% 的团队,无论有/没有 wifi,完全相同的代码库都可以正常工作,但另外 50% 的团队看到了仅覆盖率在没有 wifi 的情况下正确报告的问题行为。

对于这里可能出现的问题,我完全没有想法。为什么会发生这种情况?

0 投票
0 回答
70 浏览

python - 防止 requests-mock 降低我的帖子数据的大小写

使用 requests-mock 时,我的查询字符串数据是小写的:

request.qs的实际值为{'foo': ['bar']}

如何让它不小写我的查询字符串数据?

有一个应该在 1.1 中修复的错误:https ://bugs.launchpad.net/requests-mock/+bug/1584008

我正在使用 1.3,但仍然有这个问题。

0 投票
1 回答
884 浏览

python - Capture URL parameter with requests-mock

I'm using requests-mock to mock an external service with a dynamic response.

The service's URL is something like http://test/containers/test/1234, where 1234 is the object id I want to dynamically generate.

I've tried the regular expression matcher but I don't seem to be able to get the match object in the dynamic response callback.

Is there a way to "capture" that last bit of the URL?

0 投票
1 回答
4610 浏览

python-requests - 为什么 requests-mock 装饰器模式会在 pytest 中抛出“fixture 'm' not found”错误?

我正在使用requests库发出 HTTP GET 请求。例如(截断):

我已经按照requests-mock 装饰器模式编写了一个测试。

当我使用pytest运行测试时,出现以下错误。

为什么 requests-mock 装饰器会抛出“fixture 'm' not found”错误?我该如何解决?

0 投票
2 回答
637 浏览

python - 如何检查预期数据是否通过 post 或 put 请求发送?

我正在使用requests-mock并试图弄清楚如何断言put请求已被正确调用:

编辑:我将要测试的代码重构为一个名为funtion_to_be_tested

0 投票
1 回答
1993 浏览

python - 如何在 pytest requests mock 中伪造我的回复

我正在尝试测试的功能正在list返回strings

我的测试功能:

我收到如下错误:json.decoder.JSONDecodeErrorstr object has no attribute get

如何伪造我的响应以匹配我的函数的返回值?

0 投票
1 回答
664 浏览

python - 在 python mock_requests lib 中验证身份验证

我正在创建一个单元测试,以确保我的 http 客户端正确传递了身份验证令牌。我正在使用requests_mock

但是 history._request 是一个受保护的成员,所以我想避免在我的代码中绑定到受保护的成员。有没有更合适的方法来检查requests_mock中的 Authorization 标头?

0 投票
1 回答
281 浏览

python - 在 requests_mock 中模拟 response.elapsed

我在我的单元测试中使用 requests_mock 并且想模拟 response.elapsed 属性但没有找到正确的方法来做到这一点。刚刚找到了将睡眠添加到文本回调的解决方法。

有没有更好的方法来模拟response.elapsed使用requests_mock

0 投票
1 回答
4730 浏览

python - requests_mock NoMockAddress 异常

我目前正在尝试使用 requests_mock 库进行单元测试以修补请求。我编写了以下代码:

但我收到以下错误:

谁能帮我弄清楚如何解决它?我使用 mock 作为 pytest 功能。

0 投票
2 回答
65 浏览

requests-mock - 模拟请求执行实际请求调用,而不是模拟

我正在使用 requests-mock 在我的 unittest 代码中模拟一个 get 请求,但是当我在测试期间运行代码时,它仍然会尝试访问实际的 URL,而不是返回模拟的数据。

这是我的代码:

这是我的测试代码

我的api_url代码中的 与我传递给的 URL 匹配m.get()。但是,当我运行测试时,我没有得到“text”的值,而是得到了401 Client Error: Unauthorized 一个来自服务器的响应,指示“Bad credentials”,它告诉我它实际上试图联系服务器而不是返回文本我曾要求在模拟。

我迷路了。据我所知,我完全按照文档指示使用它。有任何想法吗?