yaml是一种数据格格式,后缀是 .yml或 .yaml
用于配置文件以及编写测试用例
区分大小写
通过缩进表示层级,缩进使用空格非Tab
#注释
字符串一般不用双引号,默认就是字符串
1.标量:最基本的数据类型
Null 对应 Python中的None
2.对象:键(空格)值对,类似dict
3.数组:用过一组同级别的-开头,类似数组
name : - name1 : 霸王 - name2 : '霸\n王' - name3 : "霸\n王" age : 25 height : 156.3 date : 2023-3-20 20:20:20 shenfen : null
读取文件中的所有数据类型
{'name': [{'name1': '霸王'}, {'name2': '霸\\n王'}, {'name3': '霸\n王'}], 'age': 25, 'height': 156.3, 'date': datetime.datetime(2023, 3, 20, 20, 20, 20), 'shenfen': None}
使用!!+要转的数据类型
age: !!str 18 6
height: !!float "17.5"
&表示锚点,*引用锚点,<<合并到当前数据
name : &name - name1 : 霸王 - name2 : '霸\n王' - name3 : "霸\n王" age : 25 height : 156.3 date : 2023-3-20 20:20:20 shenfen : null newname : *name newnages: name100: name100 <<: *name
{'name': [{'name1': '霸王'}, {'name2': '霸\\n王'}, {'name3': '霸\n王'}],
'age': 25,
'height': 156.3,
'date': datetime.datetime(2023, 3, 20, 20, 20, 20),
'shenfen': None,
'newname': [{'name1': '霸王'}, {'name2': '霸\\n王'}, {'name3': '霸\n王'}],
'newnages': {'name3': '霸\n王', 'name2': '霸\\n王', 'name1': '霸王', 'name100': 'name100'}}
python安装包
pip install pyyaml
def read_yaml(): with open("./test_case/login.yaml",encoding="utf-8") as f: value = yaml.safe_load(f) print(value)
def write_yaml(): with open("./test_case/login.yaml",encoding="utf-8",mode="a+") as f: date = {"sdad":"sdsadsa"} yaml.safe_dump(date,f)
def clean_yaml(): with open("./test_case/login.yaml", encoding="utf-8", mode="w") as f: f.truncate()
数据驱动:
@pytest.mark.parametrize(args_name,args_value)
args_name:参数名
args_value:参数值(只能是列表list或元祖tuple)
import pytest import allure @allure.epic("项目名称2") @allure.feature("模块名称2") class TestBpi: @allure.story("打印参数") @pytest.mark.parametrize("name", ["sidyoad", "sdysfysd", "fjdisifdfuo"]) def test_name(self,name): print(name)
如果allure报告样式不显示过长参数,
修改:\venv\Lib\site-packages\allure_pytest\listener.py 中为 test_result.parameters.extend([])
- feature: 模块名称1 story: 接口名称1 title: 用例标题1 request: method: get url: /cgi-bin/token params: "grant_type": "client_credential" "appid": "xx" "secret": "xx" headers: null validate: null - feature: 模块名称2 story: 接口名称2 title: 用例标题2 request: method: get url: /cgi-bin/token params: "grant_type": "client_credential" "appid": "xx" "secret": "xxx" headers: null validate: null
import pytest import allure from commons.yaml_utils import read_yaml @allure.epic("项目名称2") @allure.feature("模块名称2") class TestApi: @allure.story("打印参数") @pytest.mark.parametrize("caseinfo", read_yaml("./test_case/a/login.yaml")) def test_name(self,caseinfo,base_url): allure.dynamic.title(caseinfo["title"]) print("模块名称:%s"%caseinfo["feature"]) print("接口名称:%s"%caseinfo["story"]) print("用例标题:%s"%caseinfo["title"]) print("请求方式:%s"%caseinfo["request"]["method"]) print("请求路径:%s"%base_url+caseinfo["request"]["url"]) print("请求参数:%s"%caseinfo["request"]["params"]) print("请求头:%s"%caseinfo["request"]["headers"]) print("断言:%s"%caseinfo["validate"])
# 操作工具包 import yaml def read_yaml(yaml_path): with open(yaml_path,encoding="utf-8") as f: value = yaml.safe_load(f) return value def write_yaml(): with open("./test_case/login.yaml",encoding="utf-8",mode="a+") as f: date = {"sdad":"sdsadsa"} yaml.safe_dump(date,f) def clean_yaml(): with open("./test_case/login.yaml", encoding="utf-8", mode="w") as f: f.truncate()