foreword
Whether it is Ui automation and interface automation, code and data need to be separated, and Text, Excel, and Yaml will be used. Today I will talk about how to read file data
Remember one thing: the test data cannot be written in the code, this is a matter of principle
structure directory
Package directory address
#! /usr/bin/python3
# @Author :
from pathlib import Path
class ReadPath():
"""基本路径"""
BasePath = Path(__file__).resolve().parents[1]
# print(BasePath)
def ini_path(self):
"""获取ini文件目录"""
ini_path = ReadPath.BasePath/"data"/"test.ini"
# print(ini_path)
return ini_path
def excel_path(self):
"""获取excel文件目录"""
excel_path = ReadPath.BasePath/"data"/"test.xlsx"
# print(excel_path)
return excel_path
def yaml_path(self):
"""获取yaml目录"""
yaml_path = ReadPath.BasePath/"data"/"test.yaml"
# print(yaml_path)
return yaml_path
readText
need:
problem analysis
1. Open the txt file
2. Read each row of data
3. Store the files in the list (convenient for loop reading)
#!/usr/bin/python3
with open("E:/test.txt", "r") as f:
#readlines读取文件内每一行数据
for line in f.readlines():
line = line.strip('\n') #去掉列表中每一个元素的换行符
# split():拆分字符串。通过指定分隔符对字符串进行切片,并返回分割后的字符串列表(list)
data = (line.split(","))
print(data)
operation result:
read Excel
Everyone knows that test cases are written in Excel. If there are a small number of test cases, it is easy to handle, but what if there are hundreds or thousands of test cases?
For automated testing , it is necessary to read the use case data, which must be read in a loop to achieve automation. So the question is, how to do it?
Use the following figure for example:
problem analysis
1. Use lists to store these use case data, so use lists
2. Each row of use cases should be stored in a dictionary, so a dictionary is needed
3. Circularly write to the dictionary, and then store it in the list
#! /usr/bin/python3
# @Author
import xlrd # pip install xlrd==1.2.0 最新版本不支持xlsx
from base.base_path import ReadPath
class ExcelData:
"""读取excl表接口数据"""
def __init__(self):
# 打开文件
self.excel = xlrd.open_workbook(ReadPath().excel_path())
# 通过下标定位表格
self.sheet = self.excel.sheet_by_index(0)
# 行: 4 和列数: 5
self.rows, self.cols = self.sheet.nrows, self.sheet.ncols
print(self.rows, self.cols)
def read_execl(self):
# 获取第一行数据key
first_row = self.sheet.row_values(0)
# print(first_row) # [编号,method,host, params, result]
# 定义空列表,用于存放用例数据
self.result = []
# 从第一行用例开始循环(1, 4) 循环三次1,2,3
for i in range(1, self.rows):
# 定义空字典
info_dict = {}
# 每1次大循环要循环5次(字典里有5组数据)
for j in range(0, self.cols):
# j=0,1,2,3,4
# 添加到字典 (1)[0]---第2行第1例的值,依次循环
info_dict[first_row[j]] = self.sheet.row_values(i)[j]
self.result.append(info_dict)
print(self.result)
return self.result
if __name__ == "__main__":
ex = ExcelData()
ex.read_execl()
operation result:
Read Yaml
The suffix of the yaml file is yaml, such as file name.yaml
send_mail:[email protected]
send_code:ibzwpvisjyhrbhcb
get_mail:[email protected]
yaml is a third-party module, and pip install pyyaml needs to be installed separately
problem analysis
1. Define the file address
2. Open the yaml file
3. After reading the file, convert it into a dictionary for easy reading
#! /usr/bin/python3
# @Author
from base.base_path import ReadPath
import yaml
class ReadYaml():
def __init__(self):
# 获取yaml路径
self.yaml_path = ReadPath().yaml_path()
def read_yaml(self):
# open方法打开直接读出来
file = open(self.yaml_path, 'r', encoding='utf-8')
result = file.read()
dict = yaml.load(result, Loader=yaml.FullLoader) # 用load方法转字典
print(dict)
return
read = ReadYaml()
read.yaml_path()
operation result:
Finally, I would like to thank everyone who has read my article carefully. Reciprocity is always necessary. Although it is not a very valuable thing, you can take it away if you need it:
These materials should be the most comprehensive and complete preparation warehouse for [software testing] friends. This warehouse has also accompanied tens of thousands of test engineers through the most difficult journey, and I hope it can help you! Partners can click the small card below to receive