python selenium-6 HTML测试报告

1.生成HTML测试报告

import unittest,sys
from selenium import webdriver
from time import sleep
class TestBaidu(unittest.TestCase):
    def setUp(self):
        driver_path='/'.join(sys.path[0].split('/')[:-2])+"/driver/chromedriver"
        self.driver=webdriver.Chrome(executable_path=driver_path)
        self.driver.implicitly_wait(30)
        self.base_url="http://www.baidu.com"
    def test(self):
        driver=self.driver
        driver.get(self.base_url+"/")
        driver.find_element_by_xpath("//input[@id='kw' and @class='s_ipt']").clear()
        driver.find_element_by_xpath("//input[@id='kw' and @class='s_ipt']").send_keys("selenium")
        driver.find_element_by_xpath("//input[@value='百度一下' and @id='su']").click()
        result_text = driver.find_element_by_xpath("//span[@class='nums_text']").text
        assert "百度为您找到相关结果约" in result_text
        sleep(3)
    def tearDown(self):
        self.driver.quit()
if __name__=="__main__":
    unittest.main()

runtest.py

import unittest
from HTMLTestRunner import HTMLTestRunner
test_dir="./testcase/testsearch"
discover=unittest.defaultTestLoader.discover(test_dir,pattern="test*.py")
if __name__=="__main__":
    fp = open("report/result.html","wb")
    runner=HTMLTestRunner(stream=fp,title="百度搜索报告",description="用例执行情况")
    runner.run(discover)
    fp.close()
  • stream:指定文件
  • title:定义测试报告的标题
  • description:定义测试报告的副标题

2.增加描述,使报告更易读

2.1添加描述

python的注释有2中,comment普通的注释,doc string用于函数、类、方法的描述。
doc string有双引号、三双引号、三单引号
双引号只适合单行,三双引号与三单引号适合多行
如果类和方法有多个引号描述,只会把紧跟类名或方法名后的第一个描述作为doc string,如下图灰色描述。

def add(a,b):
    "add()函数需要2个入参,并返回两个参数相加的值"
    return a + b

进入python交互界面

from test import add
help(add)
#q退出

2.2测试用例添加描述

注意:HTMLTestRunner只能识别到第一行,即使是三双引号或三单引号的多行描述也只打印第一行。如结果图中test_search2
test_search1.py

# #没有导入pymysql.cursors
import unittest,sys
from selenium import webdriver
from time import sleep
class TestBaidu(unittest.TestCase):
    "百度搜索测试"
    def setUp(self):
        driver_path='/'.join(sys.path[0].split('/')[:-2])+"/driver/chromedriver"
        self.driver=webdriver.Chrome(executable_path=driver_path)
        self.driver.implicitly_wait(30)
        self.base_url="http://www.baidu.com"
    def test(self):
        "搜索关键字:selenium"
        driver=self.driver
        driver.get(self.base_url+"/")
        driver.find_element_by_xpath("//input[@id='kw' and @class='s_ipt']").clear()
        driver.find_element_by_xpath("//input[@id='kw' and @class='s_ipt']").send_keys("selenium")
        driver.find_element_by_xpath("//input[@value='百度一下' and @id='su']").click()
        result_text = driver.find_element_by_xpath("//span[@class='nums_text']").text
        assert "百度为您找到相关结果约" in result_text
        sleep(3)
    def tearDown(self):
        self.driver.quit()
if __name__=="__main__":
    unittest.main()

test_search2.py

import unittest,sys
from selenium import webdriver
from time import sleep
class TestBaidu(unittest.TestCase):
    '''百度
    搜索测试'''
    def setUp(self):
        driver_path='/'.join(sys.path[0].split('/')[:-2])+"/driver/chromedriver"
        self.driver=webdriver.Chrome(executable_path=driver_path)
        self.driver.implicitly_wait(30)
        self.base_url="http://www.baidu.com"
    def test(self):
        """搜索关键字:
        Jenkins"""
        driver=self.driver
        driver.get(self.base_url+"/")
        driver.find_element_by_xpath("//input[@id='kw' and @class='s_ipt']").clear()
        driver.find_element_by_xpath("//input[@id='kw' and @class='s_ipt']").send_keys("jenkins")
        driver.find_element_by_xpath("//input[@value='百度一下' and @id='su']").click()
        result_text = driver.find_element_by_xpath("//span[@class='nums_text']").text
        assert "百度为您找到相关结果约" in result_text
        sleep(3)
    def tearDown(self):
        self.driver.quit()
if __name__=="__main__":
    unittest.main()

runtest.py

import unittest
from HTMLTestRunner import HTMLTestRunner
test_dir="./testcase/testsearch"
discover=unittest.defaultTestLoader.discover(test_dir,pattern="test*.py")
if __name__=="__main__":
    fp = open("report/result.html","wb")
    runner=HTMLTestRunner(stream=fp,title="百度搜索报告",description="用例执行情况")
    runner.run(discover)
    fp.close()

3.为测试报告文件添加时间戳,便于识别

runtest.py

import unittest
from time import strftime
from HTMLTestRunner import HTMLTestRunner
test_dir="./testcase/testsearch"
discover=unittest.defaultTestLoader.discover(test_dir,pattern="test*.py")
if __name__=="__main__":
    filename="report/"+strftime("%Y_%m_%d_%H_%M_%S")+"_result.html"
    fp = open(filename,"wb")
    runner=HTMLTestRunner(stream=fp,title="百度搜索报告",description="用例执行情况")
    runner.run(discover)
    fp.close()
posted on 2018-11-28 11:08  singleSpace  阅读(288)  评论(0编辑  收藏  举报