我测试用的网站是白月黑羽网站
下载地址和测试用例都可以去网站找
系统下载地址:
https://www.byhy.net/prac/pub/info/bysms/
测试用例下载地址:
https://cdn2.byhy.net/files/selenium/testcases.xlsx
参考文档:https://blog.csdn.net/weixin_41665637/article/details/139779579?spm=1001.2014.3001.5501
import requests
# import pytest
def test1_nam(username,password):
url = "http://127.0.0.1/api/mgr/signin"
payload = f"username={username}&password={password}"
headers = {
"Accept": "*/*",
"Accept-Language": "zh-CN,zh;q=0.9",
"Cache-Control": "no-cache",
"Connection": "keep-alive",
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
"Origin": "http://127.0.0.1",
"Pragma": "no-cache",
"Referer": "http://127.0.0.1/mgr/sign.html",
"Sec-Fetch-Dest": "empty",
"Sec-Fetch-Mode": "cors",
"Sec-Fetch-Site": "same-origin",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36",
"X-Requested-With": "XMLHttpRequest",
# "sec-ch-ua": ""Not/A)Brand";v="8", "Chromium";v="126", "Google Chrome";v="126"",
"sec-ch-ua-mobile": "?0",
# "sec-ch-ua-platform": ""Windows"",
"Cookie": "sessionid=5c0lblg1a3pnib9qjv97ady4l10x9wx6",
"content-type": "application/x-www-form-urlencoded"
}
response = requests.request("POST", url, data=payload, headers=headers)
nr=response.json()['msg']
print(nr)
test1_nam(username="",password="88888888")



import requests
import pytest
# 代码初次更新,不引入装饰器
def test_nam():
url = "http://127.0.0.1/api/mgr/signin"
# payload = "username=byhy&password=88888888"
# payload = "username= password=88888888"
payload = "username=byhy&password= "
headers = {
"Accept": "*/*",
"Accept-Language": "zh-CN,zh;q=0.9",
"Cache-Control": "no-cache",
"Connection": "keep-alive",
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
"Origin": "http://127.0.0.1",
"Pragma": "no-cache",
"Referer": "http://127.0.0.1/mgr/sign.html",
"Sec-Fetch-Dest": "empty",
"Sec-Fetch-Mode": "cors",
"Sec-Fetch-Site": "same-origin",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36",
"X-Requested-With": "XMLHttpRequest",
# "sec-ch-ua": ""Not/A)Brand";v="8", "Chromium";v="126", "Google Chrome";v="126"",
"sec-ch-ua-mobile": "?0",
# "sec-ch-ua-platform": ""Windows"",
"Cookie": "sessionid=5c0lblg1a3pnib9qjv97ady4l10x9wx6",
"content-type": "application/x-www-form-urlencoded"
}
response = requests.request("POST", url, data=payload, headers=headers)
# print(response.json()['msg'])
nr=response.json()['msg']
print(nr)
# return nr
assert nr == "用户名或者密码错误"



#代码调用,引入装饰器
import requests
import pytest
@pytest.mark.parametrize("username, password", [("", "88888888"), ("byhy", "")])
def test_nam1(username,password):
url = "http://127.0.0.1/api/mgr/signin"
payload = f"username={username}&password={password}"
headers = {
"Accept": "*/*",
"Accept-Language": "zh-CN,zh;q=0.9",
"Cache-Control": "no-cache",
"Connection": "keep-alive",
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
"Origin": "http://127.0.0.1",
"Pragma": "no-cache",
"Referer": "http://127.0.0.1/mgr/sign.html",
"Sec-Fetch-Dest": "empty",
"Sec-Fetch-Mode": "cors",
"Sec-Fetch-Site": "same-origin",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36",
"X-Requested-With": "XMLHttpRequest",
# "sec-ch-ua": ""Not/A)Brand";v="8", "Chromium";v="126", "Google Chrome";v="126"",
"sec-ch-ua-mobile": "?0",
# "sec-ch-ua-platform": ""Windows"",
"Cookie": "sessionid=5c0lblg1a3pnib9qjv97ady4l10x9wx6",
"content-type": "application/x-www-form-urlencoded"
}
response = requests.request("POST", url, data=payload, headers=headers)
# print(response.json()['msg'])
nr=response.json()['msg']
print(nr)
# return nr
assert nr == "用户名或者密码错误"
# print(nr)
# test_nam(username="",password="88888888")





