单元测试有助于aiohttp

aiohttp_tests的Python项目详细描述


用于基于aiohttp的应用程序的unittest助手实用程序。

示例用法:

importasyncioimporttimefromunittestimportmockimportaiohttp_testsasatfromaiohttpimportwebsettings=mock.Mock()@at.override_settings(settings,SETTINGS_VALUE='new_value')classExampleTestCase(at.BaseTestCase):""" Aiohttp tests utilities example"""@asyncio.coroutinedefsample_view(self,request):text=yield fromself.sample_async_method()returnweb.Response(text=text)@asyncio.coroutinedefsample_async_method(self):yield fromasyncio.sleep(0.1)return'OK'definit_app(self,loop):""" You must initialize aiohttp application correctly."""app=web.Application(loop=loop)app.router.add_route('GET','/',self.sample_view)returnappdefinit_patchers(self):super().init_patchers()# Short alias for mock.patch that stores patcher and mock objects# locally. Patchers are stopped automatically on teardown.# Method is called by BaseTestCase.setUpself.mock('time.time',return_value=self.now)defsetUp(self):self.now=time.time()super().setUp()deftestMockUtils(self):""" Shows usage of get_mock and get_patcher methods."""self.assertEqual(time.time(),self.now)self.get_mock('time.time').return_value=self.now-1self.assertEqual(time.time(),self.now-1)self.get_patcher('time.time').stop()self.assertGreater(time.time(),self.now)deftestOverrideSettings(self):""" Django-style override_settings decorator for any settings module."""self.assertEqual(settings.SETTINGS_VALUE,'new_value')withat.override_settings(settings,SETTINGS_VALUE='other_value'):self.assertEqual(settings.SETTINGS_VALUE,'other_value')deftestSyncClient(self):""" Synchronous execution of requests, with new event loop every time.
        Other HTTP methods, HTTP headers, request body or form/data are also
        supported.
        """response=self.client.get('/',headers={'Accept':'application/json'})self.assertEqual(response.status,200)self.assertEqual(response.text,'OK')self.assertEqual(response.headers.get('content-type'),'text/plain; charset=utf-8')# other HTTP methodsresponse=self.client.post('/',body='POST body')self.assertEqual(response.status,405)# urlencoded form/data also supportedresponse=self.client.request('PUT','/',data={'field':'value'})self.assertEqual(response.status,405)@at.async_testdeftestAsyncClient(self):""" test client requests could be done async, if needed."""# you can mock any coroutine to return a 'done' Future with custom# result.done_future=self.empty_result("async response")# mock.patck.object shortcutself.mock_object(self,'sample_async_method',return_value=done_future)response=yield fromself.client.get('/')self.assertEqual(response.text,"async response")

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

推荐PyPI第三方库


热门话题
Java NIO选择器最多只能选择50个SelectionKeys?   java阻止ImageButton创建新活动   为什么Java方法“Arrays.copyOf”处理中的整数数组与处理对象数组时的行为不同   java将安卓 1.6日历字段提取到vcal   将字符串转换为双精度后的java问题   java使用Webflux,是否可以使用AOP获取用于日志记录的ServerHttpRequest   java与jTextArea的基本区别是什么。setText()和jTextArea。append()方法?   java程序执行时间|将值保存到文件或数据结构?   java如何将表示字符的整数转换为字符串?   Java LWJGL新的渲染问题windows   java GUI提供异常   java创建第二个对象使类的函数不再工作   如何在java中将000验证为3位数字?   java bigO算法的时间复杂度,可以在不重复的情况下找到最长的子串   在30像素内点击按钮后,java在两个图像之间转换   java eclipse maven nexus不可解父pom   当存在其他SecurityConstraint时,java TransportGuary机密将被忽略   重新连接后激发的java Red5客户端流断开事件   javafx聊天应用程序中连接客户端的java问题