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()