import json import pprint import pytest from Slience.utils.login_util import Login from Slience.utils.request_util import SendRequest from Slience.utils.yaml_util import read_yaml @pytest.fixture(scope='class',autouse=False) def my_fixture(): print("执行前置步骤") Login().login_system() yield print("执行后置步骤") logout() def logout(): SendRequest.s.close() pass class TestOrders(): """列出订单""" list = read_yaml('./orders.yaml') print(list) @pytest.mark.parametrize("keyword",list) def test_orders(self,my_fixture,keyword): url = "http://127.0.0.1/api/mgr/orders" method = "post" data = { "action": "list_order", "pagesize": 10, "pagenum": 1, "keywords": keyword } res = SendRequest().send_request(method, url, json=data) assert res.status_code == 200 print(res.json()) length = len(res.json()["retlist"]) if length >= 1: for i in range(length): print(f"keyword:{keyword}") print(f"订单名称:{res.json()['retlist'][i]['name']}") assert keyword in res.json()['retlist'][i]["name"] return res
结果:
============================= test session starts ============================= collecting ... ['南京', '北京', '郑州', '西安'] collected 4 items test_parametrize_02.py::TestOrders::test_orders[南京] [2023-09-05 13:05:12,945][INFO][request_util.py][34] [发送请求成功] test_parametrize_02.py::TestOrders::test_orders[北京] test_parametrize_02.py::TestOrders::test_orders[郑州] test_parametrize_02.py::TestOrders::test_orders[西安] 执行后置步骤 ============================== 4 passed in 1.64s ============================== 执行前置步骤 [2023-09-05 13:05:11,843][INFO][request_util.py][24] [开始发送请求:请求地址http://127.0.0.1/api/mgr/signin] [2023-09-05 13:05:12,927][INFO][request_util.py][34] [发送请求成功] [2023-09-05 13:05:12,929][INFO][request_util.py][24] [开始发送请求:请求地址http://127.0.0.1/api/mgr/orders] {'ret': 0, 'retlist': [{'id': 25, 'name': '药品订单-南京人民医院2', 'create_date': '2023-08-31T15:10:51.280Z', 'medicinelist': '[{"id": 38, "amount": "3", "name": "藿香正气"}, {"id": 37, "amount": "5", "name": "蒙脱石散"}, {"id": 36, "amount": "10", "name": "蓝芩口服液"}, {"id": 34, "amount": "10", "name": "抗病毒颗粒"}]', 'customer_name': '南京人民医院2'}, {'id': 22, 'name': '药品订单-南京人民医院1', 'create_date': '2023-08-31T15:07:28.673Z', 'medicinelist': '[{"id": 33, "amount": "3", "name": "连花清瘟胶囊"}, {"id": 31, "amount": "4", "name": "布洛芬"}]', 'customer_name': '南京人民医院1'}], 'total': 2} keyword:南京 订单名称:药品订单-南京人民医院2 keyword:南京 订单名称:药品订单-南京人民医院1 PASSED[2023-09-05 13:05:12,952][INFO][request_util.py][24] [开始发送请求:请求地址http://127.0.0.1/api/mgr/orders] [2023-09-05 13:05:12,967][INFO][request_util.py][34] [发送请求成功] {'ret': 0, 'retlist': [], 'total': 0} PASSED[2023-09-05 13:05:12,972][INFO][request_util.py][24] [开始发送请求:请求地址http://127.0.0.1/api/mgr/orders] [2023-09-05 13:05:12,987][INFO][request_util.py][34] [发送请求成功] {'ret': 0, 'retlist': [{'id': 24, 'name': '药品订单-郑州人民医院2', 'create_date': '2023-08-31T15:09:57.574Z', 'medicinelist': '[{"id": 34, "amount": "10", "name": "抗病毒颗粒"}, {"id": 35, "amount": "10", "name": "蒲地蓝消炎片"}, {"id": 36, "amount": "10", "name": "蓝芩口服液"}]', 'customer_name': '郑州人民医院2'}], 'total': 1} keyword:郑州 [2023-09-05 13:05:12,992][INFO][request_util.py][24] [开始发送请求:请求地址http://127.0.0.1/api/mgr/orders] 订单名称:药品订单-郑州人民医院2 PASSED{'ret': 0, 'retlist': [{'id': 23, 'name': '药品订单-西安中医院2', 'create_date': '2023-08-31T15:09:17.150Z', 'medicinelist': '[{"id": 30, "amount": "30", "name": "复方氨酚烷胺胶囊"}, {"id": 32, "amount": "20", "name": "对乙酰氨基酚"}, {"id": 33, "amount": "10", "name": "连花清瘟胶囊"}]', 'customer_name': '西安中医院2'}], 'total': 1} keyword:西安 订单名称:药品订单-西安中医院2 PASSED[2023-09-05 13:05:13,080][INFO][request_util.py][34] [发送请求成功] 进程已结束,退出代码0
yaml文件
- 南京- 北京
- 郑州
- 西安 标签:name,05,py,fixture,util,2023,使用,id,类中 From: https://www.cnblogs.com/Avicii2018/p/17679332.html