一、项目结构
1. 新建一个工程,工程名为:sales_interface_auto
2. 在工程的根目录新建一个py脚本:runAll.py 执行接口自动化的入口,项目工程部署完毕后直接运行该文件即可
3. 在项目下创建几个package包:
----common:这个包放置一些公共的方法,例如:读取excel,读取mysql,get和post请求的封装,发送Email的封装,读取手机公共参数的封装,Log.py是封装日志的输入
----config:这个包里是放置一些获取根文件夹目录,接口服务器地址,读写配置文件封装的方法
----result: 该包里存放日志,截图,HTML报告
----testCase:这个包放test开头的测试用例,也可以放一些封装接口的方法
----testFile/case:存放excel测试用例
----testModels :存放对应接口脚本的装饰器提取器
----util/generator:接口测试中用到的一些数据的生成,如:身份证号,名字,手机号,随机字符串等
----caselist.txt :顺序存放testCase中test打头的用例,切记注意顺序,无需执行时,首位加#号即可
----config.ini :这里是配置文件,如邮箱的一些参数,数据库,手机静态参数,以及存放测试过程生成的参数
----config_url.ini:这里是配置文件,存放接口地址
二、详细介绍个目录
1.runAll.py
源码如下:
import os import common.HTMLTestRunner as HTMLTestRunner from config import readConfig, getpathInfo import unittest from common.configEmail import send_email import common.Log from testCase.test_init_user_info import InitUserInfo from config import readConfig as readConfig, writeConfig as writeConfig, readExcel, geturlParams writeconfig = writeConfig.WriteConfig() send_mail = send_email() path = getpathInfo.get_Path() report_path = os.path.join(path, 'result') on_off = readConfig.ReadConfig().get_email('on_off') loggger = common.Log.logger #定义一个类AllTest class AllTest: # 初始化一些参数和数据 def __init__(self): global resultPath # result/report.html resultPath = os.path.join(report_path, "report.html") # 配置执行哪些测试文件的配置文件路径 self.caseListFile = os.path.join(path, "caselist.txt") # 真正的测试断言文件路径 self.caseFile = os.path.join(path, "testCase") # 初始化一下测试中用到的数据,并存储到配置文件 self.idno = InitUserInfo().generate_id_no() self.c_name = InitUserInfo().generate_c_name() self.c_mobile = InitUserInfo().generate_mobile() self.caseList = [] writeconfig.write_potentiall_user_info(self.idno,self.c_name,self.c_mobile) loggger.info('resultPath'+resultPath)# 将resultPath的值输入到日志,方便定位查看问题 loggger.info('caseListFile'+self.caseListFile) loggger.info('caseList'+str(self.caseList)) def set_case_list(self): """ 读取caselist.txt文件中的用例名称,并添加到caselist元素组 :return: """ fb = open(self.caseListFile) for value in fb.readlines(): data = str(value) if data != '' and not data.startswith("#"):# 如果data非空且不以#开头 self.caseList.append(data.replace("\n", ""))# 读取每行数据会将换行转换为\n,去掉每行数据中的\n fb.close() def set_case_suite(self): "