1、正常引入unittest套件Demo,次案例继承第三讲案例
from ddt import ddt, file_data
from base.login_base import keyDemo
import unittest
@ddt
class CassDemo(unittest.TestCase):
@file_data('../data/search.yaml')
def test_01(self, input, click):
kd = keyDemo('Chrome')
kd.open('https://www.baidu.com')
kd.input(**input)
kd.click(**click)
kd.quite()
if __name__ == '__main__':
unittest.main()
2、(多组不同数据进行执行)yaml文件书写格式
-
url: https://www.baidu.com
input:
name: id
value: kw
txt: 玩技e族
click:
name: id
value: su
-
url: https://www.jd.com
input:
name: id
value: key
txt: 玩技e族
click:
name: xpath
value: //button[@class="button"]3、同样的业务流程输入不同的数据
from ddt import ddt, file_data
from base.login_base import keyDemo
import unittest
@ddt
class CassDemo(unittest.TestCase):
@file_data('../data/search.yaml')
def test_01(self, url, input, click):
kd = keyDemo('Chrome')
kd.open(url)
kd.input(**input)
kd.click(**click)
kd.sleep(3)
kd.quite()
if __name__ == '__main__':
unittest.main()
3、继续优化冗余代码,将共有部分继续优化
from ddt import ddt, file_data
from base.login_base import keyDemo
from time import sleep
import unittest
@ddt
class CassDemo(unittest.TestCase):
def setUp(self, ) -> None:
self.kd = keyDemo('Chrome')
self.kd.maxwindow()
def tearDown(self) -> None:
self.kd.quite()
@file_data('../data/search.yaml')
def test_01(self, url, input, click):
# self.kd = keyDemo('Chrome')
self.kd.open(url)
self.kd.input(**input)
self.kd.click(**click)
sleep(3)
# self.kd.quite()
if __name__ == '__main__':
unittest.main()
