2

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?

4

1 回答 1

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
于 2016-03-17T11:08:32.570 回答