Is it possible to run tear down fixture only after all params runs?

吃可爱长大的小学妹 提交于 2019-12-20 03:33:32

问题


For instance if you have:

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

and i have this teardown fixture in conftest:

@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)

So how can i make the teardown function execute only after both EN and FR test runs are executed instead of having this run after each param run?


回答1:


For this behaviour I use scope=class and wraps my test with 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


来源:https://stackoverflow.com/questions/36036037/is-it-possible-to-run-tear-down-fixture-only-after-all-params-runs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!