为测试依赖于异步代码的代码,用一个漂亮的接口截取tornado asynchttpclient.fetch
tornado-stub-client的Python项目详细描述
##Tornado存根客户端
[![构建状态](https://travis-ci.org/venmo/tornado-stub-client.png?branch=master)(https://travis ci.org/venmo/tornado stub client)
一个库,用于在tornado中将异步调用存根到外部服务。准确地说,它去掉了asynchttpclient.fetch(但不需要精确,这是tornado中唯一的异步方法)。它工作得很好,但它是全新的,我还有几个功能要添加。
类似于[httpretty](https://github.com/gabrielfalcao/httpretty),但这只是直接修补'fetch()'方法,而不是python套接字。编写一个新的库要比让httpretty使用tornado异步客户机容易得多。如果有兴趣的话,可以很容易地让它与其他http lib(例如请求或grequest)一起工作,但您可能应该只使用httpretty。
def test_basic(self):
client=asynchttpstubclient()
带存根(“http://example.com”)。和_return(body=“hello there”):
client.fetch(“http://example.com”,self.stop)
response=self.wait()
self.assertequals(response.code,200)
self.assertequals(response.body,“hello there”)
def test_an_async_library(self):
“在本例中,somerestlib是包装
rest api的异步库。它有一个asynchttpclient作为实例变量。
我们想用stub(“http://api.example.com/user/10”)对它进行单元测试。
'twitter\u handle':'@danny cosson'}:
mylib.get_user(10,self.stop)
response_dict=self.wait()
self.assertequals(response_dict.get('name'),'danny cosson')
def test_can_queue_stub responself:
“stub支持循环响应,即,存根将循环通过
排队响应。方便测试处理状态的端点。
“
client=asynchttpstubclient()
使用stub(“/hello”)。然后返回(body=”hello“)\
。然后返回(body=”world“):
client.fetch(“/hello”,self.stop)
response=self.wait()
self.assertequal(response.code,200)
self.assertequal(response.body,“hello”)
client.fetch(“/hello”,self.stop)
response=self.wait()
self.assertequal(response.code,200)
self.assertequal(response.body,“world”)
```
请注意,不必在“with”语句中使用“stub()”和“return()”,但如果不这样做,则创建的存根不会在测试之间被删除,因此应该添加调用“reset()”的setup或teardown方法来清除数据。
\tests
运行测试(可能在virtualenv中是最好的):
$cd tornado stub client
$pip install-r requirements.txt
$pip install nose
$notests测试
[![构建状态](https://travis-ci.org/venmo/tornado-stub-client.png?branch=master)(https://travis ci.org/venmo/tornado stub client)
一个库,用于在tornado中将异步调用存根到外部服务。准确地说,它去掉了asynchttpclient.fetch(但不需要精确,这是tornado中唯一的异步方法)。它工作得很好,但它是全新的,我还有几个功能要添加。
类似于[httpretty](https://github.com/gabrielfalcao/httpretty),但这只是直接修补'fetch()'方法,而不是python套接字。编写一个新的库要比让httpretty使用tornado异步客户机容易得多。如果有兴趣的话,可以很容易地让它与其他http lib(例如请求或grequest)一起工作,但您可能应该只使用httpretty。
def test_basic(self):
client=asynchttpstubclient()
带存根(“http://example.com”)。和_return(body=“hello there”):
client.fetch(“http://example.com”,self.stop)
response=self.wait()
self.assertequals(response.code,200)
self.assertequals(response.body,“hello there”)
def test_an_async_library(self):
“在本例中,somerestlib是包装
rest api的异步库。它有一个asynchttpclient作为实例变量。
我们想用stub(“http://api.example.com/user/10”)对它进行单元测试。
'twitter\u handle':'@danny cosson'}:
mylib.get_user(10,self.stop)
response_dict=self.wait()
self.assertequals(response_dict.get('name'),'danny cosson')
def test_can_queue_stub responself:
“stub支持循环响应,即,存根将循环通过
排队响应。方便测试处理状态的端点。
“
client=asynchttpstubclient()
使用stub(“/hello”)。然后返回(body=”hello“)\
。然后返回(body=”world“):
client.fetch(“/hello”,self.stop)
response=self.wait()
self.assertequal(response.code,200)
self.assertequal(response.body,“hello”)
client.fetch(“/hello”,self.stop)
response=self.wait()
self.assertequal(response.code,200)
self.assertequal(response.body,“world”)
```
请注意,不必在“with”语句中使用“stub()”和“return()”,但如果不这样做,则创建的存根不会在测试之间被删除,因此应该添加调用“reset()”的setup或teardown方法来清除数据。
\tests
运行测试(可能在virtualenv中是最好的):
$cd tornado stub client
$pip install-r requirements.txt
$pip install nose
$notests测试