簡體   English   中英

在 pytest 中的參數化測試的測試運行之前僅運行一次設置

[英]Run setup only once before a test run for a parameterized test in pytest

我想使用一個夾具來為測試設置資源,該測試應該在測試開始之前只創建一次資源,但測試是參數化的。如果我按照下面提到的方式進行操作,它會為 xx 和 yy 的每個組合調用夾具,任何人都可以用另一種方式幫助我實現這一目標嗎?

@pytest.mark.usefixtures('create_files')
@pytest.mark.parametrize('xx', ['a', 'b', 'c'])
@pytest.mark.parametrize('yy', ['1', '2', '3'])
def test_operaration(self):
    .
    .
    .
    .

還有什么方法可以將 xx 和 yy 的值傳遞給每次運行的create_files fixture?

create_files fixture 的 scope 設置為session

@pytest.fixture(scope='session')
def create_files():
    ...

最后這有助於解決我的問題:

@pytest.fixture(scope='function')
def create_files(request: FixtureRequest):
    xx = request.getfixturevalue('xx')
    yy = request.getfixturevalue('yy')
    ...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM