问题标签 [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 回答
989 浏览

python - 是否有处理分号的 parse_qs 的替代方法?

TL;博士

哪些库/调用可用于处理包含与 parse_qs 不同的分号的查询字符串?

完整背景

我正在使用 StackExchange API 来搜索标记的问题。

搜索的布局是这样的,标签用分号分隔:

/2.1/search?order=desc&sort=activity&tagged=python;ruby&site=stackoverflow

与 API 交互就好了。当我想测试调用时,问题就出现了,特别是在使用httpretty模拟 HTTP 时。

在引擎盖下,httpretty使用urlparse.parse_qspython 标准库来解析查询字符串。

显然这并不好。这是一个小例子,这里是 httpretty 的一个片段(在测试上下文之外)。

我想使用 httpretty 的机器,但需要一个解决方法parse_qs。我现在可以修补 httpretty,但很想看看还能做什么。

0 投票
1 回答
634 浏览

python - HTTPretty 未请求如何声明 URL

在 Python 单元测试中使用 HTTPretty 拦截 HTTP 请求时,如何断言从未请求过目标 URL?

0 投票
1 回答
327 浏览

python - httpretty.enable() 之后的 MongoClient ConnectionFailure

每当我启用 HTTPretty 时,我都无法与 PyMongo 建立连接。我知道 HTTPretty 改变了核心套接字模块;有没有办法解决?

代码示例:

引发异常:

我在使用 Python 3.3 的 Windows 8.1 上。

谁能解释这种行为以及如何解决它?谢谢!

0 投票
1 回答
646 浏览

client - 使用 HTTPretty 模拟 Twisted Web 客户端 HTTP 请求

由于 Httpretty 在 Python 套接字层上工作,因此即使是 Twisted Web 请求也应该被模拟出来。但是我在使用 httpretty 时看到了一些奇怪的行为。它试图以某种方式连接到本地主机。下面的示例显示了差异:

回应是:

我如何将 httpretty 与 Twisted Web 客户端一起使用?

0 投票
1 回答
97 浏览

python - Understanding my own decorator

I have written a decorator that is working correctly but i stumbled with the correct solution by trial and error and my litle knowledge about decorators tells me that something is not well defined.

The case is i'm mocking a Rest Api to do some TDD, and this Rest is behind a token security. So before making any request i first must get my user token. I'm using httpretty for mocking the API.

So far i had to register_uri in every test case, one to mock the /token resource and another to test any other resource. But i found that very cumbersome, so a came with a solution to write a simple decorator that would mock the /token and then only had to mock the tested resource.

This is my currently working decorator...

And this is how is called.

I had to pass the test_case parameter to the inner function 'cause without it it wouldn't work, and that test_case is test_case_one method, which i thought it would be passed in the func argument, but func in the outer scope holds the object at memory of test_case.

Should't be func the returned value of a decorator? If i do that, the decorator doesn't work. When the inner function is passed that parameter?

0 投票
1 回答
3646 浏览

python - 使用 HTTPretty 模拟超时的 HTTP 请求

使用 Python 的HTTPretty 库,我可以创建选择的模拟 HTTP 响应,然后使用requests 库选择它们,如下所示:

出去:<Response [200]>

是否还有可能注册一个无法访问的 uri(例如连接超时、连接被拒绝……),这样根本就没有收到任何响应(这与已建立的连接不同,它会给出 HTTP 错误代码像404)?

我想在单元测试中使用这种行为来确保我的错误处理按预期工作(在“未建立连接”和“建立连接,错误的 HTTP 状态代码”的情况下会做不同的事情)。作为一种解决方法,我可以尝试连接到一个无效的服务器,这样http://192.0.2.0无论如何都会超时。但是,我更愿意在不使用任何真实网络连接的情况下进行所有单元测试。

0 投票
2 回答
975 浏览

python - 如何使用 httpretty/sure 执行 python 测试

我是 python 测试的新手,所以请毫不犹豫地提供任何明显的信息。

基本上我想用 python 做一些 RESTful 测试,发现 httpretty 和 sure 库看起来真的很好。

我有一个 python 文件,其中包含:

这与https://github.com/gabrielfalcao/HTTPretty提供的示例代码基本相同

我的问题是;我如何简单地运行此测试以查看它是通过还是失败?我尝试使用它来执行它,./pythonFile但这不起作用。

0 投票
1 回答
5193 浏览

python - 在 Python 中模拟 Selenium WebDriver 发送的请求,并将虚假响应显示在由驱动程序驱动的浏览器实例中

我目前正在尝试使用 Python 版本的Selenium WebDriver以及Pytest测试框架来对 Web 应用程序进行自动化测试。在我的 Selenium 代码中尝试进行 HTTP 请求模拟时遇到了一个问题。我编写了一个名为“Selenium_WebDriver_Mocking_test.py”的模块,在其中导航到 Python 官方网站并在页面顶部的搜索框中填写搜索词,然后按 Enter 键移动到结果页面。当我不尝试模拟任何东西时,代码可以完美运行。假设您同时安装了 Selenium 和 Pytest,您可以通过键入以下命令从命令行界面运行它:

或键入以下内容:

代码如下所示。

然后,我为一个虚假的结果页面编写了一些 HTML,并尝试使用 Gabriel Falcao 编写的 HTTPretty 工具将该页面作为响应返回,该响应显示在浏览器中,而不是真正的 Python.org 结果页面。修改后的代码如下所示。

这种方法不起作用,我在其中一个测试迭代的日志中收到以下消息:

我还尝试使用 David Cramer 编写的响应工具。虽然这没有返回任何错误,但它也没有做任何事情,并且测试继续进行,就好像没有发生任何嘲笑一样。我的问题是:在 Python 中有没有办法模拟 Selenium WebDriver 发送的请求,并在由驱动程序驱动的浏览器实例中显示假响应主体?任何帮助表示赞赏。

0 投票
1 回答
203 浏览

python - 挂在 Travis 上的 HTTPretty 测试

在使python-intercom Python 3 兼容时,我在Travis上遇到了一个问题。

在 Python 3.4 上运行时,该nosetests命令似乎没有退出(它在 Python 2.7 上的行为与预期一致)。我将其缩小到使用HTTPretty的测试,并创建了一个小项目来突出问题,并发布失败构建的结果:

现在我不知道如何进行。

0 投票
1 回答
752 浏览

python - 将 httpretty 与 pytest tmpdir 结合起来

以下 pytest-test 使用 httpretty 来模拟请求。它将获取的数据写入文件:

我现在想做的是利用pytest tmpdir功能。为了达到这个目的,我写了一个这样的测试(导入与上面相同):

它失败了,因为 httpretty 装饰器似乎有附加参数的问题:

任何想法如何解决这一问题?