Selenium (eighteen): unittest Unit testing framework (D) HTML test report

1. HTML test report

The test personnel, the test output is difficult to measure. In other words, the value of the testers more difficult to quantify and evaluate, I believe that the software testers deep. We spend a lot of time and effort doing automated testing as well. So, we need a beautiful and easy to understand test reports to demonstrate automated test results. Obviously, a simple Log files is not enough.

HTMLTestRunner extension is a standard library unittest Python unit testing framework, it is easy to use HTML to generate test reports. HTMLTestRunner is released under the BSD license.

Download: http://tungwaiyip.info/software/HTMLTestRunner.html

This extension is very simple, only a HTMLTestRunner.py file, after being selected right-click the shortcut menu, choose Save As to save it locally. Installation is very simple, you can copy it to the next Python installation directory.

Windows: Save the downloaded file to the ... \ under Python36 \ Lib directory. pycharm can not be used to find pycharm the lib directory, paste the file.

Linux: Ubuntu to, for example, you first need to open a terminal and find Python installation directory. After opening the terminal, Python commands input into the Python interactive mode, you can view the directory of the installation by the python sys.path. Copy the file as root HTMLTestRunner.py to the next /usr/local/Python3.6/dist-packages/ directory. Reference HTMLTestRunner module in Python interactive mode, if the system is not an error, then added successfully.

1.1 modify HTMLTestRunner

Because HTMLTestRunner.py is based on the Python 2 development, in order to support Python 3 environments, the need for part of them to be modified. Open the file by following HTMLTestRunner.py editor.

# 94 line 
Import the StringIO
change into:
import I

#第539行
self.outputBuffer = StringIO.StringIO()
change into:
self.outputBuffer = io.StringIO()

#631行
print >>sys.stderr, '\nTime Elapsed: %s' % (self.stopTime-self.startTime)
change into:
print (sys.stderr, '\nTime Elapsed: %s' % (self.stopTime-self.startTime))

#第642行
if not rmap.has_key(cls):
change into:
if not cls in rmap:

# # 766 line 
Uo = O.Decode ( ' Latin-1 ' )
change into:
u = and

# # 772 line 
Ue = O.Decode ( ' Latin-1 ' )
change into:
u = e

1.2 generate HTML test report

Let's continue to generate HTMLTestRunner test_baidu.py file as an example test report.

from selenium import webdriver
import unittest
from HTMLTestRunner import HTMLTestRunner
from time import sleep

class Baidu(unittest.TestCase):

    def setUp(self):
        self.driver = webdriver.Chrome()
        self.driver.implicitly_wait(10)
        self.base_url = "http://www.baidu.com"

    def test_baidu_search(self):
        driver = self.driver
        driver.get(self.base_url)
        driver.find_element_by_id("kw").send_keys("HTMLTestRunner")
        driver.find_element_by_id("su").click()
        sleep(5)

    def tearDown(self):
        self.driver.quit()

if __name__ == "__main__":
    testunit = unittest.TestSuite()
    testunit.addTest(Baidu("test_baidu_search"))

    # Custom report storage path 
    FP = Open ( ' ./result.html ' , ' WB ' )
     # define test report 
    Runner = HTMLTestRunner (Stream = FP,
                            title = ' Baidu search test report ' ,
                            Description = ' implementation use cases: ' ,
                            verbosity=2
                            )
    runner.run (TestUnit) # run a test case 
    fp.close () # close the report file

First, the imported modules come with HTMLTestRunner import.

Secondly, the open () method to write binary module result.html open in the current directory, if not, the file is automatically cut.

Next, under the category HTMLTestRunner HTMLTestRunner call module. stream designated test report file, title used to define the test report title, description for subtitle-defined test reports.

