实现思路
使用excel管理用例用例信息,requests模块发送http请求,实现了记录日志,邮件发送测试报告的功能
目录结构如下:
下面直接上代码:
统筹脚本
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
# -*- coding:utf-8 -*- import os from interface import Interface from testcase_get import Get_testcase from result_save import Save_test_result from result_send import Send_report from config.config import Config from logging_save import logger if __name__ = = "__main__" : cur_path = os.path.split(os.path.realpath(__file__))[ 0 ] # 获取当前文件绝对路径 case_path = os.path.join(cur_path, "test_case" , "20170602.xls" ) test_case = Get_testcase(case_path).readExcel() # 获取用例 if not isinstance (test_case, list ): # 判断用例是否获取成功 logger.info( "Test_case get failed... \n Done!" ) else : logger.info( "获取用例成功" ) # 调用接口 test_result = Interface().interfaceTest(test_case) # 获取执行结果,用于发邮件 count_success = test_result[ 3 ] count_failure = test_result[ 4 ] failed_case_detail = test_result[ 5 ] # 保存测试结果 Save_test_result().save_result(case_path, test_result[ 0 ], test_result[ 1 ], test_result[ 2 ]) logger.info( "保存测试结果成功" ) # 获取邮件配置信息 mail_config = Config(os.path.join(cur_path, "config" , "mail.conf" )).get_mail_config() logger.info( "获取邮箱配置成功" ) login_user = mail_config[ 0 ] login_pwd = mail_config[ 1 ] from_addr = mail_config[ 2 ] to_addrs = mail_config[ 3 ] smtp_server = mail_config[ 4 ] mail_send = Send_report(count_success, count_failure, failed_case_detail) # 获取最新测试报告 last_report = mail_send.newest_report() logger.info( "邮件发送结果" ) mail_send.send_result(login_user, login_pwd,from_addr, to_addrs,smtp_server,last_report) logger.info( "DONE!" ) |
请求封装
日志封装
结果比对
结果邮件
用例获取及数据格式化
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
#coding:utf-8 import xlrd from logging_save import logger class Get_testcase( object ): def __init__( self , file_path): ''' :param file_path: 用例文件路径 ''' self .file_path = file_path def readExcel( self ): ''' 读取用例函数 :return: 测试用例列表 ''' try : book = xlrd.open_workbook( self .file_path) # 打开excel except Exception, error: logger.error( '路径不在或者excel不正确 : ' + str (error)) return error else : sheet = book.sheet_by_index( 0 ) # 取第一个sheet页 rows = sheet.nrows # 取这个sheet页的所有行数 case_list = [] # 用于保存用例信息 for i in range (rows): if i ! = 0 : case_list.append(sheet.row_values(i)) # 把每一条测试用例添加到case_list中 return case_list |
请求url转换
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
#coding:utf-8 class urltransform( object ): def __init__( self ): pass def urltransform( self , url, method, param): ''' :return: ''' if param = = '': new_url = url else : if method.upper() = = 'GET' : new_url = url + '?' + param.replace( ';' , '&' ) #如果有参数,且为GET方法则组装url else : new_url = url return new_url |
测试用例excel结构
config目录下,config.py 获取配置文件信息的模块
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
#conding:utf-8 import ConfigParser class Config( object ): def __init__( self ,file_path): self .config = ConfigParser.ConfigParser() self .config.read(file_path) def get_mail_config( self ): login_user = self .config.get( 'SMTP' , 'login_user' ) login_pwd = self .config.get( 'SMTP' , 'login_pwd' ) from_addr = self .config.get( 'SMTP' , 'from_addr' ) to_addrs = self .config.get( 'SMTP' , 'to_addrs' ) smtp_server = self .config.get( 'SMTP' , 'smtp_server' ) port = self .config.get( 'SMTP' , 'port' ) return login_user, login_pwd , from_addr, to_addrs,smtp_server, port def report_save_config( self ): pass |
mail.conf
1
2
3
4
5
6
7
8
|
[SMTP] login_user = 18 * * * * * * @ 163.com login_pwd = * * * * * * from_addr = BI< 18 * * * * * * @ 163.com > to_addrs = [ '18******@163.com' ] #to_addrs = ['1******@qq.com','******.com'] smtp_server = smtp. 163.com port = 25 |
测试报告
邮件接收结果
文中可能存在描述不正确,欢迎园子里的大神们指正补充!
感谢您的阅读,如果觉得对您有帮助,就在右下角点个赞吧,感谢!