Python+selenium+unittest+HTMLTestReportCN unit test framework sharing

Before that, Xiaobian shared the Python+selenium+unittest+HTMLTestRunner unit test framework. After a lot of time, they didn't touch the code for a long time. When it was opened, it was a clanging error report. A line of striking red error report reminded me that the code was updating. Why don't you make progress? There is nothing in-depth. The God and what you have learned can bypass. Thank you !

About Unittest

unittest is a unit test framework of Python language. In the official documents of python, the unit test framework is introduced in detail. Interested readers can go to the official website to learn about it.

This chapter focuses on the application of unit test framework in automated testing. The unittest unit test framework provides solutions for creating test cases, test suites, and executing test cases in batches.

After python is installed successfully,

Unit test unit test framework can be imported and used directly. It belongs to standard library.

As the framework of unit test, unit test framework is also an agile test for the smallest module of the program.

In automated testing, although we don't need to do white box testing, we must know the unit testing framework of the language used. This is because when we have learned all the API of selenium 2, we will encounter the organization problem of use cases.

Although functional programming and object-oriented programming provide the reconstruction of code, it is impossible to write a function (method) to call and execute every test case written. With the unit test framework, you can create a class,

This class inherits the TestCase of unittest, so that each TestCase can be regarded as a smallest unit, organized by test suite, and executed directly at runtime. At the same time, test reports can be introduced. Then the report source we use here is HTMLTestReportCN

HTMLTestReportCN report style overview

If for python software testing, interface testing, automation testing, interview experience exchange. If you are interested, you can add software testing exchange: 718897738, and there will be technical exchanges with peers.

 

The following simple code is for reference only

# coding=utf-8
import os, time
import unittest
# Import the HTMLTestRunner library and put it at the beginning of the script
import HTMLTestReportCN
from selenium import webdriver
 
 
class TestAuto(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        print('start!')
 
        cls.dr = webdriver.Chrome()
        cls.dr.maximize_window()
        print('The test browser is:{0}'.format(cls.dr.name))
        time.sleep(1)
        print(u'Visit the background management system of patrol service teaching and testing environment')
        cls.dr.get('http:******.cn')
        print('Test address is:{0}'.format(cls.dr.current_url))
        time.sleep(1)
        xpath = cls.dr.find_element_by_xpath
        print(u'Click account login to enter account password to be input interface')
        xpath('//*[@id="app"]/div/div[2]/div[2]/div/div[1]/div').click()
        time.sleep(1)
        # Capture the account text column and input the account
        xpath('//*[@id="app"]/div/div[2]/div[2]/div/form/div[1]/input').send_keys(u'***')
        time.sleep(1)
        # Capture the password text bar and enter the password
        xpath('//*[@id="app"]/div/div[2]/div[2]/div/form/div[2]/input').send_keys(u'**..')
        time.sleep(1)
        print(u'Click to log in')
        xpath('//*[@id="app"]/div/div[2]/div[2]/div/form/div[3]/input').click()
        print(u'Initial environment, go to the next step of verification')
 
    @classmethod
    def tearDownclass(cls):
        print('end')
 
    def testCase_001(cls):
        '''Verify that the user is logged in'''
        print(u'test_001:Verify the background login of patrol and teaching')
        cls.assertEqual('http://xfdj.kt3.pagoda.com.cn/#/', cls.dr.current_url)
        url = 'http://xfdj.kt3.pagoda.com.cn/#/'
        if url == 'http://xfdj.kt3.pagoda.com.cn/#/':
            print('..Login successful')
        else:
            print('..Login failed')
 
    def testCase_002(cls):
        '''Enter the store scoring report page_Verify store view details function'''
        print(u'test_002:Verify the function of viewing store scoring details')
        xpath = cls.dr.find_element_by_xpath
        time.sleep(2)
        print(u'..Click the store scoring report details to enter the store scoring report page')
        xpath('/html/body/section/section/aside/ul/li[3]/span').click()
        time.sleep(1)
        print(u'..Refresh store rating report page')
        cls.dr.refresh()
        time.sleep(2)
        print(u'..Click the first page of the list,Line 5: store View button to enter store information details')
        xpath('/html/body/section/section/section/main/div[2]/div/div/div[2]/div[2]/div[1]/div[3]/table/tbody/tr[5]/td[10]/div/button/span').click()
        # Assert whether the shop detail page formulation element is displayed for mysterious visitors to determine whether the page successfully jumps to the detail page
        text = xpath('/html/body/section/section/section/main/div[2]/div/div/div[1]/div[1]/p[1]').text
        if text == 'Mystery customer':
            print('..Query store details succeeded')
        else:
            print('..Query details failed')
        cls.assertEqual(u'Mystery customer', text)
 
 
if __name__ == '__main__':
    suiteTest = unittest.TestSuite()
    suiteTest.addTest(TestAuto("testCase_001"))
    suiteTest.addTest(TestAuto("testCase_002"))
 
    # Get the current time in a certain time format (to prevent test report coverage)
    now = time.strftime(u'%Y-%m-%d-%H-%M-%S')
    # Determine the path to generate the report
    report_file = "F:\\python3\\report\\" + now + "_test_report.html"
    with open(report_file, 'wb') as report:
        runner = HTMLTestReportCN.HTMLTestRunner(stream=report, title=u'<http://***. CN > unit test report of patrol and teaching test environment ',
                                               description=u'Note: in order to reduce the time and labor cost and improve the quality of test transfer, unit test will be conducted on the basic functional modules for each code to be released in the test environment to further improve the test efficiency,The following is the execution result of use case, please refer to!',
                                               )
 
        runner.run(suiteTest)
        report.close()

Tags: Programming Python Selenium

Posted on Thu, 14 May 2020 00:01:11 -0700 by AoA_Falcon