用于api测试的python框架

apiritif的Python项目详细描述


apiritif

apiritif是一些旨在简化api测试维护过程的实用程序。

功能列表:

  • 用于发出http请求的实用程序
  • 对他们的断言
  • 交易

概述

http请求

apiritif允许使用类似于requests的简单api来发出http请求。

fromapiritifimporthttpresponse=http.get("http://example.com")response.assert_ok()# will raise AssertionError if request wasn't successful

http对象提供以下方法:

fromapiritifimporthttphttp.get("http://api.example.com/posts")http.post("http://api.example.com/posts")http.put("http://api.example.com/posts/1")http.patch("http://api.example.com/posts/1")http.delete("http://api.example.com/posts/1")http.head("http://api.example.com/posts")

所有方法(getpostputpatchdeletehead)都支持以下参数:

defget(address,# URL for the requestparams=None,# URL params dictheaders=None,# HTTP headerscookies=None,# request cookiesdata=None,# raw request datajson=None,# attach JSON object as request bodyallow_redirects=True,# automatically follow HTTP redirectstimeout=30)# request timeout, by default it's 30 seconds

http目标

目标是捕获URL资源名称(协议、域、端口)的对象。 并允许设置应用于为目标发出的所有请求的某些设置。

fromapiritifimporthttpqa_env=http.target("http://192.160.0.2")qa_env.get("/api/v4/user")qa_env.get("/api/v4/user")

目标构造函数支持以下选项:

target=apiritif.http.target(address,# target base addressbase_path=None,# base path prepended to all paths (e.g. '/api/v2')use_cookies=True,# use cookiesadditional_headers=None,# additional headers for all requestskeep_alive=True,# reuse opened HTTP connectionauto_assert_ok=True,# automatically invoke 'assert_ok' after each request)

断言

apiritif响应提供了许多可用于响应的有用断言。

下面是可以使用的断言列表:

response=http.get("http://example.com/")# assert that request succeeded (status code is 2xx or 3xx)response.assert_ok()# assert that request has failedresponse.assert_failed()# status code based assertionsresponse.assert_2xx()response.assert_3xx()response.assert_4xx()response.assert_5xx()response.assert_status_code(code)response.assert_not_status_code(code)# content-based assertions# assert that response body contains a stringresponse.assert_in_body(member)# assert that response body doesn't contain a stringresponse.assert_not_in_body(member)# search (or match) response body with a regexresponse.assert_regex_in_body(regex,match=False)response.assert_regex_not_in_body(regex,match=False)# assert that response has headerresponse.assert_has_header(header)# assert that response has header with given valueresponse.assert_header_value(header,value)# assert that response's headers contains a stringresponse.assert_in_headers(member)response.assert_not_in_headers(member)# search (or match) response body with a regexresponse.assert_regex_in_headers(member)response.assert_regex_not_in_headers(member)# assert that response body matches JSONPath queryresponse.assert_jsonpath(jsonpath_query,expected_value=None)response.assert_not_jsonpath(jsonpath_query)# assert that response body matches XPath queryresponse.assert_xpath(xpath_query,parser_type='html',validate=False)response.assert_not_xpath(xpath_query,parser_type='html',validate=False)

请注意,断言可以被链接,因此以下构造是完全有效的:

response=http.get("http://example.com/")response.assert_ok().assert_in_body("Example")

交易

apiritif允许使用transaction上下文管理器将多个请求或操作分组到一个事务中。

withtransaction("group"):http.get("https://blazedemo.com/first").assert_ok()http.get("https://blazedemo.com/second").assert_ok()

事务定义测试的名称。这将显示在火焰表报告中。

with apiritif.transaction('My Test Name'):
  response = target.get(self.url, headers=headers)
  response.assert_2xx()

金牛座整合

托多:描述一下金牛座可以提取Apiritif的动作日志并处理它。

日志记录

TOdo:描述apiritif创建的“apiritif”记录器可用于 调试http请求并交互式地编写测试。

待办事项:描述如何使日志记录静音。

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

推荐PyPI第三方库


热门话题
java如何在log4j中禁用调试?   java高级数据库修改“脚本”怎么办   java使JSlider thumb在单击track时出现   java中的数组几何布朗运动   java netty重试连接客户端将冻结   用户界面如何在Java中设置JButton的背景色?   java远程访问ejabberd   java是同步eclipse开发人员环境和集成环境的最佳方法?   Java中带有任意返回类型和Jackson序列化程序的json泛型方法   java ViewPager片段目标获取活动片段上下文   java一次从JavaFX TableView中删除多行的问题   将ArrayList对象分配给实例变量java   将参数从http头检索到java   java如何使用netbeans使用数据库语句计数   java如何在倒计时内更改txtcolor   java如何在Eclipse中查找不在工作区中的文件中的文本?