问题描述
我阅读了有关测试驱动开发的官方教程,但是对于我来说,它并不是很有帮助.我已经编写了一个小型库,该库大量使用了twisted.web.client.Agent
及其子类(例如,BrowserLikeRedirectAgent
),但是我一直在努力使本教程的代码适应我自己的测试用例.
I read the official tutorial on test-driven development, but it hasn't been very helpful in my case. I've written a small library that makes extensive use of twisted.web.client.Agent
and its subclasses (BrowserLikeRedirectAgent
, for instance), but I've been struggling in adapting the tutorial's code to my own test cases.
我看过twisted.web.test.test_web
,但是我不明白如何使所有部分组合在一起.例如,按照官方教程,我仍然不知道如何从Agent
获取Protocol
对象
I had a look at twisted.web.test.test_web
, but I don't understand how to make all the pieces fit together. For instance, I still have no idea how to get a Protocol
object from an Agent
, as per the official tutorial
有人可以告诉我如何为依赖于GET
和POST
数据的代理编写一些简单测试的代码吗?任何其他详细信息或建议,我们都非常欢迎...
Can anybody show me how to write a simple test for some code that relies on Agent to GET
and POST
data? Any additional details or advice is most welcome...
非常感谢!
推荐答案
如何使用@inlineCallbacks
使生活更简单(即代码更具可读性).
How about making life simpler (i.e. code more readable) by using @inlineCallbacks
.
实际上,我什至会建议不要直接使用Deferred
,除非对性能或特定用例绝对必要,而始终坚持使用@inlineCallbacks
-这样,您可以使代码看起来像普通代码,同时受益于非阻塞行为:
In fact, I'd even go as far as to suggest staying away from using Deferred
s directly, unless absolutely necessary for performance or in a specific use case, and instead always sticking to @inlineCallbacks
—this way you'll keep your code looking like normal code, while benefitting from non-blocking behavior:
from twisted.internet import reactor
from twisted.web.client import Agent
from twisted.internet.defer import inlineCallbacks
from twisted.trial import unittest
from twisted.web.http_headers import Headers
from twisted.internet.error import DNSLookupError
class SomeTestCase(unittest.TestCase):
@inlineCallbacks
def test_smth(self):
ag = Agent(reactor)
response = yield ag.request('GET', 'http://example.com/', Headers({'User-Agent': ['Twisted Web Client Example']}), None)
self.assertEquals(response.code, 200)
@inlineCallbacks
def test_exception(self):
ag = Agent(reactor)
try:
yield ag.request('GET', 'http://exampleeee.com/', Headers({'User-Agent': ['Twisted Web Client Example']}), None)
except DNSLookupError:
pass
else:
self.fail()
试用应该照顾其余的问题(即等待测试功能返回的Deferred
(@inlineCallbacks
包裹的可调用对象也神奇地"返回了Deferred
—我强烈建议您阅读有关@inlineCallbacks
的更多内容)如果您还不熟悉).
Trial should take care of the rest (i.e. waiting on the Deferred
s returned from the test functions (@inlineCallbacks
-wrapped callables also "magically" return a Deferred
—I strongly suggest reading more on @inlineCallbacks
if you're not familiar with it yet).
P.S.还有一个用于鼻子测试的Twisted插件",使您能够从测试函数中返回Deferred
,并鼻子等待直到它们被激发才退出: http://nose.readthedocs.org/en/latest/api/twistedtools.html
P.S. there's also a Twisted "plugin" for nosetests that enables you to return Deferred
s from your test functions and have nose wait until they are fired before exiting: http://nose.readthedocs.org/en/latest/api/twistedtools.html
这篇关于如何使用twisted.web.client.Agent及其子类编写代码测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!