问题标签 [httpretty]

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 投票
1 回答
181 浏览

python - Travis build timeouts even though test output reports success

My Travis build is timing out for some reason. Here is the link to the logs: https://travis-ci.org/madedotcom/atomicpuppy/builds/70202335

And my current travis yml https://github.com/madedotcom/atomicpuppy/blob/master/.travis.yml

I have tried running it with a normal script command

As well as creating a bash script that ran the tests and exited with $?

Even this didn't help.

My best guess is that there is some task running in the background of my Travis build due to depending on asynchronous python libs, but how can I debug that on Travis? Is there any way to increase verbosity?

Worth adding that locally tests are very fast to run, and exit with 0 without any problems, using the same commands.

0 投票
0 回答
183 浏览

python - 如何在 Python 的单元测试中为 GET 请求设置不同的响应

我需要根据通过HTTPretty的 GET 参数返回不同的响应:

此代码打印以下内容:

为什么?我认为它应该是

反而。

0 投票
0 回答
703 浏览

python - python unittest中不会导致测试失败的静默异常

我正在使用 unittest 框架为 python 程序编写单元测试。该程序的功能之一是使用请求库连接到外部 REST API。如果出现连接错误或超时,我希望该功能在失败之前最多重试 3 次。因此,我编写了一个测试,它使用模拟(实际上是httpretty)来替换外部 API,并在返回某些内容之前引发 requests.ConnectionError 两次。

这可以正常工作,并且当没有第三次尝试引发异常时测试通过,但是引发(有意)并由代码捕获和处理的两个异常被打印到控制台,从而污染了测试输出。有没有一种干净的方法来阻止这种情况发生?

更多信息

这是我正在测试的方法

控制台输出为:

0 投票
1 回答
1101 浏览

python - 如何获得不是 HttPretty 中最后一个请求的请求?

使用 Python 的HTTPretty库,我可以为我的单元测试创​​建模拟 HTTP 响应。当我正在测试的代码运行时,而不是我的请求到达第三方,请求被拦截并且我的代码接收到我配置的响应。

然后我使用last_request()并可以检查我的代码请求的 url、任何参数等。

我想知道如何不仅可以访问最后一个请求,还可以访问我的代码在最后一个请求之前发送的任何其他请求。

这似乎是可能的。在文档中,它使用了一个名为latest_requests. 例如这里

但这似乎对我不起作用。我得到一个 AttributeErrorAttributeError: module 'httpretty' has no attribute 'latest_requests'

这是一些代码,说明了我正在尝试做的事情以及我在哪里得到 AttributeError

谢谢!!

0 投票
0 回答
100 浏览

python - Httpretty导致套接字失去连接?

我正在使用 Httpretty 来模拟 Web API。

如果我使用httpretty.enable(allow_net_connect=False)(当 allow_net_connect 为 False 时,任何与未注册 uri 的连接都会抛出 httpretty.errors.UnmockedError),我会收到一个错误,因为 pymysql 无法通过套接字连接到本地数据库。

但是,当我使用 时httpretty.enable(allow_net_connect=True),我从丢失的连接中得到一个错误。

如何让 Httpretty 模拟 Web API 并让数据库连接正常运行?

0 投票
0 回答
372 浏览

python - 如何使用`httpretty`模拟请求

这是我尝试编写的测试:

但是,当我使用它运行它时pytest,我得到

查看文档,我以为我已经逐字逐句地遵循了它-有什么建议吗?

0 投票
1 回答
181 浏览

python - 如何让 httpretty 在测试期间停止打印异常?

我有一个模拟来自不存在的远程服务器的请求的测试:

get_webfinger实际上确实抓住了ConnectionError。当它运行时,测试通过——但它也报告ConnectionError异常来自 httpretty 线程:

单元测试或正在测试的代码中没有线程。这是许多类似测试之一。那些不涉及异常的都运行得很好。

我怎样才能让它停止打印异常?