需求
1、第一个接口自动化的框架
1、读excel,获取数据 url、请求方式、参数、检查点
2、调用接口,获取返回结果
3、校验用例是否通过
4、产生报告,发邮件
操作
1.新建文件夹
2.readme.md
3.config--setting
import os import faker from lib.util import random_password EMAIL_INFO = { 'user':'uitestp4p@163.com', 'host':'smtp.163.com', 'password':'houyafan123' } TO = ['511402865@qq.com','525586735@qq.com','526962645@qq.com'] CC = ['540493450@qq.com','945968766@qq.com']
#-----------以下为定义常量------------------# BASE_PATH = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))#os.path.abspath获取当前目录下的路径(atp的路径),dirname获取副目录 CASE_PATH = os.path.join(BASE_PATH,'cases')#join把case文件夹的目录拼起来 LOG_PATH = os.path.join(BASE_PATH,'logs','atp.log')# REPORT_PATH = os.path.join(BASE_PATH,'report') #------------------------------------------# f = faker.Faker(locale='zh-CN') ENV = 'test' #默认使用测试环境 host_map = { "test":"http://api.nnzhp.cn", "dev":"http://118.24.3.40:81/", "pre":"http://api.nnzhp.cn/", } HOST = host_map.get(ENV) func_map ={ "<phone>":f.phone_number, "<id_card>":f.ssn, "<email>":f.email, "<name>":f.name, "<addr>":f.address, "<password>":random_password } #这个是支持的参数化,如果要加其他的参数化,在这里继续加就行了。
4.lib--read_case.py
A.安装jsonpath模块,找相关数据方便
import jsonpath d = { "stu":{ "sex":'男', "house":{ "beijing":{"四环":5,"三环":4}, "上海":{"浦东":4} } }, "stu2":'abc' } result = jsonpath.jsonpath(d,'$..beijing') print(d['stu']['house']['上海']['浦东']) print(result)
B.config中新建用例模板
C.编辑read_case.py--读excel,获取数据
D.atp实例化--setting中实例化
func_map ={ "<phone>":f.phone_number, "<id_card>":f.ssn, "<email>":f.email, "<name>":f.name, "<addr>":f.address, "<password>":random_password } #这个是支持的参数化,如果要加其他的参数化,在这里继续加就行了。
使用Faker模块
import faker f = faker.Faker(locale='zh-CN') # print(f.ssn()) # print(f.phone_number()) # print(f.email()) # print(f.address()) # print(f.name()) import random,string def random_password(): a = random.sample(string.digits,2) b = random.sample(string.ascii_letters,2) c = random.sample(string.ascii_uppercase,2) d = random.sample(string.punctuation,2) result = a + b + c + d random.shuffle(result) return ''.join(result) func_map ={ "<phone>":f.phone_number, "<id_card>":f.ssn, "<email>":f.email, "<name>":f.name, "<addr>":f.address, "<password>":random_password } def replace_param(s): for func_name,func in func_map.items(): if func_name in s: result = func() s = s.replace(func_name,result) return s # replace_param("username=<phone>,passwd=123456,addr=<addr>,id_card=<id_card>") #username=15539055994,passwd=123456,addr=新疆维吾尔自治区东市浔阳曹路A座 498947,id_card=451123197106055998 def str_to_dict(s): d = {} for t in s.split(','): k,v = t.split('=') d[k]=v return d
E.封装发请求类lib-request
F.校验结果lib-parse_response