簡體   English   中英

Pytest跳過測試,具有一定的參數值

[英]Pytest skip test with certain parameter value

我有我想要參數化的測試,但是某些測試只應該應用於參數的一個值。 為了給出一個具體的例子,下面,我想將參數onetwo應用於test_A ,但僅將參數onetest_B

現行守則

@pytest.fixture(params=['one', 'two'])
def data(request):

    if request.param == 'one'
         data = 5
    return data

def test_A(data):

    assert True

def test_B(data):

    assert True

期望的結果

我基本上想要看起來像這樣的東西,但我無法弄清楚如何在pytest中正確編碼:

@pytest.fixture(params=['one', 'two'])
def data(request):

    data = 5
    return data

def test_A(data):

    assert True

@pytest.skipif(param=='two')
def test_B(data):

    assert True

根據您的答案,您可以檢查輸入並調用pytest.skip()如果您不希望測試運行。

您可以在測試中進行檢查:

def test_A(data):
    assert True

def test_B(data):
    if data.param == 'two':
        pytest.skip()
    assert 'foo' == 'foo'

或者您可以在子類中重新定義測試夾具:

class TestA:
    def test_A(self, data):
        assert True

class TestB:
    @pytest.fixture
    def data(self, data):
        if data.param == 'two':
            pytest.skip()
        return data

    def test_B(self, data):
        assert 'foo' == 'foo'

另一個小建議:您的Data類可以替換為namedtuple,即

import collections
Data = collections.namedtuple('Data', 'data, param')

我找到了一個有效的解決方案,但我也歡迎更多的解決方案,因為這有點“hacky”:

class Data:

    def__init__(self, data, param):
        self.data = data
        self.param = param

@pytest.fixture(params=['one', 'two'])
def data(request):

    data = 5
    return Data(data, request.param)

def test_A(data):

    assert True

def test_B(data):

    if data.param == 'two':
        assert True
    else:
        assert 'foo' == 'foo'

暫無
暫無

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

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