pytest.mark.parametrize() 列表1

yaml文件:

- 南京
- 北京
- 郑州
- 西安

 

 

代码:

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_list.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_list.py::TestOrders::test_orders[南京] 
test_parametrize_list.py::TestOrders::test_orders[北京] 
test_parametrize_list.py::TestOrders::test_orders[郑州] 
test_parametrize_list.py::TestOrders::test_orders[西安] 执行后置步骤


============================== 4 passed in 1.72s ==============================
执行前置步骤
[2023-09-05 22:01:53,804][INFO][request_util.py][24] [开始发送请求:请求地址http://127.0.0.1/api/mgr/signin]
[2023-09-05 22:01:54,981][INFO][request_util.py][34] [发送请求成功]
[2023-09-05 22:01:54,982][INFO][request_util.py][24] [开始发送请求:请求地址http://127.0.0.1/api/mgr/orders]
[2023-09-05 22:01:54,995][INFO][request_util.py][34] [发送请求成功]
{'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 22:01:54,999][INFO][request_util.py][24] [开始发送请求:请求地址http://127.0.0.1/api/mgr/orders]
[2023-09-05 22:01:55,011][INFO][request_util.py][34] [发送请求成功]
{'ret': 0, 'retlist': [], 'total': 0}
PASSED[2023-09-05 22:01:55,015][INFO][request_util.py][24] [开始发送请求:请求地址http://127.0.0.1/api/mgr/orders]
[2023-09-05 22:01:55,027][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:郑州
订单名称:药品订单-郑州人民医院2
PASSED[2023-09-05 22:01:55,031][INFO][request_util.py][24] [开始发送请求:请求地址http://127.0.0.1/api/mgr/orders]
[2023-09-05 22:01:55,043][INFO][request_util.py][34] [发送请求成功]
{'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

 

posted @ 2023-09-05 22:04  Avicii_2018  阅读(8)  评论(0编辑  收藏  举报