本文介绍了是否只有在所有参数都运行后才能运行拆卸夹具?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,如果您拥有:

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

那么我如何才能使拆解功能仅在同时执行EN和FR测试运行之后执行,而不是在每个参数运行之后都执行?

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?

推荐答案

为此,我使用scope=class并用class包装测试:

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

这篇关于是否只有在所有参数都运行后才能运行拆卸夹具?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 12:51