用例公共部分合并

在baidu.py创建类,并包含方法'su':

from selenium import webdriver
from time import sleep


class baidu():
    def su(self,contents):
        driver = webdriver.Chrome()
        driver.get('https://www.baidu.com/')
        for content in contents:
            driver.find_element_by_id('kw').clear()
            driver.find_element_by_id('kw').send_keys(content)
            driver.find_element_by_id('su').click()
            sleep(2)
        driver.quit()


if __name__ == '__main__':
    contents=['数据驱动测试','循环搜索','搜索一下','啦啦啦']
    baidu().su(contents)

二、创建StarEnd.py

from Pr_Project.baidu import baidu
import unittest


class Test_StarEnd(unittest.TestCase):
'''
创建Test_StarEnd 集成unittest.TestCase 并实现setUp与tearDown 方法
'''  
    def setUp(self):
        print('初始化')

    def tearDown(self):
        print('关闭浏览器')


class Baidu_test1(Test_StarEnd):#创建测试类 并继承Test_StarEnd
    def test_baidu(self):
        print('test_case')
        contents = ['数据驱动测试', '循环搜索', '搜索一下', '啦啦啦']
        baidu().su( contents)


class Baidu_test2(Test_StarEnd):
    def test_baidu(self):
        print('test_case2')
        contents = ['哆啦恶魔', '循环搜索2']
        baidu().su(contents)


if __name__ == '__main__':
    unittest.main()#执行全部用例 顺序依次为:测试类或者测试方法的数字与字母顺序0~9,A-Z
    suite = unittest.TestSuite()
    suite.addTest(Baidu_test1('test_baidu'))
    suite.addTest(Baidu_test2('test_baidu'))

    runner = unittest.TextTestRunner()
    runner.run(suite)

此处仅适用于setUp与tearDown 都一致的

你可能感兴趣的:(用例公共部分合并)