数据分离时需要把数据保存在文件中,如excel,批量循环读取数据时需要使用到数据驱动模块如ddt、paramunitest
1,读取excel 表格中的数据方法封装
def excel_table_byindex(self,file,colnameindex):#根据索引获取excel表格中的数据,file:excel文件路径,colnameindex:表头列各所在行的 data=xlrd.open_workbook(file) table=data.sheets()[0] nrows=table.nrows#行数 colnames=table.row_values(colnameindex)#某一行数据 list=[] for colnameindex in range(1,nrows): row=table.row_values(colnameindex) if row: app={} for i in range(len(colnames)): app[colnames[i]]=str(row[i]) list.append(app) return list
2,引用数据驱动模块循环读取数据
import ddt
input_value = Op.excel_table_byindex(file='D:\My\Interface-修改\data\testdata_block.xls', colnameindex=0)#测试用 @ddt.ddt class TestCertificate(unittest.TestCase): @ddt.data(*input_value) def test_blockchain(self,data): self.case_name=data['case_name'] self.seller_name = data['seller_name'] self.seller_address = data['seller_address'] # self.app_id=app_id self.seller_phone=data['seller_phone'] self.seller_bank_name = data['seller_bank_name'] self.seller_bank_account=data['seller_bank_account'] self.buyer_tax_code=data['buyer_tax_code'] self.buyer_title=data['buyer_title'] self.buyer_address=data['buyer_address'] self.buyer_phone= data['buyer_phone'] self.buyer_email= data['buyer_email'] self.buyer_bank_name=data['buyer_bank_name'] self.buyer_bank_account = data['buyer_bank_account'] self.remark = data['remark'] self.cashier = data['cashier'] self.checker = data['checker'] self.invoicer = data['invoicer'] self.call_back_url = data['call_back_url'] self.ep_app_id = data['ep_app_id'] self.expect=data['expect']
或者使用paramunittest
import paramunittest input_value = Te.excel_table_byindex(file='D:\My\Partner\P_testdata\service_register_testdata.xls', colnameindex=0) R=Register() @paramunittest.parametrized(*input_value) class TestLogin(unittest.TestCase): def setParameters(self, case_name,email,password,repeat,No): '''这里注意了,uname, passwd, texts三个参数和前面定义的字典一一对应''' self.case_name=case_name self.email = email self.password = password self.repeat=repeat self.No=No def test_register(self): self._testMethodDoc=self.case_name#设置用例名称 self.url="http://partner.yewifi.com" # 判断数据库邮箱是否已被注册过 r_id = Te.connect_db(database='privilege', sql="select id from qx_member where email='%s'" % self.email, charset='utf8') # 检查数据库是否存在该邮箱号 print(r_id) if r_id: # 判断元组非空 print(r_id[0][0]) Te.connect_db(database='privilege', sql="delete from qx_member where email='%s'" % self.email,charset='utf8') # 删除账户 print("priviledge删除%s账户成功!" % self.email) Te.connect_db(database='fpmerchant', sql="delete from m_service_provider where user_id='%s'" % r_id[0][0],charset='utf8') # 删除账户 print("fpmerchant删除数据成功!") Te.connect_db(database='providers_db', sql="delete from gpi_provider where user_id='%s'" % r_id[0][0],charset='utf8') # 删除账户 print("providers_db删除数据成功!") else: print("账号:%s未被注册过" % self.email) R.register(self.url, self.email, self.password, self.repeat,browserType="FF") time.sleep(10) print(R.driver.find_element_by_xpath("//input[@name='username']").text) try: assert R.driver.find_element_by_xpath("//input[@id='username']").text == '%s' % self.email # 注册后跳转到登录页,账号复写断言 print("第%s条注册成功 !" % self.No) except: R.screenshot_image(pic_path='D://My//P_result//partner_screenshot//', pic_name='第%s条注册服务商' % str(self.No)) print("第%s条注册失败 !" % self.No) print('第%s条测试结束' % self.No) time.sleep(3) R.close_Browser()