pytest fixtures 夹具的应用

1.在teardown_method中获取参数化的参数,打印带参数化的函数名

pytest_me.py:

import pytest

param_data = [
    {
        'ip': '172.31.3.80',
        'unit': 1,
    },
    {
        'ip': '172.31.3.80',
        'unit': 2,
    },
]


class Base(object):
    def setup_method(self, method):
        param = self._item.callspec.getparam('param')
        print(param)


class MyFeature(Base):
    def setup_method(self, method):
        super().setup_method(method)


class Test_Class(MyFeature):

    def teardown_method(self, method):
        param = self._item.callspec.getparam('param')
        print(param)
        if param['unit'] == 1:
            print(f'teardown_method >> {type(self).__name__}, {method.__name__}[param0]')
        else:
            print(f'teardown_method >> {type(self).__name__}, {method.__name__}[param1]')

    @pytest.mark.parametrize("param", param_data)
    def Test_my_first_test(self, param):
        # do stuff that is changed based on fragment_length
        assert True  # verify_stuff(fragment_length)

 

conftest.py

import pytest

@pytest.hookimpl(hookwrapper = True)
def pytest_runtest_protocol(item, nextitem):
    item.cls._item = item
    yield

 

输出:

 

参考:

pytest: get parametrize values during setup/teardown method

pytest: get parametrize values during setup/teardown method

posted @ 2022-10-10 10:28  小柴i  阅读(48)  评论(0编辑  收藏  举报