yaml数据:
introduce:
-
name: 'A'
age: '20'
job: '测试工程师'
-
name: 'B'
age: '20'
job: '开发工程师'
interface:
-
url: 'www.baidu.com'
headers : {'Content-Type': 'application/json'}
body: {'quest_ids': xxx}
- url: 'https://www.csdn.net/'
headers: { 'Content-Type': 'application/json' }
body: { 'quest_ids': yyy }
脚本:
import yaml
import pytest
import unittest
import ddt
with open('./ex.yaml', encoding='utf-8') as allData:
data = yaml.load(allData.read(), Loader=yaml.FullLoader)
introduce = data.get('introduce')
interface = data.get('interface')
@pytest.mark.parametrize('data', introduce)
class TestCase1(object):
def test_01(self, data):
print(f'\nname:{data.get("name")}\n'
f'age:{data.get("age")}\n'
f'job:{data.get("job")}')
@ddt.ddt()
class TestCase2(unittest.TestCase):
@ddt.data(*interface)
@ddt.unpack
def test_task_template(self, url, headers, body):
print(f'\nurl:{url}\n'
f'headers:{headers}\n'
f'body:{body}')
E:\ex\venv\Scripts\python.exe "E:\pycharm\PyCharm Community Edition 2020.3.5\plugins\python-ce\helpers\pycharm\_jb_pytest_runner.py" --path E:/ex/test_ex.py
Testing started at 14:00 ...
Launching pytest with arguments E:/ex/test_ex.py in E:\ex
============================= test session starts =============================
platform win32 -- Python 3.8.2, pytest-6.2.4, py-1.10.0, pluggy-0.13.1 -- E:\ex\venv\Scripts\python.exe
cachedir: .pytest_cache
rootdir: E:\ex
collecting ... collected 4 items
test_ex.py::TestCase1::test_01[data0] PASSED [ 25%]
name:A
age:20
job:测试工程师
test_ex.py::TestCase1::test_01[data1] PASSED [ 50%]
name:B
age:20
job:开发工程师
test_ex.py::TestCase2::test_task_template_1 PASSED [ 75%]
url:www.baidu.com
headers:{'Content-Type': 'application/json'}
body:{'quest_ids': 'xxx'}
test_ex.py::TestCase2::test_task_template_2 PASSED [100%]
url:https://www.csdn.net/
headers:{'Content-Type': 'application/json'}
body:{'quest_ids': 'yyy'}
============================== 4 passed in 0.03s ==============================
Process finished with exit code 0
|