Python自动化测试系列[v1.0.0][HTMLTestRunner测试报告]

测试代码

# encoding = utf-8
"""
__title__ = ''
__author__ = 'davieyang'
__mtime__ = '2018/4/21'
"""
from selenium import webdriver
import unittest
import time
import logging
import traceback
import ddt
from selenium.common.exceptions import NoSuchElementException
 
# 初始化日志对象
logging.basicConfig(
    # 日志级别
    level=logging.INFO,
    # 时间、代码所在文件名、代码行号、日志级别名字、日志信息
    format='%(asctime)s %(filename)s[line: %(lineno)d] %(levelname)s %(message)s',
    # 打印日志的时间
    datefmt='%a, %d %b %Y %H:%M:%S',
    # 日志文件存放的目录及日志文件名
    filename='F:\\DataDriven\\TestResults\TestResults.TestResults',
    # 打开日志的方式
    filemode='w'
)
 
 
@ddt.ddt
class DataDrivenTestByDDTHTR(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome(executable_path="F:\\automation\\webdriver\\chromedriver.exe")
 
    @ddt.file_data("F:\\DataDriven\\testData\\test_data_list.json")
    def test_dataDrivenHTRByFile(self, value):
        url = "http://www.baidu.com"
        self.driver.get(url)
        self.driver.maximize_window()
        print(value)
        # 将从.json文件中读取出的数据用“||”分割成测试数据和期望的数据
        testdata, execptdata = tuple(value.strip().split("||"))
        # 设置隐式等待时间
        self.driver.implicitly_wait(10)
        try:
            self.driver.find_element_by_id("kw").send_keys(testdata)
            self.driver.find_element_by_id("su").click()
            time.sleep(3)
            # 断言期望结果是否出现在页面中
            self.assertTrue(execptdata in self.driver.page_source)
        except NoSuchElementException as e:
            logging.error(u"查找的页面元素不存在,异常堆栈信息为:" + str(traceback.format_exc()))
        except AssertionError as e:
            logging.info(u"搜索 '%s',期望 '%s' ,失败" % (testdata, execptdata))
        except Exception as e:
            logging.error(u"未知错误,错误信息:" + str(traceback.format_exc()))
        else:
            logging.info(u"搜索 '%s',期望 '%s' ,通过" % (testdata, execptdata))
 
    def tearDown(self):
        self.driver.quit()
 
 
if __name__ == '__main__':
    unittest.main()

执行测试

import unittest
from Run.HTMLTestRunner import HTMLTestRunner
from DataDrivenTest.DataDrivenTestByDDTHTR import DataDrivenTestByDDTHTR
import time
 
 
suit = unittest.TestSuite()
suit.addTest(unittest.makeSuite(DataDrivenTestByDDTHTR))
now = time.strftime("%Y-%m-%d-%H_%M_%S", time.localtime(time.time()))
filename = "F:\\DataDriven\\TestResults\\Results-" + now + "result.html"
print(filename)
fp = open(filename, 'wb')
runner = HTMLTestRunner(stream=fp, title='Result', description='TestReport')
runner.run(suit)
print("test_DrivenByFile finished")


方法封装

# encoding = utf-8
from Run import HTMLTestRunner
import unittest
import time
 
 
class StartTest(object):
 
    def __init__(self):
        print("generate test reports...")
 
    @staticmethod
    def starttest():
        test_suite = unittest.defaultTestLoader.discover('TestScripts', pattern='test*.py')
        now = time.strftime("%Y-%m-%d-%H_%M_%S", time.localtime(time.time()))
        filename = "D:\\Programs\\Python\\PythonUnittest\\Reports\\Results-" + now + "result.html"
        print(filename)
        fp = open("D:\\Programs\Python\\PythonUnittest\\Reports\\Results-" + now + "result.html", 'wb')
        runner = HTMLTestRunner.HTMLTestRunner(stream=fp, title='Result', description='Test_Report')
        runner.run(test_suite)
        print('Test reports generate finished')
 
 
if __name__ == '__main__':
    StartTest.starttest()

测试报告

Python自动化测试系列[v1.0.0][HTMLTestRunner测试报告]_第1张图片

你可能感兴趣的:(Python自动化测试系列[v1.0.0][HTMLTestRunner测试报告])