import requests
import pytest
#代码调用,引入装饰器
@pytest.mark.parametrize('username, password, expectedalert', [
('', '88888888', '用户名或者密码错误'),
('byhy', '', '用户名或者密码错误'),
('byh', '88888888','用户名或者密码错误'),
('byhy', '8888888', '用户名或者密码错误'),
('byhy', '888888888', '用户名或者密码错误'),
]
)
def test_nam(username,password,expectedalert):
url = "http://127.0.0.1/api/mgr/signin"
payload = f"username={username}&password={password}"
headers = {
"Accept": "*/*",
"Accept-Language": "zh-CN,zh;q=0.9",
"Cache-Control": "no-cache",
"Connection": "keep-alive",
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
"Origin": "http://127.0.0.1",
"Pragma": "no-cache",
"Referer": "http://127.0.0.1/mgr/sign.html",
"Sec-Fetch-Dest": "empty",
"Sec-Fetch-Mode": "cors",
"Sec-Fetch-Site": "same-origin",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36",
"X-Requested-With": "XMLHttpRequest",
# "sec-ch-ua": ""Not/A)Brand";v="8", "Chromium";v="126", "Google Chrome";v="126"",
"sec-ch-ua-mobile": "?0",
# "sec-ch-ua-platform": ""Windows"",
"Cookie": "sessionid=5c0lblg1a3pnib9qjv97ady4l10x9wx6",
"content-type": "application/x-www-form-urlencoded"
}
response = requests.request("POST", url, data=payload, headers=headers)
# print(response.json()['msg'])
nr=response.json()['msg']
print(nr)
# return nr
assert nr == expectedalert






备注:时间长未更新用例,目前都会提示:用户名或者密码错误

https://cdn2.byhy.net/files/apitest/testcases-api.xlsx
白月黑羽地址:
https://www.byhy.net/auto/apitest/04/


视频讲解
https://www.bilibili.com/video/BV1qX4y167vG/
响应代码
import requests
import pytest
@pytest.mark.parametrize('username, password,scnr',[
('byhy', '88888888',({"ret": 0})),
('', '88888888',({"ret": 1,"msg":"用户名或者密码错误"})),
('byhy', '',({"ret": 1,"msg":"用户名或者密码错误"})),
('byhy', '8888888888888888',({"ret": 1,"msg":"用户名或者密码错误"})),
('byhybyhy', '88888888',({"ret": 1,"msg":"用户名或者密码错误"}))
])
def test(username,password,scnr):
url = "http://127.0.0.1/api/mgr/signin"
payload = f"username={username}&password={password}"
headers = {
"Accept": "*/*",
"Accept-Language": "zh-CN,zh;q=0.9",
"Connection": "keep-alive",
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
"Origin": "http://127.0.0.1",
"Referer": "http://127.0.0.1/mgr/sign.html",
"Sec-Fetch-Dest": "empty",
"Sec-Fetch-Mode": "cors",
"Sec-Fetch-Site": "same-origin",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36",
"X-Requested-With": "XMLHttpRequest",
# "sec-ch-ua": ""Not/A)Brand";v="8", "Chromium";v="126", "Google Chrome";v="126"",
"sec-ch-ua-mobile": "?0",
# "sec-ch-ua-platform": ""Windows"",
"Cookie": "sessionid=04y7o3zegx65k3x6cufk5uuvfun2c7od",
"content-type": "application/x-www-form-urlencoded"
}
response = requests.post(url, data=payload, headers=headers)
headers = response.headers['Content-Type']
nr = response.json()
status_code = response.status_code
headers = response.headers['Content-Type']
assert nr == scnr
assert status_code == 200
assert headers == 'application/json; charset=utf-8'
执行结果






