Python+unittest+requests+HTMLTestRunner框架
1 目录说明
common:公共文档,存放包含HTMLTestRunner.py,logUtils.py等公用文件
data:数据文档,存放测试数据;接口数据使用excel存储
log:存放日志文件
report:存放测试报告
page:编写模块对象层的代码,即把模块公共的方法封装成对象以供调用
testCase:测试用例代码,按照模块划分
runTest:执行所有测试用例的文件
2 common
#helper.py
#helper.pyimport os, xlrd, json, demjson, unittestclass Helper(object):'''公共方法'''def base_dir(self,filename,folder='data'):'''返回文件路径params filename:文件名称params folder:文件夹 '''return os.path.join(os.path.dirname(__file__),os.path.pardir, folder, filename)def read_excel(self,rowx,filename='data.xlsx'):''':return:读取excel中数据并且返回:params filepath :xlsx文件名称:params rowx:在excel中的行数'''book = xlrd.open_workbook(self.base_dir(filename))sheet = book.sheet_by_index(0)return sheet.row_values(rowx)def get_url(self,rowx):'''获取请求地址:params rowx:url所在的行数'''return self.read_excel(rowx)[1]def get_data(self,rowx):'''获取请求数据:params rowx:url所在的行数'''return demjson.decode(json.loads(demjson.encode(self.read_excel(rowx)[2], encoding='utf-8')))if __name__ == '__main__':t = Helper()print(t.get_url(1))print(t.get_data(1))
HTMLTestRunner.py,logUtils.py文件在网上找自己喜欢的格式下载放到目录即可
3 page
#user.py
import requestsdef getHeaders(Authorization=""):'''返回请求头'''headers = {"Content-Type": "application/json", "Authorization": Authorization}return headersdef post(url,params={},data={}):'''对post二次封装'''r = requests.post(url=url,headers=getHeaders(),timeout=6,json=data,params=params)return rdef get(url,params={},data={}):'''对get二次封装'''r = requests.get(url=url,headers=getHeaders(),timeout=6,json=data,params=params)return rif __name__ == '__main__':print(getHeaders())
4 testCase
#test_user.py
from page.user import *from common.helper import *class TestUser(unittest.TestCase,Helper):@classmethoddef setUpClass(cls):print("start===============!")@classmethoddef tearDownClass(cls):print("end===============!")def test_user_001(self):'''登录'''r = post(self.get_url(1), params=self.get_data(1))print(r.json())self.assertEqual(r.json()['code'],200)self.assertIn('token',r.text)with open(self.base_dir('pctoken'),'w',encoding='utf-8') as f:f.write(r.json()['data']['token'])@propertydef getPctoken(self):'''获取成功登录的token'''with open(self.base_dir('pctoken'), 'r', encoding='utf-8') as f:return f.read()if __name__ == '__main__':unittest.main(verbosity=2)
5 runTest
#!/usr/bin/env python#author xqimport unittestimport osfrom common import HTMLTestRunnerfrom common.helper import Helperfrom common.logUtils import LogUtilsimport timedef getSuites():suites = unittest.defaultTestLoader.discover(start_dir=os.path.join(os.path.dirname(__file__),'testCase'),pattern='test_*.py',top_level_dir=None)return suitesdef getNowTime():return time.strftime('%y-%m-%d-%H-%M-%S',time.localtime())def run():report_filename = Helper().base_dir(folder='report', filename=getNowTime()+'report.html')HTMLTestRunner.HTMLTestRunner(stream=open(report_filename,'wb'),verbosity=2,title='自动化测试报告',description='自动化测试详细的报告').run(getSuites())LogUtils.log().info('测试结束,生成基于HTML的测试报告')if __name__ == '__main__':run()
如果觉得《python ui自动化测试框架_Python自动化测试框架》对你有帮助,请点赞、收藏,并留下你的观点哦!