Chapter 7 unittest extension - 7.1 HTML test report - Selenium3 automated testing

Chapter 7 unittest extension

In Chapter 6 , we introduce the main features of unittest , but if you only use it to write Web automated testing , is still slightly less . For example , it can not generate the report in HTML format , it does not provide parametric function . However , we can make use of third-party extensions to compensate for these deficiencies .

7.1 HTML test report

HTMLTestRunner is an extension unittest, which can generate easy-to-use HTML test report. HTMLTestRunner is released under the BSD license.
Download: http://tungwaiyip.info/software/HTMLTestRunner.html

Because the extension does not support Python 3, so we do some modifications so that it can run under Python 3.
GitHub Address: https://github.com/defnngj/HTMLTestRunner

7.1.2 generated HTML test report

If you want to generate test reports with HTMLTestRunner, please see the implementation section 6.1.4 run_tests.py book files. Test case execution is provided by run TextTestRunner class () method is completed. It should replace TextTestRunner class HTMLTestRunner.py file HTMLTestRunner class.
Open HTMLTestRunner.py file on line 877 (if the code is updated, the line numbers will change) can be found HTMLTestRunner class.

Use Sublime open .py file, look for the keyword "HTMLTestRunner"

This code is partially achieved HTMLTestRunner class, mainly depends on parameters __init __ () initialization method.

● stream: Specifies the test report generated HTML files are required.
● verbosity: Specifies the level of logging, default is 1. If you want a more detailed log, you can modify the parameters 2.
● title: Specifies the title of test cases, default is None.
● description: Specifies the description of the test case, the default is None.

In HTMLTestRunner class, the same test suite to test run by the run () method. Run_tests.py modify the file as follows.

Import the unittest
 from HTMLTestRunner Import HTMLTestRunner 


    # directory as the current directory defined test 
    test_dir = ' ./test_case ' 
    SUIT = unittest.defaultTestLoader.discover (test_dir, pattern = ' Test * .py ' ) 

IF  the __name__ == ' __main__ ' : 


    # take the current date and time 
    now_time The time.strftime = ( " % D%% Y-M-% H_% of M_% S " ) 
    html_report = ' ./test_report/ ' + now_time + ' and result.html '
    fp = Open (html_report, ' wb ' )
     # call HTMLTestRunner, run test cases 
    Runner = HTMLTestRunner (= Stream fp, 
                            title = " Baidu search Test Report " , 
                            the Description = " Operating environment: Windows 10, Chrome browser " 
                            ) 
    Runner. RUN (SUIT) 
    fp.close ()
    
View Code

First, open () method to open result.html file for writing test results. If you do not result.html file, the file is automatically created, and the file object to the initialization parameter stream HTMLTestRunner class. Then, call HTMLTestRunner class run () method to run the test suite. Finally, close result.html file.

/Test_report/result.html open the file, you will get a report in HTML format. HTMLTestRunner shown in the test report.

 

Guess you like

Origin www.cnblogs.com/MarlonKang/p/12448059.html