思路:登录接口做为封装的接口,用例执行在case目录下执行,但是我们没有进行前置条件的处理


封装的代码:lib/webAPI.py
import requests
class APIMgr:
def login(username='byhy', password='88888888'):
url = "http://127.0.0.1/api/mgr/signin"
payload = f"username={username}&password={password}"
headers = {
"Accept": "*/*",
"Accept-Language": "zh-CN,zh;q=0.9",
"Cache-Control": "no-cache",
"Connection": "keep-alive",
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
"Origin": "http://127.0.0.1",
"Pragma": "no-cache",
"Referer": "http://127.0.0.1/mgr/sign.html",
"Sec-Fetch-Dest": "empty",
"Sec-Fetch-Mode": "cors",
"Sec-Fetch-Site": "same-origin",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36",
"X-Requested-With": "XMLHttpRequest",
"sec-ch-ua-mobile": "?0",
"content-type": "application/x-www-form-urlencoded"
}
response = requests.request("POST", url, data=payload, headers=headers)
return response # 直接返回 response 对象
case目录:case/登录API/test_登录.py

import json
from lib.webAPI import APIMgr
class Test_登录():
def test_API_0101(self):
print('\n用例API_0101')
response = APIMgr.login(username='byhy', password='88888888')
alerttext = response.json() # 假设登录接口返回的是JSON格式数据
status_code = response.status_code
headers = response.headers['Content-Type']
assert alerttext == {"ret": 0} # 断言返回的JSON数据是否符合预期
assert status_code == 200
assert headers == 'application/json; charset=utf-8'
def test_API_0102(self):
print('\n用例API_0102')
response = APIMgr.login('', '88888888')
alerttext = response.json() # 假设登录接口返回的是JSON格式数据
status_code = response.status_code
headers = response.headers['Content-Type']
assert alerttext == {"ret": 1,"msg":"用户名或者密码错误"} # 断言返回的JSON数据是否符合预期
assert status_code == 200
assert headers == 'application/json; charset=utf-8'
def test_API_0103(self):
print('\n用例API_0103')
response = APIMgr.login('byhy', '')
alerttext = response.json() # 假设登录接口返回的是JSON格式数据
status_code = response.status_code
headers = response.headers['Content-Type']
assert alerttext == {"ret": 1,"msg":"用户名或者密码错误"} # 断言返回的JSON数据是否符合预期
assert status_code == 200
assert headers == 'application/json; charset=utf-8'
def test_API_0104(self):
print('\n用例API_0103')
response = APIMgr.login('', '88888888')
alerttext = response.json() # 假设登录接口返回的是JSON格式数据
status_code = response.status_code
headers = response.headers['Content-Type']
assert alerttext == {"ret": 1,"msg":"用户名或者密码错误"} # 断言返回的JSON数据是否符合预期
assert status_code == 200
assert headers == 'application/json; charset=utf-8'
def test_API_0105(self):
print('\n用例API_0105')
response = APIMgr.login('byhy', '8888888888888888')
alerttext = response.json() # 假设登录接口返回的是JSON格式数据
status_code = response.status_code
headers = response.headers['Content-Type']
assert alerttext == {"ret": 1,"msg":"用户名或者密码错误"} # 断言返回的JSON数据是否符合预期
assert status_code == 200
assert headers == 'application/json; charset=utf-8'
def test_API_0106(self):
print('\n用例API_0106')
response = APIMgr.login('byhybyhy', '88888888')
alerttext = response.json() # 假设登录接口返回的是JSON格式数据
status_code = response.status_code
headers = response.headers['Content-Type']
assert alerttext == {"ret": 1,"msg":"用户名或者密码错误"} # 断言返回的JSON数据是否符合预期
assert status_code == 200
assert headers == 'application/json; charset=utf-8'
代码执行结果:

思路:没有客户,没有药品,没有订单
1、进入客户列表,有客户就删除所以客户至没有客户
2、进入药品列表,有药品就删除所以药品至没有药品
3、进入订单列表,有订单就订单所以药品至没有订单

