如何对使用twisted.web.client.Agent 发出HTTP 请求的代码进行单元测试?

编程入门 行业动态 更新时间:2024-10-26 15:17:10
本文介绍了如何对使用twisted.web.client.Agent 发出HTTP 请求的代码进行单元测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我正在寻找说明如何使用twisted.web.client.Agent 对发出HTTP 请求的代码进行单元测试的提示或示例.

I'm looking for hints or examples that illustrate how to unit test code that makes HTTP requests using twisted.web.client.Agent.

是否可以在测试中使用真实的代理并将其配置为使用 StringTransport(没有真正的 TCP 连接)连接到虚假的 HTTP 服务器?

Is it possible to use the real Agent in tests and configure it to connect to a fake HTTP server using StringTransport (no real TCP connetion)?

或者模拟出代理并将模拟注入使用代理的类是否更好?

Or is it better to mock out the Agent and inject a mock into classes that use the Agent?

推荐答案

当然可以使用真正的 Agent.您必须使用反应器作为第一个参数来构造 Agent 实例;因此,您可以提供一个反应器,例如 MemoryReactor.尽管这是深入了解 Agent 正在做什么的一种非常方便的方法,但它对于测试 Agent 本身确实非常有用.注入一个伪造的 Agent 是一种更简单的方法,因为它的 api 很浅(几乎所有东西都通过 Agent.request 传递,而且很简单(它返回一个 IResponse)

it is certainly possible to use a real Agent. You must construct the Agent instance with a reactor as first argument; as such, you can provide a fake reactor, such as MemoryReactor. Although that's a pretty handy way to get down in the guts of what Agent is doing, it's really mostly useful for testing Agent itself. Injecting a fake Agent is many times an easier route, because its api is both shallow (almost everything passes through Agent.request and simple (it returns an IResponse)

这篇关于如何对使用twisted.web.client.Agent 发出HTTP 请求的代码进行单元测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-29 16:22:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1192065.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:单元测试   代码   web   twisted   client

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!