Finally, () method to run the test suite HTMLTestRunner assembled by the test run. Last) turn off the test report file by close (.

Use case has finished running, open the "result.html" file in the current directory view the test report generation.

 

1.3 Test report more readable

Now the generated test report is not legible, because it listed a bunch of test class and test methods, we need to carefully position the test class and test methods named in order to improve the readability of test reports, if any name is "test_case1", "test_case2" and so on, then the report will lose its readability, perhaps a long time even script developers are not clear "test_case1" What is the function of the test.

When writing functional test cases, each test case has a title, then we can also add the title of it is automated test cases? Before that let's learn another knowledge: Python comments. Notes two Python, called Comment, another called doc string, the former is a common annotation, which is used to describe functions, classes, and methods.

 在类或方法的下方,通过三引号(""" """或"" "")来添加doc string类型的注释,这类注释在平时调用的时候不显示,可以通过help()方法来查看类或方法的这种注释。

回到问题的原点,HTMLTestRunner可以读取doc string类型的注释。所以我们只需给测试类或方法添加这种类型的注释即可。

class Baidu(unittest.TestCase):
    ''' 百度搜索测试 '''

    def test_baidu_search(self):
        ''' 搜索关键字:HTMLTestRunner '''

再次运行测试用例,查看测试报告。

 

1.4 测试报告文件名

在每次运行测试之前,都要手动修改报告的名称,如果忘记修改,就会把之前的报告覆盖,这样做显得很麻烦,那么有没有办法可以使每次删除的报告名称都不重复并且有意义呢?最好的方法是在报告名称中加入当前时间,这样生成的报告既不会重叠,又能更清晰的知道报告生成的时间。

Python的time模块中提供了丰富的关于时间操作的方法,可以利用这些方法来完成这个需求。

time.time():获取当前时间戳。

time.ctime():当前时间的字符串形式。

time.localtime():当前时间的struct_time形式。

time.strftime():用来获得当前时间,可以将时间格式化为字符串。

 Python中时间日期格式化符号(区分大小写)。

 %y 两位数的年份表示(00-99)

%Y 四位数的年份表示(000-9999)

%m 月份(01-12)

%d 月内中的一天(0-31)

%H 24小时制小时数(0-23)

%I 12小时制小时数(01-12)

%M 分钟数(00=59)

%S 秒(00-59)

%a 本地简化星期名称

%A 本地完整星期名称

%b 本地简化的月份名称

%B 本地完整的月份名称

%c 本地相应的日期表示和时间表示

%j 年内的一天(001-366)

%p 本地A.M.或P.M.的等价符

%U 一年中的星期数(00-53)星期天为星期的开始

%w 星期(0-6),星期天为星期的开始

%W 一年中的星期数(00-53)星期一为星期的开始

%x 本地相应的日期表示

%X 本地相应的时间表示

%Z 当前时区的名称

%% %号本身

继续打开测试用例,做如下修改。 

import time

#.............

if __name__ == "__main__":
    testunit = unittest.TestSuite()
    testunit.addTest(Baidu("test_baidu_search"))

    #按照一定格式获取当前时间
    now = time.strftime("%Y-%m-%d %H_%M_%S")

    #定义报告存放路径
    filename = './'+now+'result.html'
    fp = open(filename,'wb')
    #定义测试报告
    runner = HTMLTestRunner(stream=fp,
                            title='百度搜索测试报告',
                            description='用例执行情况:',
                            verbosity=2
                            )
    runner.run(testunit) #运行测试用例
    fp.close() #关闭报告文件

通过strftime()方法以指定的格式获取当前时间,将当前时间的字符串赋值给now变量。将now通过加号(+)拼接到生成的测试报告的文件名中。再次运行测试用例,生成测试报告。

 

 

1.5 项目集成测试报告

目前HTMLTestRunner只是针对单个测试文件生成测试报告,我们的最终目的是希望将它集成到runtest.py文件中,使其作用于整个测试项目。下面打开runtest.py文件进行修改,还有就是把test_baidu.py改回来。

import unittest
from HTMLTestRunner import HTMLTestRunner
import time

#指定测试用例为当前文件夹下的test_case目录
test_dir = './test_case'
discover = unittest.defaultTestLoader.discover(test_dir,pattern='test_*.py')

if __name__ == "__main__":
    # 按照一定格式获取当前时间
    now = time.strftime("%Y-%m-%d %H_%M_%S")

    # 定义报告存放路径
    filename = './' + now + 'result.html'
    fp = open(filename, 'wb')
    # 定义测试报告
    runner = HTMLTestRunner(stream=fp,
                            title='百度搜索测试报告',
                            Description = ' implementation use cases: ' ,
                            verbosity=2
                            )
    runner.run (the Discover)   # run a test case 
    fp.close ()   # close the report file

Generated HTML test report:

Guess you like

Origin www.cnblogs.com/liuhui0308/p/11975305.html