分析
1、列出客户API是一个接口
2、目前输入和输出的变量有7个
输入:cookie , pagenum,pagesize,keywords
cookie表示的是登录的coolie
这3个表示列表的参数
pagenum=1, pagesize=10,keywords=空字符串
输出:code,Content,fhnr
HTTP/1.1 302 —返回的状态码
Content-Type: application/json ----返回的Content-Type
{
“ret”: 302,
“msg”: “未登录”,
“redirect”: “/mgr/sign.html”
} -----------接口返回值
import requests
import pytest
def denglu():
url = "http://127.0.0.1/api/mgr/signin"
payload = "username=byhy&password=88888888"
headers = {
"Accept": "*/*",
"Accept-Language": "zh-CN,zh;q=0.9",
"Connection": "keep-alive",
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
"Origin": "http://127.0.0.1",
"Referer": "http://127.0.0.1/mgr/sign.html",
"Sec-Fetch-Dest": "empty",
"Sec-Fetch-Mode": "cors",
"Sec-Fetch-Site": "same-origin",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36",
"X-Requested-With": "XMLHttpRequest",
# "sec-ch-ua": ""Not/A)Brand";v="8", "Chromium";v="126", "Google Chrome";v="126"",
"sec-ch-ua-mobile": "?0",
# "sec-ch-ua-platform": ""Windows"",
"Accept-Encoding": "gzip, deflate, br"
}
response = requests.request("POST", url, data=payload, headers=headers)
# print(response.text)
cookie=response.headers['Set-Cookie']
# print(cookie)
parts = cookie.split(';')
# 在分隔后的字符串中找到以 'sessionid=' 开头的部分
for part in parts:
if part.strip().startswith('sessionid='):
sessionid = part.strip().split('=')[1]
break
else:
print("未启动服务")
# print(sessionid)
return sessionid
@pytest.mark.parametrize('cookie , pagenum,pagesize,keywords,code,Content,fhnr',[
('', '1', '10', '', 307,'application/json; charset=utf-8',"{'msg': '未登录', 'redirect': '/mgr/sign.html', 'ret': 302}"),
(f'{denglu()}', '1', '10', '', 200,'application/json; charset=utf-8',"{'ret': 0, 'retlist': [], 'total': 0}")
])
def test_khlb(cookie,pagenum,pagesize,keywords,code,Content,fhnr):
url = "http://127.0.0.1/api/mgr/customers"
querystring = {"action": "list_customer", "pagenum":pagenum, "pagesize":pagesize, "keywords": keywords, "_": "1721697626891"}
payload = ""
headers = {
"Accept": "*/*",
"Accept-Language": "zh-CN,zh;q=0.9",
"Connection": "keep-alive",
"Content-Type": "application/x-www-form-urlencoded",
"Referer": "http://127.0.0.1/mgr/",
"Sec-Fetch-Dest": "empty",
"Sec-Fetch-Mode": "cors",
"Sec-Fetch-Site": "same-origin",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36",
"Cookie": f"sessionid={cookie}",
"Accept-Encoding": "gzip, deflate, br"
}
response = requests.request("GET",url, data=payload, headers=headers, params=querystring)
#
fhcode=response.status_code
fhcodeheader=response.headers['Content-Type']
fhnr1=response.json()
# print(fhnr1)
# print(fhcodeheader)
assert fhcode == code
assert fhcodeheader == Content
assert fhcode == code
assert str(fhnr1) == fhnr
# print(
denglu()
# khlb()



剩下的其他15-23行,全部是修改一下参数

