接口自动化项目落地之HTTPBin网站

网友投稿 310 2022-09-05


接口自动化项目落地之HTTPBin网站

接口自动化项目落地系列

找个开源网站或开源项目,用tep实现整套pytest接口自动化项目落地,归档到电子书,作为tep完整教程的项目篇一部分。自从tep完整教程发布以后,tep被越来越多小伙伴了解。教程只是纯理论,是骡子是马,拉出来遛遛才知道。做接口自动化项目落地,一方面是为了让自己脑海中的构想实实在在的呈现出来,现实和理想存在多少差距,不断尝试去弥补和修缮;另一方面也是方便读者朋友们学习使用,借助实际项目来练习,才能在赛道中弯道超车。

HTTPBin网站

encoding=utf-8from tep.fixture import *@pytest.fixture(scope="session")def env_vars(config): class Clazz(TepVars): env = config["env"] """变量定义开始""" # 环境变量 mapping = { "{ # "domain": " }, "{ # "domain": " } # 继续添加 } # 定义类属性,敲代码时会自动补全 domain = mapping[env]["domain"] """变量定义结束""" return Clazz()

配置默认为allurefrom tep.client import request@allure.title("get请求")def test(env_vars): # 描述 # 数据 # 请求 response = request( "get", url=env_vars.domain + "/get", headers={'Host': ''Proxy-Connection': 'keep-alive', 'accept': 'application/json', 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.109 Safari/537.36', 'Referer': ''Accept-Encoding': 'gzip, deflate', 'Accept-Language': 'en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7'}, params={} ) # 提取 # 断言 assert response.status_code < 400

auth

import allurefrom tep.client import request@allure.title("Authorization以Bearer开头,认证成功")def test(env_vars): # 描述 # # 数据 # 请求 response = request( "get", url=env_vars.domain + "/bearer", headers={'Host': ''Proxy-Connection': 'keep-alive', 'accept': 'application/json', 'Authorization': 'Bearer ZG9uZ2ZhbmdlcjoxMjM0NTY=', # 替换token 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.109 Safari/537.36', 'Referer': ''Accept-Encoding': 'gzip, deflate', 'Accept-Language': 'en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7'}, params={} ) # 提取 # 断言 assert response.status_code < 400

status-codes

import allurefrom tep.client import request@allure.title("post返回状态码300")def test(env_vars): # 描述 # 数据 # 请求 response = request( "post", url=env_vars.domain + "/status/300", headers={'Host': ''Proxy-Connection': 'keep-alive', 'Content-Length': '0', 'accept': 'text/plain', 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.109 Safari/537.36', 'Origin': ''Referer': ''Accept-Encoding': 'gzip, deflate', 'Accept-Language': 'en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7', 'Cookie': 'stale_after=never; fake=fake_value'}, json={} ) # 提取 # 断言 assert response.status_code == 300

request_inspection

import allurefrom tep.client import request@allure.title("捕获请求信息--headers")def test(env_vars): # 描述 # 数据 # 请求 response = request( "get", url=env_vars.domain + "/headers", headers={'Host': ''Proxy-Connection': 'keep-alive', 'accept': 'application/json', 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.109 Safari/537.36', 'Referer': ''Accept-Encoding': 'gzip, deflate', 'Accept-Language': 'en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7', 'Cookie': 'stale_after=never; fake=fake_value'}, params={} ) # 提取 # 断言 assert response.status_code < 400 assert response.json()["headers"]

response_inspection

import allurefrom tep.client import request@allure.title("捕获响应信息--缓存")def test(env_vars): # 描述 # 数据 # 请求 response = request( "get", url=env_vars.domain + "/cache", headers={'Host': ''Proxy-Connection': 'keep-alive', 'Cache-Control': 'max-age=0', 'accept': 'application/json', 'If-None-Match': '1', 'If-Modified-Since': '1', 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.109 Safari/537.36', 'Referer': ''Accept-Encoding': 'gzip, deflate', 'Accept-Language': 'en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7', 'Cookie': 'stale_after=never; fake=fake_value'}, params={} ) # 提取 # 断言 assert response.status_code == 304

response_formats

import allurefrom tep.client import request@allure.title("txt文本text/plain")def test(env_vars): # 描述 # 数据 # 请求 response = request( "get", url=env_vars.domain + "/robots.txt", headers={'Host': ''Proxy-Connection': 'keep-alive', 'accept': 'text/plain', 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.109 Safari/537.36', 'Referer': ''Accept-Encoding': 'gzip, deflate', 'Accept-Language': 'en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7', 'Cookie': 'stale_after=never; fake=fake_value'}, params={} ) # 提取 # 断言 assert response.status_code < 400 assert response.headers["content-type"] == "text/plain"

dynamic_data

import allurefrom tep.client import request@allure.title("base64解码")def test(env_vars): # 描述 # 数据 # 请求 response = request( "get", url=env_vars.domain + "/base64/SFRUUEJJTiBpcyBhd2Vzb21l", headers={'Host': ''Proxy-Connection': 'keep-alive', 'accept': 'text/html', 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.109 Safari/537.36', 'Referer': ''Accept-Encoding': 'gzip, deflate', 'Accept-Language': 'en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7', 'Cookie': 'stale_after=never; fake=fake_value'}, params={} ) # 提取 # 断言 assert response.status_code < 400 assert "HTTPBIN is awesome" == response.text

cookies

