用于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")
所有方法(get
,post
,put
,patch
,delete
,head
)都支持以下参数:
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请求并交互式地编写测试。
待办事项:描述如何使日志记录静音。