test.py
# -*- coding: utf-8 -*-
import os
import time
import unittest
from selenium import webdriver
from dateutil.parser import parse
from BeautifulReport import BeautifulReport
class Test(unittest.TestCase):
now = time.strftime("%Y-%m-%d %H_%M_%S")
# 保存截图函数
def save_img(self, img_name):
self.browser.get_screenshot_as_file(
'{}/{}.png'.format(os.path.abspath("./img"), img_name))
# 启动函数
def setUp(self):
self.browser = webdriver.Chrome()
self.browser.maximize_window()
self.starttime = parse(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
print("开始测试时间:", self.starttime)
self.browser.get("https://www.baidu.com/")
time.sleep(3)
self.browser.find_element_by_class_name("el-icon-refresh").click()
time.sleep(10)
# 结束函数
def tearDown(self):
time.sleep(3)
self.browser.quit()
self.endtime = parse(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
print("测试结束时间:", self.endtime)
totaltime = (self.endtime - self.starttime).total_seconds()
print("总时长:", totaltime, "秒")
# 测试用例1
@BeautifulReport.add_test_img('测试用例1')
def test_01(self):
u"""登录"""
self.browser.find_element_by_id("kw").clear()
self.browser.find_element_by_id("kw").send_keys("test")
self.browser.find_element_by_id("su").click()
print('test111')
self.save_img('测试用例1')
# 测试用例2
@BeautifulReport.add_test_img('测试用例2')
def test_02(self):
u"""测试用例2"""
self.save_img('测试用例2')
print('test222')
if __name__ == '__main__':
unittest.main()
run.py
# -*- coding: utf-8 -*-
import unittest
import time
from BeautifulReport import BeautifulReport
# 用例存放位置
test_case_path = "C:/Users/Administrator/PycharmProjects/test"
# 测试报告存放位置
tdresult = 'D:\四维/baogao/te'
# 测试报告名称
now = time.strftime("%Y-%m-%d %H_%M_%S")
filename = now + '_测试报告'
# 用例名称
description = '方舟测试'
# 需要执行哪些用例
pattern = "test5.py"
if __name__ == '__main__':
test_suite = unittest.defaultTestLoader.discover(test_case_path, pattern=pattern)
result = BeautifulReport(test_suite)
result.report(filename=filename, description=description, log_path='',report_dir=tdresult)
下图为生成的测试报告