Python_自动化_ATP
Python自动化
apt缺点:
1、excel格式不灵活,操作excel代码比较繁琐
2、不适合有关联关系的接口测试
1 作业思路
1、获取excel获取所有的case--xlrd
2、根据测试用例调用接口--requests
3、校验结果--if
4、结果写入excel--xlutil
5、生成报告,发送邮件--yagmail
2 解决方案
数据驱动:根据数据去测试的。
代码驱动:根据代码去测试的。
关键字驱动:根据关键字去测试的。
框架:一堆工具的集合
初期:适用于用例较多,用例独立无关联,批量运行时。如下代码
问题:数据存在mongodb、redis里,用例有关联时无法测试,无法加cookie,加header
解决:加2列(加请求头,是否为json)、使用另外的框架
3 程序清单
# 程序清单如下: apt-------------------程序目录
bin---------------可执行文件
start.py------启动文件
cases-------------测试用例目录
测试用例.xls---用例文件
测试用例1.xls--用例文件
config------------配置目录
setting.py----配置信息
lib---------------主程序目录
my_request.py-封装请求方法
tools.py------封装工具
logs--------------日志目录
apt.log-------日志文件
readme.txt--------说明文件
4 程序
4.1 start.py
1 #!/usr/bin/python
2 # -*- coding:utf-8 -*-
3 # Author:hwm
4 import os,glob
5 from config.setting import CASE_PATH
6 from lib import tools
7 BASE_PATH = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
8 sys.path.insert(0,BASE_PATH)
9 def main():
10 all_count = 0 #存放所有文件用例的个数
11 all_pass_count = 0 #存放所有文件用例通过的个数
12 files = [] #存放运行过的所有用例文件
13 for file in glob.glob(os.path.join(CASE_PATH,'*.xls')):#glob支持正则
14 case_abs_path = os.path.join(CASE_PATH,file)
15 all_case = tools.readExcel(case_abs_path) #获取所有的用例
16 all_res = tools.run_case(all_case) #获取所有的执行结果
17 tools.write_res(case_abs_path,all_res) #将执行结果写入文件
18 case_count = len(all_res) #当前用例总数
19 pass_count = str(all_res).count('通过') #当前通过用例总数
20 all_count += case_count #累加每个用例文件的用例数
21 all_pass_count += pass_count #累加每个用例文件通过的用例数
22 files.append(file) #加入每个附件
23 msg='''
24 各位好:
25 本次运行%s条用例,通过%s条,失败%s条,详细信息见附件。
26 '''%(all_count,all_pass_count,all_count-all_pass_count)
27 tools.send_email(msg,files)
28 main()
4.2 setting.py
1 #!/usr/bin/python
2 # -*- coding:utf-8 -*-
3 # Author:hwm
4 import nnlog,os,sys
5 BASE_PATH = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
6 LOG_FILE_NAME = 'atp.log'
7 ABS_FILE_PATH = os.path.join(BASE_PATH,'logs',LOG_FILE_NAME)
8 my_log = nnlog.Logger(ABS_FILE_PATH) #日志信息
9 CASE_PATH = os.path.join(BASE_PATH,'cases') #存放用例的目录
10 MAIL_INFO = {
11 'user':'925441532@qq.com',#邮箱账号
12 'password':'kiujkguxiwdcbbhj',#邮箱密码
13 'host':'smtp.qq.com'#邮箱服务器
14 }#邮箱信息
15 TO = ['925441532@qq.com']#发送人
16 CC = ['357017152@qq.com']#抄送人
4.3 my_request.py
1 #!/usr/bin/python
2 # -*- coding:utf-8 -*-
3 # Author:hwm
4 import requests
5 from config.setting import my_log
6 def post(url,data,headers=None,cookies=None,is_json=False):
7 try:
8 if is_json:
9 res = requests.post(url,json=data,headers=headers,cookies=cookies).text
10 else:
11 res = requests.post(url, data=data, headers=headers, cookies=cookies).text
12 except Exception as e:
13 my_log.error('接口请求出错,%s'%e)
14 res = str(e)
15 return res
16 def get(url,data,headers=None,cookies=None):
17 try:
18 res = requests.get(url, params=data, headers=headers, cookies=cookies).text
19 except Exception as e:
20 my_log.error('接口请求出错,%s'%e)
21 res = str(e)
22 return res
4.4 tools.py
1 #!/usr/bin/python
2 # -*- coding:utf-8 -*-
3 # Author:hwm
4 import xlrd,requests,xlutils,yagmail,time
5 from xlutils.copy import copy
6 from config.setting import my_log,MAIL_INFO,TO,CC
7 from lib import my_request
8 def readExcel(file_path):
9 try:
10 book = xlrd.open_workbook(file_path)
11 except Exception as e:
12 my_log.error('打开用例出错,文件名是%s'%file_path)
13 return []
14 else:
15 all_case = [] #存放所有的用例,二维list
16 sheet = book.sheet_by_index(0)
17 for i in range (1,sheet.nrows):
18 row_data=sheet.row_values(i) #每一行数据,list
19 all_case.append(row_data[4:8])
20 return all_case
21 def check_res(res:str,check:str):#str只是说明,但并不强制类型
22 new_res = res.replace('": "','=').replace('": ','=')
23 for c in check.split(','):
24 if c not in new_res:
25 return '失败'
26 return '通过'
27 def str_to_dict(s:str,seq='&'):
28 d = {}
29 if s.strip():
30 for tmp in s.split(seq):
31 k,v = tmp.split('=')
32 d[k]=v
33 return d
34 def run_case(all_case):
35 all_res = []
36 for case in all_case:
37 url,method,data,check = case
38 my_log.info('正在运行:%s'%url)
39 data = str_to_dict(data) #把请求参数转成字典
40 if method.upper() == 'POST':
41 res = my_request.post(url,data)
42 else:
43 res = my_request.get(url,data)
44 status = check_res(res,check)
45 all_res.append([res,status])
46 return all_res
47 def write_res(file_name,res_list):
48 book = xlrd.open_workbook(file_name)
49 new_book = copy(book)
50 sheet = new_book.get_sheet(0)
51 for row,data in enumerate(res_list,1):
52 res,status=data #取小List的两个元素
53 sheet.write(row,8,res) #写入响应报文
54 sheet.write(row,9,status) #写入测试结果
55 new_book.save(file_name)
56 def send_email(content,files):
57 mail = yagmail.SMTP(**MAIL_INFO)
58 subject = '%s_接口测试报告' % time.strftime('%Y-%m-%d %H:%M:%S')
59 # 连接上邮箱了
60 mail.send(to=TO,
61 cc=CC,
62 subject=subject,
63 contents=content,
64 attachments=files)