You learn python automated test it? You heard requests database? tablib is that one can operate a variety of file formats such as Excel, turning them into a common data set library of third-party libraries requests perennial maintenance.
tablib support of major data formats are:
- xls, the old version of office excel file format;
- xlsx series, the new office file format;
- json
- yaml
- html
- csv
- df, pandas of DataFrame, you need to install pandas
That is, different data formats can be converted into a universal tablib relational data format, and then seamlessly switch between the various formats. What is a relational data format it? such as:
- MySQL data, each row of data corresponding to a field name. You can use this library to query the database data easily into Excel.
- Excel data, each row has a header.
- json, and key value data into
- It is similar to the above.
tablib This feature common data format to solve some of the problems some of the previous Excel Library Operation.
Excel operation of several popular libraries
- xlrd
- openpyxl
These libraries are very good library, there are restrictive:
1, xlrd problems. He can only read, can not write. You have to write another installed base.
2, the problem openpyxl. Xlsx series only supports reading and writing. Because most are now using this Excel format, so the problem can actually be ignored, but if your office software is relatively old, only with xls format, the library will not take up.
tablib installation
1, the installation pip install tablib
2, introduced import tablib
Key concept
tablib.Dataset() # 相当于 Excel 中的 sheet
tablib.Databook() # 相当于 Excel 中的 workbook 工作簿
Dataset Getting Started
Look at a small example:
url | method | expected |
---|---|---|
lemon.ke.qq.com | get | success |
lemonban.com | post | success |
url, method, expected is a table header header table, in fact, the name of each column in the database that we call the field name. The following line is the data.
Excel want to create a very simple, only you need to prepare the data you need data, header header table name and title:
import tablib
# 表各列的标题 header
headers = ['url', 'method', 'expected']
# 需要存到 excel 的数据
data_list = [
['https://lemon.ke.qq.com', 'get', '成功'],
['https://lemonban.com', 'post', '成功']
]
# 生成数据
data = tablib.Dataset(*data_list, headers=headers,title='测试用例')
print(data)
Print is this:
Save as excel
To save as excel document, you need only operate as an ordinary file as read and write on it, or write data.xls data.xlsx property inside the data:
with open('demo.xls', 'wb') as f:
f.write(a.xls)
with open('demo.xlsx', 'wb') as f:
f.write(a.xlsx)
Note: The mode requires wb binary mode
After the results are saved like this:
Databook
Databook is similar to the concept of the workbook, it is actually required parameters Databook the above DataSet. If an Excel only one table, with the DataSet is enough, if there are multiple tables:
book = tablib.Databook([data, data2])
with open('demo_book.xls', 'wb') as f:
f.write(book.xls)
Spreadsheet Import: import_set
Then write to Excel, followed by a read operation, read the excel file is a regular file read and write and similar calls import_set method:
with open('demo.xls', 'rb') as f:
# 接受 2 个参数,读出来的数据和读取的文件格式
data = tablib.import_set(f.read(), 'xls')
print(data)
# 或者采用第二种方式
with open('demo.xls', 'rb') as f:
data = tablib.DataSet().load(f.read(), 'xls')
print(data)
Acquiring data for automated testing
With an example of actual combat, get excel file data, perform automated test procedures:
def api_tester(url, method, expected):
print("正在测试{},请求方法{}, 预期结果{}".format(
url, method, expected))
with open('demo.xls', 'rb') as f:
# 接受 2 个参数,读出来的数据和读取的文件格式
data = tablib.import_set(f.read(), 'xls')
print(data)
for i in data:
api_tester(*i)
Import book
DataSet and operation of the same:
with open('demo.xls', 'rb') as f:
# 接受 2 个参数,读出来的数据和读取的文件格式
data = tablib.import_book(f.read(), 'xls')
print(data)
# 或者采用第二种方式
with open('demo.xls', 'rb') as f:
data = tablib.DataBook().load(f.read(), 'xls')
print(data)
to sum up
This we are familiar with the core concepts of tablib:
- DataBook
- DataSet
- Reading, import_set, import_book
- Write, write
We will next ranks and data manipulation, as well as some of its features.