本文介绍一个面向对象设计领域里,很常见的一种思想,继承。继承有很多好处,常听到的一句话就是,子类能够直接使用父类的方法,这样就可以减少子类代码量。其实,在自动化测试框架设计过程中,是很有必要把继承加入到你的测试脚本中去。
现在baidu目录下建一个inherit.py文件,这个就是我们的父类,里面有一个打开chrome浏览器和打开百度首页的方法。代码如下:
import time
from selenium import webdriver
class Inherit(object):
def open_baidu(self):
driver = webdriver.Chrome()
driver.get("https://www.baidu.com")
time.sleep(1)
driver.quit()
然后再建一个testinherit.py的文件,测试一下继承这个(Inherit)父类,代码如下:
from baidu.inherit import Inherit
class Class1(Inherit):
def test_inherit(self):
self.open_baidu()
test = Class1()
test.test_inherit()
继承可以减少代码的繁琐,后面自动化测试框架会经常用到。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)