接口自动化项目落地之HTTPBin网站
接口自动化项目落地系列
找个开源网站或开源项目,用tep实现整套pytest接口自动化项目落地,归档到电子书,作为tep完整教程的项目篇一部分。自从tep完整教程发布以后,tep被越来越多小伙伴了解。教程只是纯理论,是骡子是马,拉出来遛遛才知道。做接口自动化项目落地,一方面是为了让自己脑海中的构想实实在在的呈现出来,现实和理想存在多少差距,不断尝试去弥补和修缮;另一方面也是方便读者朋友们学习使用,借助实际项目来练习,才能在赛道中弯道超车。
HTTPBin网站
httpbin.org是一个简单的在线提供HTTP服务的网站:
它能够用来对HTTP进行在线测试。
测试报告
HTTPBin网站的接口自动化项目包含11个用例集:
67条测试用例:
自动化执行正确率98.5%,其中有1条错误结果,是我故意为之的,因为想展示下断言失败的效果。
环境配置
包含http和https两套环境,因为HTTPBin支持HTTP&HTTPS:
fixtures/fixture_env_vars.py
#!/usr/bin/python # encoding=utf-8
from tep.fixture import *
@pytest.fixture(scope="session")
def env_vars(config):
class Clazz(TepVars):
env = config["env"]<span class="token string">""</span><span class="token string">"变量定义开始"</span><span class="token string">""</span> # 环境变量 mapping <span class="token operator">=</span> <span class="token punctuation">{</span> <span class="token string-property property">"http"</span><span class="token operator">:</span> <span class="token punctuation">{</span> # http环境 <span class="token string-property property">"domain"</span><span class="token operator">:</span> <span class="token string">"http://httpbin.org"</span><span class="token punctuation">,</span> <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token string-property property">"https"</span><span class="token operator">:</span> <span class="token punctuation">{</span> # https环境 <span class="token string-property property">"domain"</span><span class="token operator">:</span> <span class="token string">"https://httpbin.org"</span><span class="token punctuation">,</span> <span class="token punctuation">}</span> # 继续添加 <span class="token punctuation">}</span> # 定义类属性,敲代码时会自动补全 domain <span class="token operator">=</span> mapping<span class="token punctuation">[</span>env<span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">"domain"</span><span class="token punctuation">]</span> <span class="token string">""</span><span class="token string">"变量定义结束"</span><span class="token string">""</span> <span class="token keyword">return</span> <span class="token function">Clazz</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><div class="developer-code-block-operation"><button class="developer-code-block-btn">复制</button></div></div><p>配置默认为http环境:</p><p><em style="font-style:italic">conf.yaml</em></p><div class="developer-code-block"><pre class="prism-token token line-numbers language-javascript" tabindex="0"><code class="language-javascript" style="margin-left:0"><span class="token literal-property property">env</span><span class="token operator">:</span> http<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><div class="developer-code-block-operation"><button class="developer-code-block-btn">复制</button></div></div><h2 id="abinr" name="%E7%94%A8%E4%BE%8B%E9%9B%86">用例集</h2><h3 id="3stc7" name="http-methods">http-methods</h3><figure class=""><div class="rno-markdown-img-url" style="text-align:center"><div class="rno-markdown-img-url-inner" style="width:47.45%"><div style="width:100%"><img src="https://ask.qcloudimg.com/http-save/yehe-5545582/9111469d89507106db3134d8945d1581.png"></div></div></div></figure><div class="developer-code-block"><pre class="prism-token token line-numbers language-javascript" tabindex="0"><code class="language-javascript" style="margin-left:0"><span class="token keyword">import</span> allure
from tep.client import request
@allure.title("get请求")
def test(env_vars):
# 描述
# 数据
# 请求
response = request(
"get",
url=env_vars.domain + "/get",
headers={'Host': 'httpbin.org', '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': 'http://httpbin.org/', '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 allure
from tep.client import request
@allure.title("Authorization以Bearer开头,认证成功")
def test(env_vars):
# 描述
# http://httpbin.org/#/Auth/get_basic_auth__user___passwd_
# 数据
# 请求
response = request(
"get",
url=env_vars.domain + "/bearer",
headers={'Host': 'httpbin.org', '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': 'http://httpbin.org/', '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 allure
from tep.client import request
@allure.title("post返回状态码300")
def test(env_vars):
# 描述
# 数据
# 请求
response = request(
"post",
url=env_vars.domain + "/status/300",
headers={'Host': 'httpbin.org', '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': 'http://httpbin.org', 'Referer': 'http://httpbin.org/', '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 allure
from tep.client import request
@allure.title("捕获请求信息--headers")
def test(env_vars):
# 描述
# 数据
# 请求
response = request(
"get",
url=env_vars.domain + "/headers",
headers={'Host': 'httpbin.org', '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': 'http://httpbin.org/', '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 allure
from tep.client import request
@allure.title("捕获响应信息--缓存")
def test(env_vars):
# 描述
# 数据
# 请求
response = request(
"get",
url=env_vars.domain + "/cache",
headers={'Host': 'httpbin.org', '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': 'http://httpbin.org/', '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 allure
from tep.client import request
@allure.title("txt文本text/plain")
def test(env_vars):
# 描述
# 数据
# 请求
response = request(
"get",
url=env_vars.domain + "/robots.txt",
headers={'Host': 'httpbin.org', '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': 'http://httpbin.org/', '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 allure
from tep.client import request
@allure.title("base64解码")
def test(env_vars):
# 描述
# 数据
# 请求
response = request(
"get",
url=env_vars.domain + "/base64/SFRUUEJJTiBpcyBhd2Vzb21l",
headers={'Host': 'httpbin.org', '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': 'http://httpbin.org/', '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 allure
from tep.client import request
@allure.title("cookies")
def test(env_vars):
# 描述
# 数据
# 请求
response = request(
"get",
url=env_vars.domain + "/cookies",
headers={'Host': 'httpbin.org', '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': 'http://httpbin.org/', '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 allure
from tep.client import request
@allure.title("图片")
def test(env_vars):
# 描述
# 数据
# 请求
response = request(
"get",
url=env_vars.domain + "/image",
headers={'Host': 'httpbin.org', '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': 'http://httpbin.org/', '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 allure
from tep.client import request
@allure.title("重定向")
def test(env_vars):
# 描述
# 数据
# 请求
response = request(
"get",
url=env_vars.domain + "/redirect/1",
headers={'Host': 'httpbin.org', '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': 'http://httpbin.org/', '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 allure
from tep.client import request
@allure.title("返回所有数据")
def test(env_vars):
# 描述
# 数据
# 请求
response = request(
"delete",
url=env_vars.domain + "/anything",
headers={'Host': 'httpbin.org', '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 <span class="token operator">=</span> <span class="token function">request</span><span class="token punctuation">(</span>
<span class="token string">"get"</span><span class="token punctuation">,</span>
url<span class="token operator">=</span>env_vars<span class="token punctuation">.</span>domain <span class="token operator">+</span> <span class="token string">"/anything"</span><span class="token punctuation">,</span>
headers<span class="token operator">=</span><span class="token punctuation">{</span><span class="token string-property property">'Host'</span><span class="token operator">:</span> <span class="token string">'httpbin.org'</span><span class="token punctuation">,</span> <span class="token string-property property">'Proxy-Connection'</span><span class="token operator">:</span> <span class="token string">'keep-alive'</span><span class="token punctuation">,</span> <span class="token string-property property">'accept'</span><span class="token operator">:</span> <span class="token string">'application/json'</span><span class="token punctuation">,</span>
<span class="token string-property property">'User-Agent'</span><span class="token operator">:</span> <span class="token string">'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'</span><span class="token punctuation">,</span>
<span class="token string-property property">'Referer'</span><span class="token operator">:</span> <span class="token string">'/'</span><span class="token punctuation">,</span> <span class="token string-property property">'Accept-Encoding'</span><span class="token operator">:</span> <span class="token string">'gzip, deflate'</span><span class="token punctuation">,</span>
<span class="token string-property property">'Accept-Language'</span><span class="token operator">:</span> <span class="token string">'en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7'</span><span class="token punctuation">,</span>
<span class="token string-property property">'Cookie'</span><span class="token operator">:</span> <span class="token string">'stale_after=never; fake=fake_value; freeform=3; name=dongfanger'</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
params<span class="token operator">=</span><span class="token punctuation">{</span><span class="token punctuation">}</span>
<span class="token punctuation">)</span>
# 提取
# 断言
assert response<span class="token punctuation">.</span>status_code <span class="token operator"><</span> <span class="token number">400</span>
# 描述
# 数据
# 请求
response <span class="token operator">=</span> <span class="token function">request</span><span class="token punctuation">(</span>
<span class="token string">"patch"</span><span class="token punctuation">,</span>
url<span class="token operator">=</span>env_vars<span class="token punctuation">.</span>domain <span class="token operator">+</span> <span class="token string">"/anything"</span><span class="token punctuation">,</span>
headers<span class="token operator">=</span><span class="token punctuation">{</span><span class="token string-property property">'Host'</span><span class="token operator">:</span> <span class="token string">'httpbin.org'</span><span class="token punctuation">,</span> <span class="token string-property property">'Proxy-Connection'</span><span class="token operator">:</span> <span class="token string">'keep-alive'</span><span class="token punctuation">,</span> <span class="token string-property property">'accept'</span><span class="token operator">:</span> <span class="token string">'application/json'</span><span class="token punctuation">,</span>
<span class="token string-property property">'User-Agent'</span><span class="token operator">:</span> <span class="token string">'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'</span><span class="token punctuation">,</span>
<span class="token string-property property">'Origin'</span><span class="token operator">:</span> <span class="token string">''</span><span class="token punctuation">,</span> <span class="token string-property property">'Referer'</span><span class="token operator">:</span> <span class="token string">'/'</span><span class="token punctuation">,</span> <span class="token string-property property">'Accept-Encoding'</span><span class="token operator">:</span> <span class="token string">'gzip, deflate'</span><span class="token punctuation">,</span>
<span class="token string-property property">'Accept-Language'</span><span class="token operator">:</span> <span class="token string">'en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7'</span><span class="token punctuation">,</span>
<span class="token string-property property">'Cookie'</span><span class="token operator">:</span> <span class="token string">'stale_after=never; fake=fake_value; freeform=3; name=dongfanger'</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
json<span class="token operator">=</span><span class="token punctuation">{</span><span class="token punctuation">}</span>
<span class="token punctuation">)</span>
# 提取
# 断言
assert response<span class="token punctuation">.</span>status_code <span class="token operator"><</span> <span class="token number">400</span>
# 描述
# 数据
# 请求
response <span class="token operator">=</span> <span class="token function">request</span><span class="token punctuation">(</span>
<span class="token string">"post"</span><span class="token punctuation">,</span>
url<span class="token operator">=</span>env_vars<span class="token punctuation">.</span>domain <span class="token operator">+</span> <span class="token string">"/anything"</span><span class="token punctuation">,</span>
headers<span class="token operator">=</span><span class="token punctuation">{</span><span class="token string-property property">'Host'</span><span class="token operator">:</span> <span class="token string">'httpbin.org'</span><span class="token punctuation">,</span> <span class="token string-property property">'Proxy-Connection'</span><span class="token operator">:</span> <span class="token string">'keep-alive'</span><span class="token punctuation">,</span> <span class="token string-property property">'Content-Length'</span><span class="token operator">:</span> <span class="token string">'0'</span><span class="token punctuation">,</span>
<span class="token string-property property">'accept'</span><span class="token operator">:</span> <span class="token string">'application/json'</span><span class="token punctuation">,</span>
<span class="token string-property property">'User-Agent'</span><span class="token operator">:</span> <span class="token string">'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'</span><span class="token punctuation">,</span>
<span class="token string-property property">'Origin'</span><span class="token operator">:</span> <span class="token string">''</span><span class="token punctuation">,</span> <span class="token string-property property">'Referer'</span><span class="token operator">:</span> <span class="token string">'/'</span><span class="token punctuation">,</span> <span class="token string-property property">'Accept-Encoding'</span><span class="token operator">:</span> <span class="token string">'gzip, deflate'</span><span class="token punctuation">,</span>
<span class="token string-property property">'Accept-Language'</span><span class="token operator">:</span> <span class="token string">'en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7'</span><span class="token punctuation">,</span>
<span class="token string-property property">'Cookie'</span><span class="token operator">:</span> <span class="token string">'stale_after=never; fake=fake_value; freeform=3; name=dongfanger'</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
json<span class="token operator">=</span><span class="token punctuation">{</span><span class="token punctuation">}</span>
<span class="token punctuation">)</span>
# 提取
# 断言
assert response<span class="token punctuation">.</span>status_code <span class="token operator"><</span> <span class="token number">400</span>
# 描述
# 数据
# 请求
response <span class="token operator">=</span> <span class="token function">request</span><span class="token punctuation">(</span>
<span class="token string">"put"</span><span class="token punctuation">,</span>
url<span class="token operator">=</span>env_vars<span class="token punctuation">.</span>domain <span class="token operator">+</span> <span class="token string">"/anything"</span><span class="token punctuation">,</span>
headers<span class="token operator">=</span><span class="token punctuation">{</span><span class="token string-property property">'Host'</span><span class="token operator">:</span> <span class="token string">'httpbin.org'</span><span class="token punctuation">,</span> <span class="token string-property property">'Proxy-Connection'</span><span class="token operator">:</span> <span class="token string">'keep-alive'</span><span class="token punctuation">,</span> <span class="token string-property property">'Content-Length'</span><span class="token operator">:</span> <span class="token string">'0'</span><span class="token punctuation">,</span>
<span class="token string-property property">'accept'</span><span class="token operator">:</span> <span class="token string">'application/json'</span><span class="token punctuation">,</span>
<span class="token string-property property">'User-Agent'</span><span class="token operator">:</span> <span class="token string">'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'</span><span class="token punctuation">,</span>
<span class="token string-property property">'Origin'</span><span class="token operator">:</span> <span class="token string">''</span><span class="token punctuation">,</span> <span class="token string-property property">'Referer'</span><span class="token operator">:</span> <span class="token string">'/'</span><span class="token punctuation">,</span> <span class="token string-property property">'Accept-Encoding'</span><span class="token operator">:</span> <span class="token string">'gzip, deflate'</span><span class="token punctuation">,</span>
<span class="token string-property property">'Accept-Language'</span><span class="token operator">:</span> <span class="token string">'en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7'</span><span class="token punctuation">,</span>
<span class="token string-property property">'Cookie'</span><span class="token operator">:</span> <span class="token string">'stale_after=never; fake=fake_value; freeform=3; name=dongfanger'</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
json<span class="token operator">=</span><span class="token punctuation">{</span><span class="token punctuation">}</span>
<span class="token punctuation">)</span>
# 提取
# 断言
assert response<span class="token punctuation">.</span>status_code <span class="token operator"><</span> <span class="token number">400</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><div class="developer-code-block-operation"><button class="developer-code-block-btn">复制</button></div></div><h2 id="8gi40" name="%E5%8F%AA%E8%8A%B1%E4%BA%863%E5%B0%8F%E6%97%B6%E5%AE%8C%E6%88%90">只花了3小时完成</h2><p>通过mitmproxy来录制流量自动生成用例,效率得到了极大的提高,从原来的1天缩短到3小时就完成了整个项目落地。相比于手工编写用例,这次写HTTPBin的接口自动化,我使用了<code>utils/mitm.py</code>来录制流量,mitmproxy稍微不方便的是需要手动开启代理,不过适应了以后还是能接受。录制流量后就会生成自动化用例,但是还需要二次修改,才会变成最终的用例。主要修改的工作量是在添加断言,根据业务设置合理的断言。其次是替换url为<code>env_vars.domain + "/api"</code>拼接方式,直接批量Replace即可。然后就是修改文件名和<code>@allure.title</code>了,给用例加上标题。工欲善其事,必先利其器。</p><h2 id="cv25e" name="tep%E5%85%B1%E5%BB%BA">tep共建</h2><p>欢迎添加微信:<strong>cekaigang</strong>,分享交流tep实践案例,可以提供开源项目我来写,也可以写好后发我一起看看,优秀的项目会添加到tep完整教程的项目篇,以便更多测试同行们借鉴,大佬们赶快来加入我们吧。</p><blockquote><p> 参考资料:
HTTPBin接口自动化项目源码 https://github.com/dongfanger/httpbin
postman https://www.postman.com/postman/workspace/httpbin/collection/
原文链接:https://cloud.tencent.com/developer/article/1997535
标签:zh,en,httpbin,落地,接口,env,org,HTTPBin,response
From: https://www.cnblogs.com/sunny3158/p/18092076