class 中使用 fixture()

 

import json
import pprint

import pytest

from Slience.utils.login_util import Login
from Slience.utils.request_util import SendRequest

@pytest.fixture(scope='class',autouse=False)
def my_fixture():
    print("我是前置步骤")
    Login().login_system()
    yield
    print("我是后置步骤")
    SendRequest.s.close()


class TestSearch:
    # def setup(self,my_fixture):  #  对某个类中使用fixture
    #     # 登录
    #     print("执行setup_class")
    #     res = Login().login_system()
    #
    #     return res

    def test_search_customer(self,my_fixture):
        # 查询
        method = 'GET'
        url = 'http://127.0.0.1/api/mgr/customers'
        data = {
            "action": "list_customer",
            "pagesize": 100,
            "pagenum": 1,
            "keywords": "人民医院"
        }
        # 使用同一个session对象发送请求, 保持在一个会话中
        res = SendRequest().send_request(method, url, params=data)
        print(json.dumps(res.json(),ensure_ascii=False,indent=4))
        return res

    def test_search_medicines(self):
        # 查询
        method = 'GET'
        url = 'http://127.0.0.1/api/mgr/medicines'
        data = {
            "action": "list_medicine",
            "pagesize": 100,
            "pagenum": 1,
            "keywords": ""
        }
        # 使用同一个session对象发送请求, 保持在一个会话中
        res = SendRequest().send_request(method, url, params=data)
        print(json.dumps(res.json(),ensure_ascii=False,indent=4))
        # print('接口响应时间:',res.elapsed.total_seconds())


        return res

运行结果:

============================= test session starts =============================
collecting ... collected 2 items

test_fixture_class2.py::TestSearch::test_search_customer [2023-09-05 22:27:23,947][INFO][request_util.py][24] [开始发送请求:请求地址http://127.0.0.1/api/mgr/signin]

test_fixture_class2.py::TestSearch::test_search_medicines 我是后置步骤


============================== 2 passed in 1.62s ==============================
我是前置步骤
[2023-09-05 22:27:25,165][INFO][request_util.py][34] [发送请求成功]
[2023-09-05 22:27:25,166][INFO][request_util.py][24] [开始发送请求:请求地址http://127.0.0.1/api/mgr/customers]
[2023-09-05 22:27:25,201][INFO][request_util.py][34] [发送请求成功]
[2023-09-05 22:27:25,205][INFO][request_util.py][24] [开始发送请求:请求地址http://127.0.0.1/api/mgr/medicines]
{
    "ret": 0,
    "retlist": [
        {
            "id": 43,
            "name": "南京人民医院3",
            "phonenumber": "2551867813",
            "address": "江苏省-南京市-秦淮区-汉中路-5013"
        },
        {
            "id": 42,
            "name": "南京人民医院2",
            "phonenumber": "2551867812",
            "address": "江苏省-南京市-秦淮区-汉中路-5012"
        },
        {
            "id": 41,
            "name": "南京人民医院1",
            "phonenumber": "2551867811",
            "address": "江苏省-南京市-秦淮区-汉中路-5011"
        },
        {
            "id": 40,
            "name": "郑州人民医院3",
            "phonenumber": "0371867853",
            "address": "河南省-郑州市-上街区-前进路-203"
        },
        {
            "id": 39,
            "name": "郑州人民医院2",
            "phonenumber": "0371867852",
            "address": "河南省-郑州市-金水区-建设路-202"
        },
        {
            "id": 38,
            "name": "郑州人民医院1",
            "phonenumber": "0371867851",
            "address": "河南省-郑州市-中原区-解放路-201"
        }
    ],
    "total": 6
}
PASSED[2023-09-05 22:27:25,218][INFO][request_util.py][34] [发送请求成功]
{
    "ret": 0,
    "retlist": [
        {
            "id": 38,
            "name": "藿香正气",
            "sn": "YP-009",
            "desc": "解表化湿、理气和中。治疗胃炎、呕吐、泄泻"
        },
        {
            "id": 37,
            "name": "蒙脱石散",
            "sn": "YP-008",
            "desc": "止泻药,用于治疗成人及儿童急、慢性腹泻"
        },
        {
            "id": 36,
            "name": "蓝芩口服液",
            "sn": "YP-007",
            "desc": "清热解毒,利咽消肿。用于急性咽炎、肺胃实热证所致的咽痛咽干等"
        },
        {
            "id": 35,
            "name": "蒲地蓝消炎片",
            "sn": "YP-006",
            "desc": "清热解毒,抗炎消肿。用于疖肿、咽炎、扁桃腺炎等疾病的治疗"
        },
        {
            "id": 34,
            "name": "抗病毒颗粒",
            "sn": "YP-005",
            "desc": "清热祛湿,凉血解毒。用于风热感冒,上呼吸道感染,流感"
        },
        {
            "id": 33,
            "name": "连花清瘟胶囊",
            "sn": "YP-004",
            "desc": "用于治疗流行性感冒属热毒袭肺证,发热或高热,咳嗽头痛,咽痛等"
        },
        {
            "id": 32,
            "name": "对乙酰氨基酚",
            "sn": "YP-003",
            "desc": "用于普通感冒或流行性感冒引起的发热,也用于缓解轻至中度疼痛如头痛、关节痛、偏头痛、牙痛等"
        },
        {
            "id": 31,
            "name": "布洛芬",
            "sn": "YP-002",
            "desc": "缓解头痛,肌痛,关节痛,痛经等,或感冒引起的发热"
        },
        {
            "id": 30,
            "name": "复方氨酚烷胺胶囊",
            "sn": "YP-001",
            "desc": "缓解感冒引起的发热头痛、四肢酸痛、流鼻涕、鼻塞、咽痛等症状"
        },
        {
            "id": 29,
            "name": "青霉素盒装3",
            "sn": "YP-32342343",
            "desc": "青霉素注射液,每支15ml,40支装"
        },
        {
            "id": 28,
            "name": "青霉素盒装2",
            "sn": "YP-32342342",
            "desc": "青霉素注射液,每支15ml,30支装"
        },
        {
            "id": 27,
            "name": "青霉素盒装1",
            "sn": "YP-32342341",
            "desc": "青霉素注射液,每支15ml,20支装"
        }
    ],
    "total": 12
}
PASSED

 

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