Python3-接口自动化-3-接口自动化项目目录框架

一、项目结构

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

----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):
        """

        :return:
        """
        self.set_case_list()# 通过set_case_list()拿到caselist元素组
        test_suite = unittest.TestSuite()
        suite_module = []
        for case in self.caseList:# 从caselist元素组中循环取出case
            case_name = case.split("/")[-1]# 通过split函数来将aaa/bbb分割字符串,-1取后面,0取前面
            print(case_name+".py")# 打印出取出来的名称
            # 批量加载用例,第一个参数为用例存放路径,第一个参数为路径文件名
            discover = unittest.defaultTestLoader.discover(self.caseFile, pattern=case_name + '.py', top_level_dir=None)
            suite_module.append(discover)# 将discover存入suite_module元素组
            print('suite_module:'+str(suite_module))
        if len(suite_module) > 0:# 判断suite_module元素组是否存在元素
            for suite in suite_module:# 如果存在,循环取出元素组内容,命名为suite
                for test_name in suite:# 从discover中取出test_name,使用addTest添加到测试集
                    test_suite.addTest(test_name)
        else:
            print('测试套件中无可用的测试用例')
            return None
        return test_suite 

    def run(self):
        """
        run test
        :return:
        """
        try:
            suit = self.set_case_suite()#调用set_case_suite获取test_suite
            print('try')
            print(str(suit))
            if suit is not None:#判断test_suite是否为空
                print('if-suit')
                fp = open(resultPath, 'wb')# 打开result/20181108/report.html测试报告文件,如果不存在就创建
                # 调用HTMLTestRunner
                runner = HTMLTestRunner.HTMLTestRunner(stream=fp, title='Test Report', description='Test Description')
                runner.run(suit)
            else:
                print("Have no case to test.")
        except Exception as ex:
            print(str(ex))
            # log.info(str(ex))

        finally:
            print("*********TEST END*********")
            # log.info("*********TEST END*********")
            fp.close()
        # 判断邮件发送的开关
        if on_off == 'on':
            send_mail.outlook()
        else:
            print("邮件发送开关配置关闭,请打开开关后可正常自动发送测试报告")
# pythoncom.CoInitialize()
# scheduler = BlockingScheduler()
# scheduler.add_job(AllTest().run, 'cron', day_of_week='1-5', hour=14, minute=59)
# scheduler.start()

if __name__ == '__main__':
    AllTest().run()

1.1 __init__   初始化数据

  # 定义全局变量
global resultPath

# 取到report.html绝对路径
resultPath = os.path.join(report_path, "report.html")

# 拿到caselist.txt的绝对路径
    self.caseListFile = os.path.join(path, "caselist.txt")

# 拿到testCase的绝对路径
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)
  
    # 将resultPath等的值输入到日志,方便定位查看问题
loggger.info('resultPath'+resultPath)
loggger.info('caseListFile'+self.caseListFile)
loggger.info('caseList'+str(self.caseList))


1.2 set_case_list :读取caselist.txt文件中的用例名称,并添加到caselist元素组

read()                  #一次性读取文本中全部的内容,以字符串的形式返回结果

readline()           #只读取文本第一行的内容,以字符串的形式返回结果

readlines()          #读取文本所有内容,并且以数列的格式返回结果,一般配合for in使用
# 打开caselist.txt文件,
fb = open(self.caseListFile)

for value in fb.readlines():
data = str(value)
    # 如果data非空且不以#开头
    if data != '' and not data.startswith("#"):
        # 读取每行数据会将换行转换为\n,去掉每行数据中的\n
self.caseList.append(data.replace("\n", ""))
fb.close()

1.3 set_case_suite 设置测试套件


# 通过set_case_list()拿到caselist元素组
self.set_case_list()
test_suite = unittest.TestSuite()
suite_module = []

# 从caselist元素组中循环取出case
for case in self.caseList:

case_name = case
# 打印出取出来的名称
print(case_name+".py")
# 批量加载用例,第一个参数self.caseFile为用例存放路径,第二个参数case_name为路径文件名
    discover = unittest.defaultTestLoader.discover(self.caseFile, pattern=case_name + '.py', top_level_dir=None)
# 将discover存入suite_module元素组
suite_module.append(discover)

输出为此模式:找到测试用例文件中的test开头的方法
suite_module:[<unittest.suite.TestSuite tests=[<unittest.suite.TestSuite tests=[<unittest.suite.TestSuite tests=[<paramunittest.TestLogin_0 testMethod=test01case>]>]>]>]

print('suite_module:'+str(suite_module))

# 判断suite_module元素组是否存在元素
if len(suite_module) > 0:
# 如果存在,循环取出元素组内容,命名为suite
for suite in suite_module:
# 从discover中取出test_name,使用addTest添加到测试集
for test_name in suite:
test_suite.addTest(test_name)
else:
print('
测试套件中无可执行的测试用例')
    return None
return test_suite


1.4 run 执行测试用例套件


try:
suit = self.set_case_suite()#调用set_case_suite获取test_suite

if suit is not None:#判断test_suite是否为空
print('if-suit')
fp = open(resultPath, 'wb')# 打开result/20181108/report.html测试报告文件,如果不存在就创建
# 调用HTMLTestRunner
runner = HTMLTestRunner.HTMLTestRunner(stream=fp, title='Test Report', description='Test Description')
runner.run(suit)
else:
print("Have no case to test.")
except Exception as ex:
print(str(ex))
# log.info(str(ex))

finally:
print("*********TEST END*********")
# log.info("*********TEST END*********")
fp.close()
# 判断邮件发送的开关
if on_off == 'on':
send_mail.outlook()
else:
print("邮件发送开关配置关闭,请打开开关后可正常自动发送测试报告")
 

猜你喜欢

转载自www.cnblogs.com/chushujin/p/12943469.html