5.测试报告

引入HTMLTestRunner.py, 是Python标准库unittest模块的一个扩展。它可以生成直观的HTML测试报告。

HTMLTestRunner.py文件地址:http://tungwaiyip.info/software/HTMLTestRunner.html

这是针对Python2.7版本,那么对于Python3.x的使用,需要改动几处。

同时谢谢 http://www.bubuko.com/infodetail-529431.html的分享。

具体改动如下:

第94行,将import StringIO修改成import io

第539行,将self.outputBuffer = StringIO.StringIO()修改成self.outputBuffer= io.StringIO()

第631行,将print >> sys.stderr, ‘\nTime Elapsed: %s‘ %(self.stopTime-self.startTime)修改成print(sys.stderr, ‘\nTimeElapsed: %s‘ % (self.stopTime-self.startTime))

第642行,将if not rmap.has_key(cls):修改成if notcls in rmap:

第766行,将uo = o.decode(‘latin-1‘)修改成uo = e

第775行,将ue = e.decode(‘latin-1‘)修改成ue = e

第778行,将output = saxutils.escape(uo+ue),修改成output = saxutils.escape(str(uo)+str(ue)),

然后将文件HTMLTestRunner.py放到C:\Python27\Lib目录中,检验是否加载成功,在Python IDLE 中输入

import HTMLTestRunner

若无报错,那么加载成功。

python 中os.path 路径模块的学习地址

http://www.runoob.com/python/python-os-path.html 

os.getcwd()   当前路径

os.path.dirname(os.getcwd()) 上级目录


附上我的代码

# -*- coding: utf-8 -*-

from seleniumimport webdriver

from selenium.webdriver.common.byimport By

from selenium.common.exceptionsimport NoSuchElementException

import os

import unittest

import HTMLTestRunner

import time

class SearchTest(unittest.TestCase):

@classmethod

    def setUpClass(cls):

# 得到IE驱动的地址

        cls.dir = os.path.dirname(__file__)

# dir得到的是python01.py文件所在的位置

        cls.ie_driver_path =cls.dir +"\IEDriverServer.exe"

        cls.driver = webdriver.Ie(cls.ie_driver_path)

cls.driver.implicitly_wait(30)

cls.driver.maximize_window()

cls.driver.get("https://www.baidu.com/")

@classmethod

    def tearDownClass(cls):

cls.driver.quit()

def test_search_field_exist(self):

# 检查搜索框存在

        self.assertTrue(self.is_element_present(By.NAME, "wd"))

def test_search(self):

self.search_filed =self.driver.find_element(By.NAME, "wd")

self.search_filed.clear()

self.search_filed.send_keys(u"炫彩流萤")

self.search_filed.submit()

self.products =self.driver.find_element(By.XPATH, "//h3[@class='t']/a").text

self.assertEqual(16, len(self.products))

print self.products

def test_search_num(self):

self.search_filed =self.driver.find_element(By.NAME, "wd")

self.search_filed.clear()

self.search_filed.send_keys(u"星期天")

self.search_filed.submit()

self.numText =self.driver.find_element(By.CLASS_NAME, "nums_text").text

print self.numText

def is_element_present(self, how, what):

"""

Utility method to check presence of an element on page

:params how: By locator type

:params what: locator value

"""

        try:

self.driver.find_element(by=how, value=what)

except NoSuchElementException, e:

return False

        return True

# if __name__ == '__main__':

#    unittest.main(verbosity=2)

# 将测试用例集放在TestSuit中,然后调用TextTestRunner

search_tests = unittest.TestLoader().loadTestsFromTestCase(SearchTest)

smoke_tests = unittest.TestSuite(search_tests)

dir = os.path.dirname(os.getcwd())

now = time.strftime("%Y-%m-%d-%H_%M_%S",time.localtime(time.time()))

outfile =open(dir +"\\report\\result_" + now +".html", "w")

# configure HTMLTestRunner options

runner = HTMLTestRunner.HTMLTestRunner(

stream=outfile,

                title=u'测试报告',

                description=u'测试执行情况'

                )

# run the suite

runner.run(smoke_tests)


5.测试报告_第1张图片


5.测试报告_第2张图片

你可能感兴趣的:(5.测试报告)