pip install pyyaml
YAML、YML在线编辑器(格式化校验)-BeJSON.com
# data.yaml
hero:
name: angel
word: 火焰是我最喜欢的玩具
hp: 445.5
hero2: { name: angel, word: 火焰是我最喜欢的玩具,hp: 445.5 }
heros_name:
- Angel
- Bill
- Jordan
heros:
- name: Gates
word: microsoft
hp: 444.4
heros_name_list:
- Angel
- Bill
- Gates
test:
name: login_case1
request:
url: http://x.x.x.x/api/v1/login
method: POST
headers:
content-Type: application/json
json1:
username: test
password: 123456
# read_data.py
import yaml
f = open("../config/data.yaml", encoding="utf8")
data = yaml.safe_load(f)
# print(data)
# print(data["hero"])
# print(data["hero2"])
# print(data["heros_name"])
# print(data["heros"])
print(data["test"]["name"])
print(data["test"]["request"]["url"])
print(data["test"]["request"]["json1"]["username"])
print(data["test"]["request"]["json1"]["password"])
/////////////////////////////
# login_case1
# http://x.x.x.x/api/v1/login
# test
# 123456
文件目录

# data.yaml
heroes_name:
- Angel
- Bill
- Jordan
# test_param_yaml.py
import pytest
from utils.read_data import get_data
@pytest.mark.parametrize("name", get_data['heroes_name'])
def test_parametrize01(name):
print(name)
if __name__=='__main__':
pytest.main()
# pytest.ini
[pytest]
testpaths=./test_param
addopts:-v
# conftest.py
import pytest
@pytest.fixture(scope="function", autouse=True)
def func():
print("我是前置步骤")
yield
print("我是后置步骤")
# 执行结果
cachedir: .pytest_cache
rootdir: C:\Users\xxx\PycharmProjects\pythonProject
configfile: pytest.ini
collecting ... collected 3 items
test_param_yaml.py::test_parametrize01[Angel] PASSED [ 33%]
test_param_yaml.py::test_parametrize01[Bill] PASSED [ 66%]
test_param_yaml.py::test_parametrize01[Jordan] PASSED [100%]
============================== 3 passed in 0.02s ==============================
# 修改pytest.ini
[pytest]
testpaths=./test_param
addopts:-s
# result
configfile: pytest.ini
collected 3 items
test_param_yaml.py 我是前置步骤
Angel
.我是后置步骤
我是前置步骤
Bill
.我是后置步骤
我是前置步骤
Jordan
.我是后置步骤
============================== 3 passed in 0.01s ==============================
读取多参数
# data.yaml
hero_parm:
- - Angel
- Test_Angel
- - Bill
- Test_Bill
- - Jordan
- Test_Jordan
# test_param_yaml.py
import pytest
from utils.read_data import get_data
@pytest.mark.parametrize("name,parm", get_data['hero_parm'])
def test_parametrize01(name,parm):
print(f'{name}的属性是{parm}')
if __name__=='__main__':
pytest.main()
# result
configfile: pytest.ini
collecting ... collected 3 items
test_param_yaml.py::test_parametrize01[Angel-Test_Angel] PASSED [ 33%]
test_param_yaml.py::test_parametrize01[Bill-Test_Bill] PASSED [ 66%]
test_param_yaml.py::test_parametrize01[Jordan-Test_Jordan] PASSED [100%]
============================== 3 passed in 0.02s ==============================
interface
# data.yaml
mobile: {shouji: 13456759012,appkey: 0c818521d38759e1}
# test_mobile.py
import requests
import pytest
from utils.read_data import get_data
def test_mobile():
param = get_data['mobile']
r = requests.get(url="http://api.binstd.com/shouji/query",
params={"shouji": param['shouji'], "appkey": param['appkey']})
print(r.status_code)
print(r.json())
if __name__=='__main__':
pytest.main()
# pytest.ini
[pytest]
addopts:-s
test_mobile.py 我是前置步骤
200
{'status': 0, 'msg': 'ok', 'result': {'shouji': '13456759012', 'province': '浙江', 'city': '杭州', 'company': '中国移动', 'cardtype': None, 'areacode': '0571'}}
.我是后置步骤
多参数
# data.yaml
mobile:
- [ 13456759012, 0c818521d38759e1 ]
- [ 13556759012, 0c818521d38759e1 ]
- [ 13656759012, 0c818521d38759e1 ]
- [ 13756759012, 0c818521d38759e1 ]
# pytest.ini
[pytest]
addopts:-v
# test_mobile.py
import requests
import pytest
from utils.read_data import get_data
@pytest.mark.parametrize("shouji,apikey", get_data['mobile'])
def test_mobile(shouji,apikey):
param = get_data['mobile']
r = requests.get(url="http://api.binstd.com/shouji/query",
params={"shouji": shouji, "appkey": apikey})
print(r.status_code)
print(r.json())
if __name__=='__main__':
pytest.main()
# result
test_mobile.py::test_mobile[13456759012-0c818521d38759e1] PASSED [ 25%]
test_mobile.py::test_mobile[13556759012-0c818521d38759e1] PASSED [ 50%]
test_mobile.py::test_mobile[13656759012-0c818521d38759e1] PASSED [ 75%]
test_mobile.py::test_mobile[13756759012-0c818521d38759e1] PASSED [100%]
网友评论