怎么获得函数get_test_data()返回的是一个包含测试数据的列表。每个测试数据都是一个字典,字典的键是Excel表格中的列名,值是对应行的数据。
时间: 2024-02-23 10:03:34 浏览: 18
你可以在调用`get_test_data()`函数时传入Excel文件路径和工作表名称,函数会自动解析Excel文件,并将每行数据转换为一个字典,最后将所有字典组成的列表作为函数返回值返回。比如,你可以这样调用该函数:
```
file_path = 'test_data.xlsx'
sheet_name = 'Sheet1'
test_data = get_test_data(file_path, sheet_name)
```
这样就可以得到一个包含测试数据的列表`test_data`。你可以遍历这个列表,取出每个字典,再根据键名来获取对应的测试数据。
相关问题
写一个使用excel做数据驱动的接口测试脚本
好的,以下是一个使用Excel做数据驱动的接口测试脚本的示例:
1. 首先,导入所需的库和模块:
```
import xlrd
import requests
```
2. 然后,定义一个函数来读取Excel文件并获取测试数据:
```
def get_test_data(file_path, sheet_name):
# 打开Excel文件并获取工作簿对象
workbook = xlrd.open_workbook(file_path)
# 获取指定的工作表对象
sheet = workbook.sheet_by_name(sheet_name)
# 获取行数和列数
rows = sheet.nrows
cols = sheet.ncols
# 定义一个空列表,用于存储测试数据
test_data = []
# 遍历每一行数据
for i in range(1, rows):
# 定义一个字典,用于存储每一行数据
row_data = {}
# 遍历每一列数据
for j in range(cols):
# 获取单元格的值并添加到字典中
key = sheet.cell_value(0, j)
value = sheet.cell_value(i, j)
row_data[key] = value
# 将每一行数据添加到测试数据列表中
test_data.append(row_data)
# 返回测试数据列表
return test_data
```
3. 接下来,定义一个函数来发送HTTP请求并获取响应结果:
```
def send_request(url, method, headers, params=None, data=None, json=None):
# 发送HTTP请求并获取响应结果
response = requests.request(method=method, url=url, headers=headers, params=params, data=data, json=json)
# 获取响应状态码和响应内容
status_code = response.status_code
content = response.text
# 将响应状态码和响应内容添加到字典中
result = {'status_code': status_code, 'content': content}
# 返回响应结果字典
return result
```
4. 最后,定义一个函数来执行接口测试并输出测试结果:
```
def run_test(file_path, sheet_name):
# 获取测试数据
test_data = get_test_data(file_path, sheet_name)
# 遍历每一条测试数据
for data in test_data:
# 获取测试数据中的请求URL、请求方法、请求头、请求参数、请求体等参数
url = data['url']
method = data['method']
headers = eval(data['headers'])
params = eval(data['params']) if data['params'] else None
data_body = eval(data['data']) if data['data'] else None
json_body = eval(data['json']) if data['json'] else None
# 发送HTTP请求并获取响应结果
response = send_request(url=url, method=method, headers=headers, params=params, data=data_body, json=json_body)
# 获取期望结果和实际结果
expected_result = eval(data['expected_result'])
actual_result = response['content']
# 判断期望结果和实际结果是否一致
if expected_result == actual_result:
# 输出测试结果
print('测试通过')
else:
# 输出测试结果
print('测试失败')
```
使用以上代码,你可以编写一个数据驱动的接口测试脚本,从Excel文件中读取测试数据并执行接口测试,并输出测试结果。
怎么获得 ymltext = open_yml() file_path = ymltext['excel_path'] sheet_name = ymltext['open_excel_sheet_name'] "打开excel文件" def get_test_data(file_path, sheet_name): # 打开Excel文件并获取工作簿对象 workbook = xlrd.open_workbook(file_path) # 获取指定的工作表对象 sheet = workbook.sheet_by_name(sheet_name) # 获取行数和列数 rows = sheet.nrows cols = sheet.ncols # 定义一个空列表,用于存储测试数据 test_data = [] # 遍历每一行数据 for i in range(1, rows): # 定义一个字典,用于存储每一行数据 row_data = {} # 遍历每一列数据 for j in range(cols): # 获取单元格的值并添加到字典中 key = sheet.cell_value(0, j) value = sheet.cell_value(i, j) row_data[key] = value #将每一行数据添加到测试数据列表中 test_data.append(row_data) # 返回测试数据列表 return test_data里面返回的值并带到 def send_request(url, method, headers, params=None, data=None, json=None): # 发送HTTP请求并获取响应结果 response = requests.request(method=method, url=url, headers=headers, params=params, data=data, json=json) # 获取响应状态码和响应内容 status_code = response.status_code content = response.text # 将响应状态码和响应内容添加到字典中 result = {'status_code': status_code, 'content': content} # 返回响应结果字典 return result方法里面
在这个例子中,先使用`open_yml()`函数打开一个YAML文件,然后从中获取Excel文件路径和工作表名称,然后把这两个参数传给`get_test_data()`函数来读取Excel文件中的测试数据,最后把测试数据传给`send_request()`函数来发送HTTP请求并获取响应内容。
具体实现步骤如下:
1. 首先,使用`open_yml()`函数打开一个YAML文件,保存返回值到`ymltext`变量中。
```
ymltext = open_yml()
```
2. 从`ymltext`变量中获取Excel文件路径和工作表名称。
```
file_path = ymltext['excel_path']
sheet_name = ymltext['open_excel_sheet_name']
```
3. 调用`get_test_data()`函数读取Excel文件中的测试数据。
```
test_data = get_test_data(file_path, sheet_name)
```
4. 遍历`test_data`列表,依次发送HTTP请求并获取响应内容。
```
for data in test_data:
url = data['url']
method = data['method']
headers = data['headers']
params = data.get('params')
data = data.get('data')
json = data.get('json')
response = send_request(url, method, headers, params=params, data=data, json=json)
print(response)
```
注意,在发送HTTP请求时,需要根据测试数据中的请求方法、请求头、请求参数等信息来构造请求,并把请求参数传给`send_request()`函数。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)