结果
{‘person1’: {‘name’: ‘xiaoming’, ‘age’: ’18’}, ‘person2’: {‘name’: ‘xiaohong’, ‘age’:
’20’}}
字典嵌套列表
- 需求
- {person: [“1”, “2”, “3”]}
- 示例
persion:
-
“1”
-
“2”
-
“3”
-
结果
- {‘person’: [‘1’, ‘2’, ‘3’]}
列表嵌套字典
- 需求
[{
“username1”: “test1”
}, {
“password1”: “111”,
“username2”: “test2”,
“password2”: “222”
}]
示例
-
username1: “test1”
-
password1: “111”
username2: “test2”
password2: “222”
结果
[{‘username1’: ‘test1’}, {‘password1’: 111, ‘username2’: ‘test2’, ‘password2’: 222}]
列表嵌套列表
- 需求
- [[“a”,”b”,”c”],[“1″,”2″,”3”]]
- 示例
–
-
“a”
-
“b”
-
“c”
–
-
“1”
-
“2”
-
“3”
结果
[[‘a’, ‘b’, ‘c’], [1, 2, 3]]
注意:
pycharm 会自动将 yaml 中的 tab 转化成空格。但是不能复制 “tab 键 ” ,不会进行转化。
同级别左侧必须要对齐,少个空格无所谓。
读取 Yaml 文件
读取单个 Yaml 文档
yaml 文档
#data.yaml
"用户名称": "test123"
"密码": "123456"
示例函数: demo.py 中文问题 (windows) :
encoding=’utf-8′ import yaml
import yaml
2)打开文件
with open("./data.yml","r",encoding='utf-8') as f:
3)使用yaml读取文件
data= yaml.safe_load(f)
3、输出这个文件
print(data)
读取实际上就是先使用 with open 的形式获取文件对象,再使用 yaml 中的 load 方法加载这个文件对象 , 通过
过 load 方法,一个字符串变成了一个字典
结果
{‘ 用户名称 ‘: ‘test123’, ‘ 密码 ‘: ‘123456’}
5.4.2 读取多个 Yaml 文档
多个文档在一个 yaml 文件,使用 — 分隔方式来分段
yaml 文档
” 用户名称 2″: “test456”
” 密码 “: “123456”
示例
import yaml
with open("./data.yaml",'r') as f:
data = yaml.safe_load_all(f)
for i in data:
print(i)
结果
{‘ 用户名称 1’: ‘test123’, ‘ 密码 ‘: ‘123456’}
{‘ 用户名称 2’: ‘test456’, ‘ 密码 ‘: ‘123456’}
yaml 封装
@File : YamlUtil.py
1、创建类
import os
import yaml
class YamlReader:
2、初始化,文件是否存在
def __init__(self,yamlf):
if os.path.exists(yamlf):
self.yamlf = yamlf
else:
raise FileNotFoundError("文件不存在")
self._data = None
self._data_all = None
3、yaml读取
#单个文档
def data(self):
#第一次调用data,读取yaml文档,如果不是,直接返回之前保存的数据
if not self._data:
with open(self.yamlf,'rb') as f:
self._data = yaml.safe_load(f)
return self._data
#多个文档
def data_all(self):
#第一次调用data,读取yaml文档,如果不是,直接返回之前保存的数据
if not self._data_all:
with open(self.yamlf,'rb') as f:
self._data_all = list(yaml.safe_load_all(f))
return self._data_all
配置文件设置
配置文件 conf.yaml
BASE:
#log等级
log_level: "debug"
#扩展名
log_extension: ".log"
test:
url: "http://211.103.136.242:8064"
case_file: "testdata.xlsx"
case_sheet: "美多商城接口测试"
基本目录配置
@File : Conf.py
import os
from utils.YamlUtil import YamlReader
1、获取项目基本信息
#获取当前项目的绝对路径
current = os.path.abspath(__file__)
BASE_DIR = os.path.dirname(os.path.dirname(current))
#定义config目录的路径
_config_path = BASE_DIR+os.sep+"config"
#定义conf.yml文件的路径
_config_file = _config_path+os.sep+"conf.yml"
def get_config_path():
return _config_path
def get_config_file():
return _config_file
#2、读取配置文件
#创建类
class ConfigYaml:
初始化yaml读取配置文件
def __init__(self):
self.config = YamlReader(get_config_file()).data()
定义方法获取需要信息
def get_conf_url(self):
return self.config['BASE']['test']['url']
if __name__ == '__main__':
conf_read = ConfigYaml()
print(conf_read.get_conf_url())
修改测试用例
-*- coding: utf-8 -*-
@Time : 2021/12/7 13:47
@Author : jeffky
@File : Test_Mail.py
1、导包
from pprint import pprint
from utils.RequestsUtil import requests_get, requests_post, Request
from config.Conf import ConfigYaml
#登录
'''
登录成功
http://211.103.136.242:8064
/authorizations/
POST
json
{
"username":"python",
"password":"12345678"
}
user_id': 1, 'username': 'python'
'''
import requests
2、定义登录方式
conf_y = ConfigYaml()
url_path = conf_y .get_conf_url()
def login():
3、定义测试数据
url = url_path+'/authorizations/'
data = {
"username":"python",
"password":"12345678"
}
4、发送POST请求
r = requests_post(url,json=data)
request = Request()
r = request.post(url, json=data)
5、输出结果
pprint(r)
return r['body']
'''
个人信息
获取个人信息正确
/user/
get
认证:
js
headers: {
'Authorization': 'JWT ' + this.token
},
'''
def info():
# 1、参数
url = url_path+'/user/'
token = login()['token']
headers={
'Authorization': 'JWT ' + token
}
# 2、get请求
# r = requests_get(url,headers = headers)
request =Request()
r = request.get(url,headers = headers)
# 3、输出
pprint(r)
'''
商品列表数据正确
请求方式: GET
/categories/(?P<category_id>\d+)/skus?page=xxx&page_size=xxx&ordering=xxx`
/categories/115/skus/
get json
{
"page":"1",
"page_size": "10",
"ordering": "create_time"
}
'''
def goods_list():
# 1、参数
url = url_path+'/categories/115/skus/'
data = {
"page":"1",
"page_size": "10",
"ordering": "create_time"
}
# 2、get请求
# r = requests_get(url,json= data)
request =Request()
r = request.get(url,json=data)
# 3、输出
pprint(r)
'''
添加购物车成功
/cart/
login_4 post
json
{"sku_id": "3","count": "1", "selected": "true"}
{"Authorization": "JWT ${token}$"}
'''
def cart():
# 1、参数
url = url_path+'/cart/'
token = login()['token']
headers={
'Authorization': 'JWT ' + token
}
data = {"sku_id": "3","count": "1", "selected": "true"}
# 2、get请求
# r = requests_post(url,headers = headers,json=data)
request =Request()
r = request.get(url,headers = headers,json=data)
# 3、输出
pprint(r)
'''
保存订单
/orders/
post
login_4 post
json
{ "address":"1","pay_method":"1" }
{"Authorization": "JWT ${token}$"}
'''
def order():
# 1、参数
url = url_path+'/orders/'
token = login()['token']
headers={
'Authorization': 'JWT ' + token
}
data = { "address":"1","pay_method":"1" }
# 2、get请求
# r = requests_post(url,headers = headers,json=data)
request =Request()
r = request.post(url,headers = headers,json=data)
# 3、输出
pprint(r)
if __name__ == '__main__':
login()
# info()
# goods_list()
# cart()
# order()</category_id>
Original: https://blog.csdn.net/python_jeff/article/details/121778175
Author: tester Jeffky
Title: Python3+Pytest 接口自动化测试全方案设计与开发-No.2接口自动化框架编写-3-配置文件
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/774758/
转载文章受原作者版权保护。转载请注明原作者出处!