只有在所有参数运行后才能运行拆卸夹具吗?

时间:2016-03-16 12:43:10

标签: python pytest finalizer fixture teardown

例如,如果你有:

@pytest.mark.parametrize('lang',
                         ["EN",
                          "FR"])
def test_whats_hot_quick_links_are_displayed(self, lang):
       # Do something here

我有这个拆解夹具:

@pytest.fixture(scope='function', autouse=True)
def teardown_function(request):    
    def execute_at_the_end():
        logging.info("Ending Test Case...")   
        database.clear()

    request.addfinalizer(execute_at_the_end)

那么,如果在执行EN和FR测试运行后执行拆卸功能,而不是在每个参数运行后运行,我怎样才能执行拆解功能?

1 个答案:

答案 0 :(得分:1)

对于此行为,我使用scope=class并使用class包装我的测试:

import pytest

@pytest.yield_fixture(scope='class')
def teardown_after_all_params():
    yield
    execute_at_the_end()

@pytest.mark.usefixtures('teardown_after_all_params')
class TestLinks:
    @pytest.mark.parametrize('lang', ["EN", "FR"])
    def test_whats_hot_quick_links_are_displayed(self, lang):
        # Do something here