1.按天自动生成html/log/png文件目录
init_folder.py实现代码如下:
#按天为单位初始化生成html png log 文件目录
import os,datetime
def init_folder(date):
html_folder_path ='./../report/html/'
folder_path = html_folder_path+date
if not os.path.exists(folder_path):
os.makedirs(folder_path)
png_folder_path ='./../report/png/'
folder_path = png_folder_path+date
if not os.path.exists(folder_path):
os.makedirs(folder_path)
log_folder_path = './../report/log/'
folder_path = log_folder_path + date
if not os.path.exists(folder_path):
os.makedirs(folder_path)
2.用HTMLTestRunner生成html报告
运行模块,先初始化init_folder,生成文件目录,再生成html报告到html报告目录下
import unittest,HTMLTestRunner,datetime
from utils.init_folder import init_folder
suit = unittest.defaultTestLoader.discover('./../case_manager/',pattern='test_baidu_sou.py')
if __name__=='__main__':
#runner = unittest.TextTestRunner()
date_time = datetime.datetime.now()
date=date_time.strftime('%Y-%m-%d')
report_time=date_time.strftime('%H-%M-%S')
init_folder(date)
runner=HTMLTestRunner.HTMLTestRunner(open('./../report/html/'+date+'/'+report_time+'report.html','wb'),
title='test_html',description='text')
runner.run(suit)
3.自动生成日志Log
封装log日志生成类,代码如下:
import logging,datetime
class Log():
def __init__(self):
date_time = datetime.datetime.now()
date = date_time.strftime('%Y-%m-%d')
logging.basicConfig(
level = logging.INFO,
format = '%(asctime)s %(levelname)s %(message)s',
datefmt = '%Y-%m-%d %H %M %S',
filename='./../report/log/'+date+'/test.log',
filemode = 'w'
)
def add_log(self,page,func,des):
out_str = page +':'+func+':'+des
logging.info(out_str)
在具体用例执行每次断言后加入日志,简单百度搜索的例子如下:
import unittest,time,ddt
from selenium import webdriver
from utils.excel_read import ParseExcel
from utils.log import Log
excel_path = "./../data_manager/test_data.xlsx"
sheetName = 'data_sou'
excel=ParseExcel(excel_path,sheetName)
@ddt.ddt
class Test_badu_News(unittest.TestCase):
#百度搜索用例
@classmethod
def setUpClass(self):
self.driver = webdriver.Chrome()
self.log = Log()
@classmethod
def tearDown(self):
self.driver.quit()
def setUp(self):
self.driver.get('https://www.baidu.com/')
time.sleep(2)
def tearDown(self):
pass
@ddt.data( * excel.getDataFromSheet_mul())
def test_sou(self,data):
#'''搜索方式'''
try:
self.driver.find_element_by_id('kw').send_keys(data[0])
time.sleep(2)
self.driver.find_element_by_id('su').click()
time.sleep(5)
self.assertEqual(data[0] + '_百度搜索', self.driver.title)
except AssertionError as e:
self.log.add_log(data[0],data[1],format(e))
self.assertEqual(data[0] + '_百度搜索', self.driver.title)
else:
self.log.add_log(data[0],data[1],'用例执行成功')
if __name__ == 'main':
unittest.main()
4.截图并保存到指定路径
使用以下函数:
self.driver.get_screenshot_as_file('路径') #截图
保存到先前自动生成的png文件夹里:
#截图保存到指定目录
date_time = datetime.datetime.now()
date = date_time.strftime('%Y-%m-%d')
st= date_time.strftime('%Y_%m_%d_%H_%M_%S')
file_name = st + '.png' #自定义图片名
path = './../../report/png/'+date+'/' #指定保存截图路径
whole_path=path+file_name
self.driver.get_screenshot_as_file(whole_path) #截图
5.生成的报告结果
html报告如下: log日志如下: png截图文件:
|