unittest 参数化parametrize结合多线程

  • 对unittest进行改造,以使能实现传参功能:
#大牛测试:qq2574674466
#coding=utf-8
import unittest
class ParaCase(unittest.TestCase):
   #unittest增加参数化
    def __init__(self, methodName='Tests', param=None):
        super(ParaCase, self).__init__(methodName)
        self.driver = param
    def setUp(self):
        self.driver.maximize_window()

 

 

 

  •  以下方法是为了创建测试套件,此套件可以在被继承子类调用并在子类中设置需要运行的方法,只需通过param参数
@staticmethod

    def parametrize(testcase, param=None):
        testloader = unittest.TestLoader()
        testnames = testloader.getTestCaseNames(testcase)
        suite = unittest.TestSuite()
        for name in testnames:
            suite.addTest(testcase(name, param=param))
        return suite

 

 

 

#大牛测试:qq 2574674466
#
coding=utf-8 from thred_unit.basic_unit import ParaCase import time class DetailCase(ParaCase): #以下函数是为了测试携程网登录功能。 def test_login(self): time.sleep(2) self.driver.get('https://passport.ctrip.com/user/login?') self.driver.find_element_by_id('nloginname').send_keys("TimTest") self.driver.find_element_by_id('npwd').send_keys("TimTest") self.driver.find_element_by_id('nsubmit').click()

 

 


#大牛测试:qq 2574674466

#
coding=utf-8 import unittest import threading from thred_unit.basic_case import ParaCase from thred_unit.basic_case import DetailCase from selenium import webdriver #本例实现本地多个浏览器对同一脚本并发操作 #继承父类threading.Thread class myThread (threading.Thread): def __init__(self, device): threading.Thread.__init__(self) self.device=device def run(self): print ("Starting " + self.name) print ("Exiting " + self.name) run_suite(self.device) #定义多线程实际要执行的操作 def run_suite(device): suite = unittest.TestSuite() suite.addTest(ParaCase.parametrize(DetailCase, param=device)) unittest.TextTestRunner(verbosity=1).run(suite) if __name__ == '__main__': #以下代是在本地通过同时访问多浏览器 dr = [webdriver.Firefox(executable_path='需要输入正确的firefox webdriver路径'), webdriver.Chrome('需要输入正确的Chrome webdriver路径’)] for i in range(len(dr)): print(dr[i]) th = myThread(dr[i]) th.start() th.join()

运行结果:

 

posted @ 2021-11-30 08:57  大牛测试技术  阅读(72)  评论(0编辑  收藏  举报