import requests
import pytest
def denglu():
url = "http://127.0.0.1/api/mgr/signin"
payload = "username=byhy&password=88888888"
headers = {
"Accept": "*/*",
"Accept-Language": "zh-CN,zh;q=0.9",
"Connection": "keep-alive",
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
"Origin": "http://127.0.0.1",
"Referer": "http://127.0.0.1/mgr/sign.html",
"Sec-Fetch-Dest": "empty",
"Sec-Fetch-Mode": "cors",
"Sec-Fetch-Site": "same-origin",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36",
"X-Requested-With": "XMLHttpRequest",
# "sec-ch-ua": ""Not/A)Brand";v="8", "Chromium";v="126", "Google Chrome";v="126"",
"sec-ch-ua-mobile": "?0",
# "sec-ch-ua-platform": ""Windows"",
"Accept-Encoding": "gzip, deflate, br"
}
response = requests.request("POST", url, data=payload, headers=headers)
# print(response.text)
cookie=response.headers['Set-Cookie']
# print(cookie)
parts = cookie.split(';')
# 在分隔后的字符串中找到以 'sessionid=' 开头的部分
for part in parts:
if part.strip().startswith('sessionid='):
sessionid = part.strip().split('=')[1]
break
else:
print("未启动服务")
# print(sessionid)
return sessionid
@pytest.mark.parametrize('cookie , pagenum,pagesize,keywords,code,Content,fhnr',[
#API-0101
('', '1', '10', '', 307,'application/json; charset=utf-8',"{'msg': '未登录', 'redirect': '/mgr/sign.html', 'ret': 302}"),
#API-0102
(f'{denglu()}', '1', '10', '', 200,'application/json; charset=utf-8',"{'ret': 0, 'retlist': [], 'total': 0}"),
# API-0103
(f'{denglu()}', 'hello', '10', '', 400, 'application/json; charset=utf-8', "{'msg': 'bad pagenum:hello', 'ret': 2}"),
#API-0104
(f'{denglu()}', '1', 'hello', '', 400,'application/json; charset=utf-8',"{'msg': 'bad pagesize:hello', 'ret': 2}"),
#API-0105
(f'{denglu()}', '', '10', '', 400,'application/json; charset=utf-8',"{'msg': 'bad pagenum:', 'ret': 2}"),
#API-0106
(f'{denglu()}', '1', '', '', 400,'application/json; charset=utf-8',"{'msg': 'bad pagesize:', 'ret': 2}"),
# API-0107
(f'{denglu()}', '1', '10', '', 200, 'application/json; charset=utf-8', "{'ret':1,'msg':'请求参数错误'}"),
# API-0108
(f'{denglu()}', '1', '10', '', 200, 'application/json; charset=utf-8', "{'ret': 0, 'retlist': [], 'total': 0}"),
# API-0109
(f'{denglu()}', '2', '10', '', 200, 'application/json; charset=utf-8', "{'ret': 0, 'retlist': [], 'total': 0}"),
# API-0110
(f'{denglu()}', '3', '10', '', 200, 'application/json; charset=utf-8', "{'ret': 0, 'retlist': [], 'total': 0}"),
# API-0111
(f'{denglu()}', '3', '5', '', 200, 'application/json; charset=utf-8', "{'ret': 0, 'retlist': [], 'total': 0}"),
])
def test_khlb(cookie,pagenum,pagesize,keywords,code,Content,fhnr):
url = "http://127.0.0.1/api/mgr/customers"
querystring = {"action": "list_customer", "pagenum":pagenum, "pagesize":pagesize, "keywords": keywords, "_": "1721697626891"}
payload = ""
headers = {
"Accept": "*/*",
"Accept-Language": "zh-CN,zh;q=0.9",
"Connection": "keep-alive",
"Content-Type": "application/x-www-form-urlencoded",
"Referer": "http://127.0.0.1/mgr/",
"Sec-Fetch-Dest": "empty",
"Sec-Fetch-Mode": "cors",
"Sec-Fetch-Site": "same-origin",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36",
"Cookie": f"sessionid={cookie}",
"Accept-Encoding": "gzip, deflate, br"
}
response = requests.request("GET",url, data=payload, headers=headers, params=querystring)
#
fhcode=response.status_code
fhcodeheader=response.headers['Content-Type']
fhnr1=response.json()
# print(fhnr1)
# print(fhcodeheader)
assert fhcode == code
assert fhcodeheader == Content
assert fhcode == code
assert str(fhnr1) == fhnr
# print(
denglu()
# khlb()

先写2个简单的case
