接口测试示例:
利用fiddler检测请求,获取请求数据进行相应参数替换。
获取请求体,求改请求参数,参数化请求,数据库、CSV、excel储存方式
1.处理HTTP协议
# python中处理HTTP协议,需要导入http.client
from http.client import HTTPConnection
# 打开首页,并进行断言
# 建立与服务器端的连接
conn = HTTPConnection('localhost',8080)
# 获取该请求的响应
# 发送请求,URL地址不到HOST信息
conn.request('GET','/sales/')
# 获取该请求的响应
resp = conn.getreaponse().read().decode()
if '首页' in resp:
print('打开首页-成功')
else:
print('打开首页-失败')
# 关闭连接
conn.close()
# 实现登录POST请求,必须指定三个要素:正文,URL地址,Content-Type
# 发送请求,URL地址不带HOST请求
conn.request('GET','/salse/')
param = 'username=admin&pas sword= admin123&verifycode=0000'
header = {
' Content-Type': 'application/x-Www-form-urlencoded'}
conn.request('POST','http://localhost:8080/user/login/',body=param,headers=header)
resp = conn.getresponse().read().decode()
print(resp)
conn.close()
2.模拟登录与添加数据
请求参数中有中文使用 encode()编码
import http.client
class CustomerManage:
def __init__(self):
self.conn = http,client.HTTPConnection(host='localhost',port=8080)
# 登录
def login(self):
param = 'username=admin&pa s sword=admin123&verifycode=000O'
header = {
'Content-Type': 'application/x-ww form-urlencoded'}
self.cinn.request(method="POST",url="/user/login",body=param,headers=header)
resp = self.conn.getresponse().read().decode()
print(resp)
# 添加数据
def add(self):
param = 'customername=接口王&customerphone=13512356783&childsex=男&childdate=2019-06-29&creditkids=100&creditcloth=200'
header = {
'Content-Type': 'application/x-www-form- urlencoded'}
# 请求体出现正文body=param.encode('utf-8')
self.conn.request(method='POST',url='/customer/add',body=param.encode('utf-8'),headers=header)
resp = self.conn.getresponse().read().decode()
print(resp)
if __name__ == '__main__':
cm = CustomerManager()
cm.login()
cm.add()
3.利用多线程threading模拟多用户
from http.client import HTTPConnection
import threading
def stree_test():
conn = HTTPConnection(host='localhost',port=8080)
aram = 'username=admin&pas sword= admin123&verifycode=0000'
header = {
'Content-Type': ' application/x-Www-form-urlencoded '}
conn.request(method='POST',url='/user/login',body=param,headers=hesder)
resp = conn.getresponse().read().decode()
if resp == 'login-pass':
print('登录测试--成功')
else:
print('登陆测试--失败')
conn.close()
if __name__ == '__main__':
threading.Thread(target=stre_test).start()
4.采用封装断言
主文件client.py
import http.client
import common
class CustomerManage:
def __init__(self):
self.conn = http.client.HTTPCOnnection('localhost',8080)
self.cookie = None
def login(self):
param = 'username=admin&password=admin123&verifycode=000O'
header = {
'Content-Type': 'application/x-www form-urlencoded'}
self.conn.request(method='POST',url='/user/login',body=param,headers=header)
# 读取响应的标题头信息
response = self.conn.getresponse()
self.cookie = response.getheader('Set-Cookie')
resp = response.read().decode()
common.assert_contain(resp,'login-pass','登录成功')
def add(self):
param ='customername=接口王&customerphone=13512356783&childsex=男&childdate=2019-06-29&creditkids=100&creditcloth=200'
header = {
'Content-Type': 'application/x-www-form-urlencoded',
'Cookie':self.cookie}
self.conn.request(method='POST',url='/costomer/add',body=param.encode(),headers=header)
resp = self.conn.getresponse().read().decode()
common.assert_contaion(resp,'add-successful','新增用户')
def query(self):
param = 'customerphone=18&page=3 '
header = {
'Content-Type': ' application/ x-www-form-urlencoded',
'Cookie':self.cookie}
self.conn.request(method='POST',url='/customer/search/,body=param,headers=header)
resp = self.conn.getresponse().read().decode()
print(resp)
if __name__ == '__main__':
cm = CustomerManage()
cm.login()
cm.add()
cm.query()
上下文均调用断言封装文件 common.py
import re
def assert_equal(actual,expect,case):
if expect in actual:
print('测试用例:%s的测试用例结果为:成功'%case)
else:
print('测试用例:%s的测试用例结果为:失败'%case)
def assert_contain(actual,expect,cace):
if expect in actual:
print('测试用例:%s的测试结果为:成功'%case)
else:
print('测试用例:%s的测试结果为:失败'%case)
def assert_true(actual,case):
if actual == True:
print('测试用例:%s的测试结果为:成功'%case)
else:
print('测试用例:%s的测试结果为:失败'%case)
def assert_re(strung, regex, case):
result = re.match(regex,string)
if result is None:
print('测试用例:[%s]的测试结果为:失败'%case)
else:
print('测试用例:[%s]的测试结果为:失败'%case)
# 将post请求正文的key=value&key=value&key=value转换为字典
def post_2_dict(post):
dict = {
}
list = post.split('&')
for item in list:
key = item.split('=')[0]
value = item.split('=')[1]
dict[key] = value
return dict
5.数据库存取、JSON
主文件sell.py
import http.client, random
import json, re
import database
import common
class SellManage:
def __init__(self):
self.conn = http.client.HTTPConnection('localhost',8080)
self.database = database.Database()
self.header = {
'Content-Type': 'application/x-www form-urlencoded'}
self.goodinfo = None
# 外部数据源完成数据驱动:List+Dict,XML,CSV,Excel,数据库表
def login(self):
param = 'username=admin&pa s sword=admin123&verifycode=000O'
self.conn.request(method='POST',url='/login',body=param,headers=self.header)
# 读取响应的头部信息,并将cookie值添加刀字典header中
response = self.conn.getresponse()
self.header['Coolie'] = response.getheader('Set-Cookie')
resp = respsonse.read().decode()
common.assert_contaion(resp,'login-pass', '登录系统')
# 查询并断言
def barcode(self):
barcode = self.random_barcode()
param = 'barcode=%s'%barcode
self.conn.request(method='POST',url='/sell/barcode',body=param,headers=self.header)
resp = self.conn.getresponse().read().decode()
# 解析参数转换为字典
list = eval(resp)
result = (len(list