使用unittest框架的自動化測試,報告一定很重要,目前介紹一個比較高大上的報告模板 BeautifulReport。如果首次使用的話需要安裝 pip install beautifulreport
下面直接上代碼,里面關鍵的地方通過注釋體現
1 # -*- coding:utf-8 -*- 2 ''' 3 # @Time : 2019/12/3 16:50 4 # @Author : nihaoya 5 # @FileName: WeiBo_test.py 6 # @Software: PyCharm 7 ''' 8 import os 9 import time 10 import unittest 11 from appium import webdriver 12 from selenium.webdriver.common.by import By 13 from selenium.webdriver.support.ui import WebDriverWait 14 from selenium.webdriver.support import expected_conditions as EC 15 from BeautifulReport import BeautifulReport as bf 16 17 server = 'http://localhost:4723/wd/hub' 18 desired_capabilities = { 19 'platformName': 'Android', 20 'deviceName': 'wohenhao', 21 'appPackage': 'com.sina.weibo', 22 'appActivity': 'com.sina.weibo.VisitorMainTabActivity', 23 'autoGrantPermissions': True # 添加這個是防止每次拉起應用時要重新授權 24 } 25 26 driver = webdriver.Remote(server, desired_capabilities) 27 wait = WebDriverWait(driver, 30) 28 29 30 class WeiBo(unittest.TestCase): 31 def setUp(self) -> None: 32 print(time.strftime("%Y-%m-%d %H:%M:%S"), "start to test") 33 34 def tearDown(self) -> None: 35 print(time.strftime("%Y-%m-%d %H:%M:%S"), "end test") 36 37 def test_a_switch_guest_mode(self): 38 print(time.strftime("%Y-%m-%d %H:%M:%S"), "switch to guest mode") 39 if wait.until(EC.element_to_be_clickable((By.ID, "com.sina.weibo:id/tv_title_lookaround"))): 40 driver.find_element_by_id("com.sina.weibo:id/tv_title_lookaround").click() 41 42 def test_b_browsing(self): 43 print(time.strftime("%Y-%m-%d %H:%M:%S"), "begging to browse the content") 44 for i in range(5): 45 print((time.strftime("%Y-%m-%d %H:%M:%S") + " This is the {} time to refresh").format(i+1)) 46 time.sleep(5) 47 driver.swipe(400, 100, 400, 350, 10) 48 49 50 51 if __name__ == "__main__": 52 suite = unittest.TestLoader().loadTestsFromTestCase(WeiBo) 53 # unittest.TextTestRunner().run(suite) 54 # bf(suite).report("WebBo", "WeiBoTest") 55 run = bf(suite) 56 run.report(filename=u"./report/WeiBo報告_" + time.strftime("%Y-%m-%d_%H_%M_%S"), description=u"以游客形式瀏覽微博") # 這個filename關鍵字參數中,給的值一定不能出現冒號,否則最后生成報告時必定提示參數異常
注意:
1、如果是使用pycharm的話,一定不要在pycharm中執行腳本,它是不會生成報告的,原因是用pycharm自帶的unittest執行的,並不會走腳本中的 main 方法,所以也就不會有報告。正確的姿勢就是在命令行通過 python xxx.py 進行執行,這樣就肯定會有報告生成。
2、在最后報告生成時至於filename那塊不能帶有冒號,大家可以在windows端自行去新建一個文件,以冒號(英文)的形式去創建一個文件,它也會提示這9個特殊字符是不能被包含的,例如: \ / : * ? " < > |
下面show一下生成的報告文件及報告內容: