引入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, "http://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)