import allurefrom tep.client import request@allure.title("cookies")def test(env_vars): # 描述 # 数据 # 请求 response = request( "get", url=env_vars.domain + "/cookies", headers={'Host': ''Proxy-Connection': 'keep-alive', 'accept': 'application/json', 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.109 Safari/537.36', 'Referer': ''Accept-Encoding': 'gzip, deflate', 'Accept-Language': 'en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7', 'Cookie': 'stale_after=never; fake=fake_value'}, params={} ) # 提取 # 断言 assert response.status_code < 400 assert response.json()["cookies"]

images

import allurefrom tep.client import request@allure.title("图片")def test(env_vars): # 描述 # 数据 # 请求 response = request( "get", url=env_vars.domain + "/image", headers={'Host': ''Proxy-Connection': 'keep-alive', 'accept': 'image/webp', 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.109 Safari/537.36', 'Referer': ''Accept-Encoding': 'gzip, deflate', 'Accept-Language': 'en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7', 'Cookie': 'stale_after=never; fake=fake_value; freeform=3; name=dongfanger'}, params={} ) # 提取 # 断言 assert response.status_code < 400

redirects

import allurefrom tep.client import request@allure.title("重定向")def test(env_vars): # 描述 # 数据 # 请求 response = request( "get", url=env_vars.domain + "/redirect/1", headers={'Host': ''Proxy-Connection': 'keep-alive', 'accept': 'text/html', 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.109 Safari/537.36', 'Referer': ''Accept-Encoding': 'gzip, deflate', 'Accept-Language': 'en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7', 'Cookie': 'stale_after=never; fake=fake_value; freeform=3; name=dongfanger'}, params={} ) # 提取 # 断言 assert response.status_code == 404

anything

import allurefrom tep.client import request@allure.title("返回所有数据")def test(env_vars): # 描述 # 数据 # 请求 response = request( "delete", url=env_vars.domain + "/anything", headers={'Host': ''Proxy-Connection': 'keep-alive', 'accept': 'application/json', 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.109 Safari/537.36', 'Origin': '', 'Referer': '/', 'Accept-Encoding': 'gzip, deflate', 'Accept-Language': 'en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7', 'Cookie': 'stale_after=never; fake=fake_value; freeform=3; name=dongfanger'}, json={} ) # 提取 # 断言 assert response.status_code < 400 # 描述 # 数据 # 请求 response = request( "get", url=env_vars.domain + "/anything", headers={'Host': ''Proxy-Connection': 'keep-alive', 'accept': 'application/json', 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.109 Safari/537.36', 'Referer': '/', 'Accept-Encoding': 'gzip, deflate', 'Accept-Language': 'en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7', 'Cookie': 'stale_after=never; fake=fake_value; freeform=3; name=dongfanger'}, params={} ) # 提取 # 断言 assert response.status_code < 400 # 描述 # 数据 # 请求 response = request( "patch", url=env_vars.domain + "/anything", headers={'Host': ''Proxy-Connection': 'keep-alive', 'accept': 'application/json', 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.109 Safari/537.36', 'Origin': '', 'Referer': '/', 'Accept-Encoding': 'gzip, deflate', 'Accept-Language': 'en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7', 'Cookie': 'stale_after=never; fake=fake_value; freeform=3; name=dongfanger'}, json={} ) # 提取 # 断言 assert response.status_code < 400 # 描述 # 数据 # 请求 response = request( "post", url=env_vars.domain + "/anything", headers={'Host': ''Proxy-Connection': 'keep-alive', 'Content-Length': '0', 'accept': 'application/json', 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.109 Safari/537.36', 'Origin': '', 'Referer': '/', 'Accept-Encoding': 'gzip, deflate', 'Accept-Language': 'en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7', 'Cookie': 'stale_after=never; fake=fake_value; freeform=3; name=dongfanger'}, json={} ) # 提取 # 断言 assert response.status_code < 400 # 描述 # 数据 # 请求 response = request( "put", url=env_vars.domain + "/anything", headers={'Host': ''Proxy-Connection': 'keep-alive', 'Content-Length': '0', 'accept': 'application/json', 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.109 Safari/537.36', 'Origin': '', 'Referer': '/', 'Accept-Encoding': 'gzip, deflate', 'Accept-Language': 'en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7', 'Cookie': 'stale_after=never; fake=fake_value; freeform=3; name=dongfanger'}, json={} ) # 提取 # 断言 assert response.status_code < 400

只花了3小时完成

通过mitmproxy来录制流量自动生成用例,效率得到了极大的提高,从原来的1天缩短到3小时就完成了整个项目落地。相比于手工编写用例,这次写HTTPBin的接口自动化,我使用了​​utils/mitm.py​​​来录制流量,mitmproxy稍微不方便的是需要手动开启代理,不过适应了以后还是能接受。录制流量后就会生成自动化用例,但是还需要二次修改,才会变成最终的用例。主要修改的工作量是在添加断言,根据业务设置合理的断言。其次是替换url为​​env_vars.domain + "/api"​​​拼接方式,直接批量Replace即可。然后就是修改文件名和​​@allure.title​​了,给用例加上标题。工欲善其事,必先利其器。

tep共建

参考资料:HTTPBin接口自动化项目源码 ​​​​https://postman.com/postman/workspace/httpbin/collection/​​


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Java中classpath讲解及使用方式
下一篇:民间流转的pytest项目结构设计(pytest框架设计)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~