falcon框架的pytest“client”fixture

pytest-falcon-client的Python项目详细描述


pytest falcon客户端

用于Falcon Framework的pytestclient夹具。

Build StatusCoverage StatusCode style: blackPython versionsPyPi

安装

pip install pytest-falcon-client

设置

Plugin提供了make_clientfixture,您可以通过将falcon.API实例作为参数按原样使用它:

importpytestfromyout_applicationimportcreate_api@pytest.fixturedefapi():returncreate_api()deftest_something(make_client,api):client=make_client(api)got=client.get('/your_url/42/')assertgot=={'awesome':'response'}

为了更简单的使用,您可以定义自己的client设备:

importpytestfromyout_applicationimportcreate_api@pytest.fixturedefclient(make_client):api=create_api()returnmake_client(api)deftest_something(client):got=client.get('/your_url/42/')assertgot=={'awesome':'response'}

断言示例

将响应体获取为json,并为状态代码

执行自动断言
deftest_something(client):got=client.get('/your_url/42/')assertgot=={'some':'staff'}

获取响应体作为json,并为自己的状态代码执行自动断言

deftest_something(client):got=client.get('/your_url/42/',expected_statuses=[400,401])assertgot=={'some':'stuff'}

按原样获取响应对象并跳过任何自动断言

deftest_something(client):response=client.get('/your_url/42/',as_response=True)assertresponse.status_code==400assertresponse.json=={'some':'stuff'}

每个请求的自定义自动断言

例如,您希望断言每个响应都有 Access-Control-Allow-Origin具有值falconframework.org的头。 您可以使用自定义ApiTestClient来完成此操作,如下所示:

importpytestfrompytest_falcon_clientimportApiTestClientfromyout_applicationimportcreate_apiclassCustomApiTestClient(ApiTestClient):defresponse_assertions(self,response):# You can do any automatic assertions here for every requestassertresponse.headers['Access-Control-Allow-Origin']=='falconframework.org'@pytest.fixturedefclient():api=create_api()returnCustomApiTestClient(api)deftest_something(client):response=client.get('/your_url/42/',as_response=True)assertresponse.status_code==400assertresponse.json=={'some':'stuff'}

模拟一些默认的客户端行为

如果你的服务依赖于一些默认的客户端行为,比如头, 饼干还是别的您可以为每个请求设置此行为 使用自定义ApiTestClient

importpytestfrompytest_falcon_clientimportApiTestClientfromyout_applicationimportcreate_apiclassCustomApiTestClient(ApiTestClient):defprepare_request(self,method,expected_statuses,*args,**kwargs):kwargs['headers']={'Origin':'falconframework.org'}# add `ORIGIN` header to every requestreturnargs,kwargs,expected_statuses# should returns all of this@pytest.fixturedefclient():api=create_api()returnCustomApiTestClient(api)deftest_something(client):got=client.get('/your_url/42/',as_response=True)assertgot=={'some':'stuff'}

请看tests中的更多示例。

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

推荐PyPI第三方库


热门话题
java如何在MVC中更新和查看当前窗口   检查输入是否为正确的java类型   java这个代码可以从数组中打印出随机字符串吗?   编译一起编译Java类   java需要编写一个通用函数,根据用户提供的输入,对地图中的对象进行类型转换   在Java中的DataInputStream中来回移动   java我的应用程序没有正确关闭,我不知道为什么   java在从地图检索数据时获得双方括号   用于解析的正则表达式出现java堆栈溢出错误   spring中使用jdbcTemplate的java批处理更新   JavaJAXB解组模式问题:“org。xml。萨克斯。SAXSparseException cvcelt。1'   java为什么SOAP在Spring Boot中将从客户端发送到服务器的obj数据请求为Null   java无法获取线程。中断工作   用Java发送XMLHttpRequest   java是连接到本地网络的机器列表的jTree   java是实现毕加索的最佳方法。生成器和onImageLoadFailed函数   java spring hibernate使用onetomany保存实体失败   Java拆分字符串位于|   Red Hat Developer Studio 12中的java JDBC驱动程序模板