pip install xlrd |
import xlrd # 将excel进行实例化 book = xlrd.open_workbook('E:\\web\\123.xlsx') # 通过下标方法读取sheet值 sheet = book.sheet_by_index(0) # 通过name值进行读取sheet # sheet = book.sheet_by_name('Sheet1') # 获取列表的总数 nrows = sheet.nrows # 循环读取每行数据 for i in range(1, nrows): # 通过每行进行读取数据 # print(sheet.row_values(i)) # 讲数据通过组合成dic+t格式 data = dict(zip(sheet.row_values(0), sheet.row_values(i))) print(data) |
def Requests_result(item): # 封装request请求,并读取数据 response = requests.request( method=item['method'], url=item['url'], data=json.loads(item['body']) ) result = response.json()['reason'] # 这里返回实际结果是否和预期值一样 return result == item['expect'] |
import xlrd import requests import json import pytest def Read_Excel(): # 将excel进行实例化 book = xlrd.open_workbook('E:\\web\\123.xlsx') # 通过下标方法读取sheet值 sheet = book.sheet_by_index(0) # 循环读取每行数据 return [dict(zip(sheet.row_values(0), sheet.row_values(row))) for row in range(1, sheet.nrows)] class Test_01: def Requests_result(self, item): # 封装请求 response = requests.request( method=item['method'], url=item['url'], data=json.loads(item['body']) ) result = response.json()['reason'] print(result) return result == item['expect'] # 通过参数化的方式进行导入excel的数据 @pytest.mark.parametrize('item', Read_Excel()) def test_01(self, item): response_result = self.Requests_result(item) # 断言请求返回的结果是否为True assert response_result == True if __name__ == '__main__': pytest.main(['-vs']) |
import xlrd import requests import json import unittest import ddt def Read_Excel(): # 将excel进行实例化 book = xlrd.open_workbook('E:\\web\\123.xlsx') # 通过下标方法读取sheet值 sheet = book.sheet_by_index(0) # 循环读取每行数据 return [dict(zip(sheet.row_values(0), sheet.row_values(row))) for row in range(1, sheet.nrows)] @ddt.ddt class Test_01(unittest.TestCase): def Requests_result(self, item): response = requests.request( method=item['method'], url=item['url'], data=json.loads(item['body']) ) result = response.json()['reason'] return result # 通过ddt进行读取数据 @ddt.data(*Read_Excel()) def test_01(self, data): response_result = self.Requests_result(data) self.assertTrue(response_result) if __name__ == '__main__': unittest.main() |
欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) | Powered by Discuz! X3.2 |