一、项目结构
1、新建一个工程(一定要创建工程),工程名称自己定义,如:ApiAutoTest
2、在工程下创建以下几个 pakage 包:
--testcase:这个包放 test 开头的测试用例--common:这个包放一些公共的方法,如:读取 excel 文件方法,读取 mysql 等 E_mail.py:封装邮件收发方法ReadExcel.py:封装 Excel 读取方法ExcelValue.py:读取 Excel 中内容request_base.py:封装请求公共方法--config:这个包下面放配置文件 conf.py:存放路径 url --run_all:这个包下放入执行用例的文件--report:这里存放测试报告--data:这里放接口测试用例二、示例如下
1、config 包下文件
""" conf.py 路径 url """ import os project_path = "D:/work_doc/CodeFile/class11" data_path = os.path.join(project_path,"data","data.xlsx") report_path = os.path.join(project_path,"report") case_path = os.path.join(project_path,"testcase")2、data 包下文件
3、common 模块下文件
""" ReadExcel.py """ import xlrd class ReadExcel(): def __init__(self,filename,sheet_name): workbook = xlrd.open_workbook(filename) self.sheet = workbook.sheet_by_name(sheet_name) def getValue(self,rowx,colx): value = self.sheet.cell(rowx,colx).value if isinstance(value,float): return int(value) else: return value """ ExcelValue.py """ from class11.common.ReadExcel import ReadExcel from class11.config.conf import * Url = ReadExcel(data_path,"Sheet1").getValue(1,0) Mode = ReadExcel(data_path,"Sheet1").getValue(1,1) Header = ReadExcel(data_path,"Sheet1").getValue(1,2) #正常参数 Rigth_body = ReadExcel(data_path,"Sheet1").getValue(1,3) #错误参数 Error_body = ReadExcel(data_path,"Sheet1").getValue(2,3) """ E_mail.py 配置收发邮件 """ from email.mime.text import MIMEText from email.header import Header import smtplib import time import os def send_mail(new_report): f = open(new_report, "rb") mail_body = f.read() f.close() username = "1980958379@qq.com" #发件箱用户名 password = "gekzdyrczclhbfxx" #发件箱密码(授权码) sender = "1980958379@qq.com" #发件人邮箱 receiver = ["1980958379@qq.com"] #收件人邮箱 # 邮件正文是MIMEText msg = MIMEText(mail_body, "html", "utf-8") # 邮件对象 msg["Subject"] = Header("自动化测试报告", "utf-8").encode() msg["From"] = Header(u"测试机 <%s>"%sender) msg["To"] = Header("测试负责人 <%s>"%receiver) msg["date"] = time.strftime("%a,%d %b %Y %H:%M:%S %z") #发送邮件 smtp = smtplib.SMTP() smtp.connect("smtp.qq.com") # 邮箱服务器 smtp.login(username, password) # 登录邮箱 smtp.sendmail(sender, receiver, msg.as_string()) # 发送者和接收者 smtp.quit() print("邮件已发出!注意查收。") # ======查找测试目录,找到最新生成的测试报告文件====== def new_report(test_report): lists = os.listdir(test_report) # 列出目录的下所有文件和文件夹保存到lists lists.sort(key=lambda fn: os.path.getmtime(test_report + "\\" + fn)) # 按时间排序 file_new = os.path.join(test_report, lists[-1]) # 获取最新的文件保存到file_new return file_new """ request_base.py 封装请求公共方法 """ import requests import json def ApiRequest(url,mothed,headers,requestBody): if mothed == "post" or mothed == "POST": if "x-www-form" in json.loads(headers)["Content-Type"]: res = requests.request(mothed,url,headers=json.loads(headers),data=json.loads(requestBody)) return res else: res = requests.request(mothed,url,headers=json.loads(headers),json=json.loads(requestBody)) return res else: res = requests.request(mothed,url,headers=json.loads(headers),params=json.loads(requestBody)) return res4、testcase 包下文件
import unittest from ApiAotuTest.common.ExcelValue import * from ApiAotuTest.common.request_base import ApiRequest class ApiTest(unittest.TestCase): def test_case01(self): res = ApiRequest(Url,Method,Header,Rigth_body) self.assertEqual(res.json()["code"],"200",msg="test_case01 assert failed") def test_case02(self): res = ApiRequest(Url, Method, Header, Error_body) self.assertEqual(res.json()["code"], "400", msg="test_case02 assert failed") if __name__ == '__main__': unittest.main()5、runall 包下文件
import unittest from ApiAotuTest.config.conf import * import HTMLTestRunner from ApiAotuTest.common.E_mail import * now = time.strftime("%Y-%m-%d_%H-%M-%S") report = report_path + "/" + now + "report.html" #拼接报告绝对路径 reportfile = open(report,"wb") def runall(): dis = unittest.defaultTestLoader.discover(case_path,pattern="case*.py") runner = HTMLTestRunner.HTMLTestRunner(stream=reportfile) runner.run(dis) def Email(): NewReport = new_report(report_path) send_mail(NewReport) if __name__ == '__main__': runall() Email()