使用unittest框架的自动化测试,报告一定很重要,目前介绍一个比较高大上的报告模板 BeautifulReport。如果首次使用的话需要安装 pip install beautifulreport

下面直接上代码,里面关键的地方通过注释体现

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。
 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一下生成的报告文件及报告内容:

unittest框架之 BeautifulReport 模板报告生成的正确姿势 Python 第1张

 

 unittest框架之 BeautifulReport 模板报告生成的正确姿势 Python 第2张

 

 

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