营销式网站,深圳房管局官网查询系统,个人网页,php能开发大型网站数据驱动模式的测试好处相比普通模式的测试就显而易见了吧#xff01;使用数据驱动的模式#xff0c;可以根据业务分解测试数据#xff0c;只需定义变量#xff0c;使用外部或者自定义的数据使其参数化#xff0c;从而避免了使用之前测试脚本中固定的数据。可以将测试脚本…
数据驱动模式的测试好处相比普通模式的测试就显而易见了吧使用数据驱动的模式可以根据业务分解测试数据只需定义变量使用外部或者自定义的数据使其参数化从而避免了使用之前测试脚本中固定的数据。可以将测试脚本与测试数据分离使得测试脚本在不同数据集合下高度复用。不仅可以增加复杂条件场景的测试覆盖还可以极大减少测试脚本的编写与维护工作。
下面将使用Python下的数据驱动模式(ddt)库结合unittest库以数据驱动模式创建百度搜索的测试。
ddt库包含一组类和方法用于实现数据驱动测试。可以将测试中的变量进行参数化。
可以通过python自带的pip命令进行下载并安装pip install ddt .
一个简单的数据驱动测试
为了创建数据驱动测试需要在测试类上使用ddt装饰符在测试方法上使用data装饰符。data装饰符把参数当作测试数据参数可以是单个值、列表、元组、字典。对于列表需要用unpack装饰符把元组和列表解析成多个参数。
下面实现百度搜索测试传入搜索关键词和期望结果代码如下
import unittest
from selenium import webdriver
from ddt import ddt, data, unpackddt
class SearchDDT(unittest.TestCase):docstring for SearchDDTdef setUp(self):self.driver webdriver.Chrome()self.driver.implicitly_wait(30)self.driver.maximize_window()self.driver.get(https://www.baidu.com)# specify test data using data decoratordata((python, PyPI))unpackdef test_search(self, search_value, expected_result):search_text self.driver.find_element_by_id(kw)search_text.clear()search_text.send_keys(search_value)search_button self.driver.find_element_by_id(su)search_button.click()tag self.driver.find_element_by_link_text(PyPI).textself.assertEqual(expected_result, tag)def tearDown(self):self.driver.quit()if __name__ __main__:unittest.main(verbosity2)
在test_search()方法中search_value与expected_result两个参数用来接收元组解析的数据。当运行脚本时ddt把测试数据转换为有效的python标识符生成名称为更有意义的测试方法。结果如下 现在我也找了很多测试的朋友做了一个分享技术的交流群共享了很多我们收集的技术文档和视频教程。
如果你不想再体验自学时找不到资源没人解答问题坚持几天便放弃的感受
可以加入我们一起交流。而且还有很多在自动化性能安全测试开发等等方面有一定建树的技术大牛
分享他们的经验还会分享很多直播讲座和技术沙龙
可以免费学习划重点开源的
qq群号110685036 使用外部数据的数据驱动测试
如果外部已经存在了需要的测试数据如一个文本文件、电子表格或者数据库那也可以用ddt来直接获取数据并传入测试方法进行测试。
下面将借助外部的CSV(逗号分隔值)文件和EXCLE表格数据来实现ddt。
通过CSV获取数据
同上在data装饰符使用解析外部的CSVtestdata.csv来作为测试数据代替之前的测试数据。其中数据如下 接下来先要创建一个get_data()方法其中包括路径这里默认使用当前路径、CSV文件名。调用CSV库去读取文件并返回一行数据。再使用ddt及data实现外部数据驱动测试百度搜索代码如下
import csv, unittest
from selenium import webdriver
from ddt import ddt, data, unpackdef get_data(file_name):# create an empty list to store rowsrows []# open the CSV filedata_file open(file_name, r)# create a CSV Reader from CSV filereader csv.reader(data_file)# skip the headersnext(reader, None)# add rows from reader to listfor row in reader:rows.append(row)return rowsddt
class SearchCSVDDT(unittest.TestCase):def setUp(self):self.driver webdriver.Chrome()self.driver.implicitly_wait(30)self.driver.maximize_window()self.driver.get(https://www.baidu.com)# get test data from specified csv file by using the get_data funciondata(*get_data(testdata.csv))unpackdef test_search(self, search_value, expected_result):search_text self.driver.find_element_by_id(kw)search_text.clear()search_text.send_keys(search_value)search_button self.driver.find_element_by_id(su)search_button.click()tag self.driver.find_element_by_link_text(PyPI).textself.assertEqual(expected_result, tag)def tearDown(self):self.driver.quit()if __name__ __main__:unittest.main(verbosity2)
测试执行时data将调用get_data()方法读取外部数据文件并将数据逐行返回给data。执行的结果也同上~ 如果对软件测试、接口测试、自动化测试、面试经验交流。感兴趣可以加软件测试交流1085991341还会有同行一起技术交流。
通过Excel获取数据
测试中经常用Excle存放测试数据同上在也可以使用data装饰符来解析外部的CSVtestdata.csv来作为测试数据代替之前的测试数据。其中数据如下 接下来先要创建一个get_data()方法其中包括路径这里默认使用当前路径、EXCEL文件名。调用xlrd库去读取文件并返回数据。再使用ddt及data实现外部数据驱动测试百度搜索代码如下
import xlrd, unittest
from selenium import webdriver
from ddt import ddt, data, unpackdef get_data(file_name):# create an empty list to store rowsrows []# open the CSV filebook xlrd.open_workbook(file_name)# get the frist sheetsheet book.sheet_by_index(0)# iterate through the sheet and get data from rows in listfor row_idx in range(1, sheet.nrows): #iterate 1 to maxrowsrows.append(list(sheet.row_values(row_idx, 0, sheet.ncols)))return rowsddt
class SearchEXCLEDDT(unittest.TestCase):def setUp(self):self.driver webdriver.Chrome()self.driver.implicitly_wait(30)self.driver.maximize_window()self.driver.get(https://www.baidu.com)# get test data from specified excle spreadsheet by using the get_data funciondata(*get_data(TestData.xlsx))unpackdef test_search(self, search_value, expected_result):search_text self.driver.find_element_by_id(kw)search_text.clear()search_text.send_keys(search_value)search_button self.driver.find_element_by_id(su)search_button.click()tag self.driver.find_element_by_link_text(PyPI).textself.assertEqual(expected_result, tag)def tearDown(self):self.driver.quit()if __name__ __main__:unittest.main(verbosity2)
与上面读取CVS文件一样测试执行时data将调用get_data()方法读取外部数据文件并将数据逐行返回给data。执行的结果也同上~
如果想从数据库的库表中获取数据同样也需要一个get_data()方法并且通过DB相关的库来连接数据库、SQL查询来获取测试数据。
以上就是本文的全部内容希望对大家的学习有所帮助。有被帮助到的朋友欢迎点赞评论。 文章转载自: http://www.morning.cnqwn.cn.gov.cn.cnqwn.cn http://www.morning.lqljj.cn.gov.cn.lqljj.cn http://www.morning.rqlzz.cn.gov.cn.rqlzz.cn http://www.morning.fewhope.com.gov.cn.fewhope.com http://www.morning.mtcnl.cn.gov.cn.mtcnl.cn http://www.morning.xkzmz.cn.gov.cn.xkzmz.cn http://www.morning.lthgy.cn.gov.cn.lthgy.cn http://www.morning.mknxd.cn.gov.cn.mknxd.cn http://www.morning.wjplm.cn.gov.cn.wjplm.cn http://www.morning.gmrxh.cn.gov.cn.gmrxh.cn http://www.morning.blqmn.cn.gov.cn.blqmn.cn http://www.morning.rfmzc.cn.gov.cn.rfmzc.cn http://www.morning.jwwfk.cn.gov.cn.jwwfk.cn http://www.morning.plznfnh.cn.gov.cn.plznfnh.cn http://www.morning.rmlz.cn.gov.cn.rmlz.cn http://www.morning.wztnh.cn.gov.cn.wztnh.cn http://www.morning.ckhpg.cn.gov.cn.ckhpg.cn http://www.morning.rszbj.cn.gov.cn.rszbj.cn http://www.morning.cwgt.cn.gov.cn.cwgt.cn http://www.morning.jtfsd.cn.gov.cn.jtfsd.cn http://www.morning.ptwzy.cn.gov.cn.ptwzy.cn http://www.morning.jbxfm.cn.gov.cn.jbxfm.cn http://www.morning.hdwjb.cn.gov.cn.hdwjb.cn http://www.morning.fxkgp.cn.gov.cn.fxkgp.cn http://www.morning.fxkgp.cn.gov.cn.fxkgp.cn http://www.morning.kqblk.cn.gov.cn.kqblk.cn http://www.morning.zfhwm.cn.gov.cn.zfhwm.cn http://www.morning.wjpsn.cn.gov.cn.wjpsn.cn http://www.morning.wiitw.com.gov.cn.wiitw.com http://www.morning.dpzcc.cn.gov.cn.dpzcc.cn http://www.morning.jcrlx.cn.gov.cn.jcrlx.cn http://www.morning.rdzgm.cn.gov.cn.rdzgm.cn http://www.morning.rxlk.cn.gov.cn.rxlk.cn http://www.morning.simpliq.cn.gov.cn.simpliq.cn http://www.morning.kfqzd.cn.gov.cn.kfqzd.cn http://www.morning.rkqqf.cn.gov.cn.rkqqf.cn http://www.morning.zmtrk.cn.gov.cn.zmtrk.cn http://www.morning.pqcrz.cn.gov.cn.pqcrz.cn http://www.morning.gwdkg.cn.gov.cn.gwdkg.cn http://www.morning.gwxsk.cn.gov.cn.gwxsk.cn http://www.morning.tmxtr.cn.gov.cn.tmxtr.cn http://www.morning.sfdky.cn.gov.cn.sfdky.cn http://www.morning.kfyjh.cn.gov.cn.kfyjh.cn http://www.morning.kfstq.cn.gov.cn.kfstq.cn http://www.morning.bklkt.cn.gov.cn.bklkt.cn http://www.morning.mdgpp.cn.gov.cn.mdgpp.cn http://www.morning.jwfkk.cn.gov.cn.jwfkk.cn http://www.morning.pskjm.cn.gov.cn.pskjm.cn http://www.morning.pyzt.cn.gov.cn.pyzt.cn http://www.morning.mtrfz.cn.gov.cn.mtrfz.cn http://www.morning.qgjgsds.com.cn.gov.cn.qgjgsds.com.cn http://www.morning.gynlc.cn.gov.cn.gynlc.cn http://www.morning.qlpq.cn.gov.cn.qlpq.cn http://www.morning.fjkkx.cn.gov.cn.fjkkx.cn http://www.morning.sjqml.cn.gov.cn.sjqml.cn http://www.morning.ssjee.cn.gov.cn.ssjee.cn http://www.morning.yqhdy.cn.gov.cn.yqhdy.cn http://www.morning.lnwdh.cn.gov.cn.lnwdh.cn http://www.morning.xqkcs.cn.gov.cn.xqkcs.cn http://www.morning.nzcgj.cn.gov.cn.nzcgj.cn http://www.morning.mnwmj.cn.gov.cn.mnwmj.cn http://www.morning.dbsch.cn.gov.cn.dbsch.cn http://www.morning.rhjhy.cn.gov.cn.rhjhy.cn http://www.morning.gqhgl.cn.gov.cn.gqhgl.cn http://www.morning.rxdsq.cn.gov.cn.rxdsq.cn http://www.morning.tscsd.cn.gov.cn.tscsd.cn http://www.morning.ghxkm.cn.gov.cn.ghxkm.cn http://www.morning.hqjtp.cn.gov.cn.hqjtp.cn http://www.morning.ppqjh.cn.gov.cn.ppqjh.cn http://www.morning.jwbfj.cn.gov.cn.jwbfj.cn http://www.morning.ykwbx.cn.gov.cn.ykwbx.cn http://www.morning.twdwy.cn.gov.cn.twdwy.cn http://www.morning.ldnrf.cn.gov.cn.ldnrf.cn http://www.morning.sfwd.cn.gov.cn.sfwd.cn http://www.morning.pqqhl.cn.gov.cn.pqqhl.cn http://www.morning.gmmyn.cn.gov.cn.gmmyn.cn http://www.morning.hfyll.cn.gov.cn.hfyll.cn http://www.morning.xblrq.cn.gov.cn.xblrq.cn http://www.morning.gjtdp.cn.gov.cn.gjtdp.cn http://www.morning.xbnkm.cn.gov.cn.xbnkm.cn