python接口自动化学习笔记(封装获取测试数据方法)
▼
关注测试君 | 会上瘾
本篇文章是接于python接口自动化学习笔记(封装方法用于读取excel) 后的拓展,讲解在封装完成excel的数据读取代码后,如何在data层进行使用
首先,我准备了这样一个excel表格用以存储测试数据
1、新建data_config文件
声明所有变量
#coding:utf-8
class
global_var
:
#case_id
id
=
0
url
=
1
request_method
=
2
data
=
3
expected
=
4
#获取case_id
def
get_id
():
return
global_var
.
id
#获取url
def
get_url
():
return
global_var
.
url
#获取请求方式
def
get_request_method
():
return
global_var
.
request_method
#获取请求数据
def
get_data
():
return
global_var
.
data
#获取预期结果
def
get_expected
():
return
global_var
.
expected
2、新建get_data文件用以读取测试数据
封装读取测试数据的代码 封装的目的是为了在调用相应的方法时,可以获取对应的excel中对应字段的值,例如调用get_data(),可以获取到“请求参数”,封装代码如下:
#coding:utf-8
from
data
import
data_config
from
data
.
operation_excel
import
OpeExcel
class
GetData
:
def
__init__
(
self
):
self
.
operation_excel
=
OpeExcel
()
# 获取excel行数(case个数)
def
get_case_lines
(
self
):
return
self
.
operation_excel
.
get_lines
()
# 获取url
def
get_url
(
self
,
row
):
col
=
data_config
.
get_url
()
url
=
self
.
operation_excel
.
get_value
(
row
,
col
)
return
url
# 获取请求方式
def
get_request_method
(
self
,
row
):
col
=
data_config
.
get_request_method
()
request_method
=
self
.
operation_excel
.
get_value
(
row
,
col
)
return
request_method
# 获取请求参数
def
get_data
(
self
,
row
):
col
=
data_config
.
get_data
()
data
=
self
.
operation_excel
.
get_value
(
row
,
col
)
return
data
# 获取请求结果
def
get_expected
(
self
,
row
):
col
=
data_config
.
get_expected
()
result
=
self
.
operation_excel
.
get_value
(
row
,
co
)
return
result
封装完成,可以执行以下代码查看结果,会打印出“请求参数”中第3行的值
if
__name__
==
'__main__'
:
data
=
GetData
()
(
data
.
get_data
(
2
))
打印结果:
有疑问的小伙伴可以在下方留言,我会针对问题不断优化文章内容,希望能有效的帮到更多人,谢谢~
一篇文章从了解到入门shell
postman请求数据库方法(Omysql)
软件测试工程师在上海的求职经历
还有一件重要的事情要和大家说,我不是经常看公众号后台,所以有时候大家在后台发留言,时间一旦超过2天,我就没有回复权限了。所以,我要公布自己的微信号了,欢迎大家来埋伏我