yaml文件存放测试用例

yaml文件存放测试用例#

一、YAML简介#

YAML 是一种可读性非常高,与程序语言数据结构非常接近。同时具备丰富的表达能力和可扩展性,并且易于使用的数据标记语言。

YAML是 "YAML Ain't a Markup Language"(YAML不是一种标记语言)的递归缩写。

这么命名的方式,表示 YAML 强调的不是标记,而是数据本身。

二、基本语法#

  • 1.大小写敏感
  • 2.使用缩进表示层级关系
  • 3.不允许使用 TAB 键来缩进,只允许使用空格键来缩进
  • 4.缩进的空格数量不重要
  • 5.使用"#"来表示注释

三、支持的数据格式#

  • 1.对象:键值对的集合,又称映射 (mapping) / 哈希(hashes)/ 字典 (dictionary)
  • 2.数组: 一组按次序排列的值,又称序列 (sequence) / 列表 (list)
  • 3.纯量 (scalars) :单个的,不可再分的值,
    • 常见的纯量: 字符串、布尔值、整数、浮点数、null、时间、日期

四、数据格式简单示例#

1、对象数据格式#

yaml格式:
Copy
name: xiaoming
读取出来的格式:
Copy
{'name':'xiaoming'}

2、数组数据格式(在前面添加 ‘-’ 读出来就是数组格式)#

yaml格式:
Copy
- list1 - list2 - list3
读取出来的格式:
Copy
['list1', 'list2', 'list3']

3、纯量数据格式#

yaml格式:
Copy
number: 18.5 string: hello bool: true nothing: ~ date: 2020-04-21 time: 2020-04-21 13:14:21
读取出来的格式:
Copy
{ 'number': 18.5, 'string': 'hello', 'bool': True, 'nothing': None, 'date': datetime.date(2020, 4, 21), 'time': datetime.datetime(2020, 4, 21, 13, 14, 21) }

五、YAML使用#

1、安装yaml库#

Copy
pip install pyyaml

2、导入yaml库#

Copy
import yaml

3、读取数据#

Copy
with open(file="conf.yaml", encoding='utf8') as f: data = yaml.load(f, yaml.FullLoader)

【注】:YAMLLoadWarning: calling yaml.load() without Loader=... is deprecated, as the default Loader is unsafe. Please read https://msg.pyyaml.org/load for full details.
data = yaml.load(f)

为了去除这个警告,我们可以在yaml.load( )加一个参数 yaml.FullLoader 即可

4、写入数据(mode参数:w,覆盖写入, a,追加写入)#

Copy
new_data = {'name': 'Tom'} with open(file="conf.yaml", mode='w', encoding="utf-8") as f: yaml.dump(new_data, f)

六、YAML文件存放测试用例数据#

yaml测试用例示例#

Copy
register: - case_id: 1 title: 注册用例1 method: POST url: /register data: account: '123456789' pwd: '12345678' expect: code: 0 msg: OK - case_id: 2 title: 注册用例2 method: POST url: /register data: account: '123456788' pwd: '12345678' type: 1 expect: code: 0 msg: OK login: - case_id: 1 title: 登陆用例1 method: POST url: /login data: account: '123456789' pwd: '12345678' expect: code: 0 msg: OK - case_id: 2 title: 登陆用例2 method: POST url: /login data: account: '123456788' pwd: '12345678' expect: code: 0 msg: OK

读取出的数据(清晰明了)#

Copy
{ 'register': [ {'case_id': 1, 'title': '注册用例1', 'method': 'POST', 'url': '/register', 'data': {'account': '123456789', 'pwd': '12345678'}, 'expect': {'code': 0, 'msg': 'OK'}}, {'case_id': 2, 'title': '注册用例2', 'method': 'POST', 'url': '/register', 'data': {'account': '123456788', 'pwd': '12345678', 'type': 1}, 'expect': {'code': 0, 'msg': 'OK'}} ], 'login': [ {'case_id': 1, 'title': '登陆用例1', 'method': 'POST', 'url': '/login', 'data': {'account': '123456789', 'pwd': '12345678'}, 'expect': {'code': 0, 'msg': 'OK'}}, {'case_id': 2, 'title': '登陆用例2', 'method': 'POST', 'url': '/login', 'data': {'account': '123456788', 'pwd': '12345678'}, 'expect': {'code': 0, 'msg': 'OK'}} ] }

七、对读取YAML文件进行封装#

Copy
class YamlHandle(object): def __init__(self, conf_file): self.conf_file = conf_file def load(self) -> dict: """ 读取yaml文件,获取全部数据 :return: dict """ with open(file=self.conf_file, encoding='utf8') as f: data = yaml.load(f, yaml.FullLoader) return data def get_data(self, node) -> list: """ 获取节点数据 :param node: 节点名称 :return: dict&str """ return self.load()[node]

yaml 文件通过 pyyaml 库转换后就是 Python 中对应的数据类型。直接进行操作即可,相较从 excel 中读取数据全部是字符串的方式来说,处理更方便。相对于 json 格式,可读性更高。#

posted @   DesireYang  阅读(1441)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
CONTENTS