pytest.mark.parametrize() 字典

发布时间 2023-09-05 22:08:42作者: Avicii_2018

yaml文件

-
  action: list_order
  keywords: 南京
-
  action: list_order
  keywords: 郑州
-
  action: list_order
  keywords: 西安

代码:

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_dict.yaml')
    print(list)
    @pytest.mark.parametrize("info",list)
    def test_orders(self,my_fixture,info):
        url = "http://127.0.0.1/api/mgr/orders"
        method = "post"
        data = {

            "action": info["action"],
            "pagesize": 10,
            "pagenum": 1,
            "keywords": info["keywords"]
        }

        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:{info['keywords']}")
                print(f"订单名称:{res.json()['retlist'][i]['name']}")
                assert info['keywords'] in res.json()['retlist'][i]["name"]

        return res

 

运行结果:

============================= test session starts =============================
collecting ... [{'action': 'list_order', 'keywords': '南京'}, {'action': 'list_order', 'keywords': '郑州'}, {'action': 'list_order', 'keywords': '西安'}]
collected 3 items

test_parametrize_dict.py::TestOrders::test_orders[info0] 
test_parametrize_dict.py::TestOrders::test_orders[info1] 
test_parametrize_dict.py::TestOrders::test_orders[info2] 执行后置步骤
[2023-09-05 21:41:05,279][INFO][request_util.py][34] [发送请求成功]


============================== 3 passed in 1.60s ==============================
执行前置步骤
[2023-09-05 21:41:04,078][INFO][request_util.py][24] [开始发送请求:请求地址http://127.0.0.1/api/mgr/signin]
[2023-09-05 21:41:05,217][INFO][request_util.py][34] [发送请求成功]
[2023-09-05 21:41:05,218][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 21:41:05,247][INFO][request_util.py][34] [发送请求成功]
[2023-09-05 21:41:05,251][INFO][request_util.py][24] [开始发送请求:请求地址http://127.0.0.1/api/mgr/orders]
{'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}
[2023-09-05 21:41:05,264][INFO][request_util.py][34] [发送请求成功]
keyword:郑州
订单名称:药品订单-郑州人民医院2
PASSED[2023-09-05 21:41:05,267][INFO][request_util.py][24] [开始发送请求:请求地址http://127.0.0.1/api/mgr/orders]
{'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
进程已结束,退出代码0