前言php
# coding:utf-8
import unittest
from
selenium import webdriver
import time
class Test1(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.driver = webdriver.Firefox()
def setUp(self):
self.driver.get("http://www.cnblogs.com/yoyoketang/")
def test_01(self):
time.sleep(3)
t = self.driver.title
print t
# 隨便寫的用例,沒寫斷言
def test_02(self):
time.sleep(3)
t = self.driver.title
print t
h = self.driver.window_handles
print h
# 隨便寫的用例,沒寫斷言
@classmethod
def tearDownClass(cls):
cls.driver.quit()
if __name__ == "__main__":
unittest.main()
|
#!/usr/bin/env python # -*- coding:utf-8 -*- #設置路徑:Defualt Settings---Editor--File and Code Templates import unittest import HTMLTestRunnerNew import os from tomorrow import threads # 獲取路徑# 當前腳本所在目錄 curpath = os.path.dirname(os.path.realpath(__file__)) #測試用例路徑 casepath = os.path.join(curpath, "case") #測試報告路徑 reportpath = os.path.join(curpath, "report") def add_case(case_path=casepath, rule="test*.py"): '''加載全部的測試用例''' discover = unittest.defaultTestLoader.discover(case_path, pattern=rule,top_level_dir=None) return discover @threads(5) def run_case(all_case, report_path=reportpath, nth=0): '''執行全部的用例, 並把結果寫入測試報告''' report_abspath = os.path.join(report_path, "result%s.html"%nth) with open(report_abspath, "wb+") as file: runner = HTMLTestRunnerNew.HTMLTestRunner(stream=file, title=u'自動化測試報告,測試結果以下:',description=u'用例執行狀況:') # 調用add_case函數返回值 runner.run(all_case) if __name__ == "__main__": # 用例集合 cases = add_case() # 以前是批量執行,這裏改爲for循環執行 for i, j in zip(cases, range(len(list(cases)))): run_case(i, nth=j) # 執行用例,生成報告 # print(i,j)
|