为测试依赖于异步代码的代码,用一个漂亮的接口截取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测试

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
API调用时返回java Null   JavaJAXB:如何从嵌套元素中检索文本属性?   java如何在Windows机器上用Maven创建Dockerfile,然后发送到远程Linux服务器以构建映像,然后运行?   并行处理集群java独立应用程序   java使用POST将图像上传到服务器   分发组的java JavaMail/Exchange AuthAs标头   swing Java JScrollPane多个组件   JAXB将XML文档映射到Java对象   java如何在JDK中实现OpenCSV CSVReader,我正在使用VSC代码   基于屏幕大小的java翻译动画   在java中比较堆栈元素和对象的搜索   java如何在Swing中更改化身?   java JAXB将1个XML标记映射到2个变量   java面试问题:递归生成素数的最快方法是什么?   JavaEclipseIDE频繁关闭   Java中从像素读取RGB值的图像   通过查询oracle DB列值获得的字符串中的java转义XML特殊字符   java从ArrayList中删除多个项