亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

pytest:通過固定裝置參數化測試用例

pytest:通過固定裝置參數化測試用例

holdtom 2022-12-27 16:34:54
您如何編寫產生/返回參數化測試參數的夾具(一種方法)?例如,我有一個測試如下:@pytest.mark.parametrize(    "input,expected",     [("hello", "hello"),    ("world", "world")])def test_get_message(self, input, expected):    assert expected == MyClass.get_message(input)我對以下方法感興趣,而不是通過input和expected傳遞:@[email protected](scope="session")def test_messages(self):    # what should I write here to return multiple     # test case with expected value for each?    passdef test_get_message(self, test_messages):    expected = test_messages["expected"] # somehow extracted from test_messages?    input = test_messages["input"]       # somehow extracted from test message?    assert expected == MyClass.get_message(input)
查看完整描述

2 回答

?
不負相思意

TA貢獻1777條經驗 獲得超10個贊

要將參數移動到夾具中,您可以使用夾具參數:


@pytest.fixture(params=[("hello", "hello"),

    ("world", "world")], scope="session")

def test_messages(self, request):

    return request.param


def test_get_message(self, test_messages):

    input = test_messages[0]   

    expected = test_messages[1]

    assert expected == MyClass.get_message(input)

您還可以將參數放入單獨的函數中(與 wih 相同parametrize),例如


def get_test_messages():

    return [("hello", "hello"), ("world", "world")]


@pytest.fixture(params=get_test_messages(), scope="session")

def test_messages(self, request):

    return request.param


查看完整回答
反對 回復 2022-12-27
?
烙印99

TA貢獻1829條經驗 獲得超13個贊

對我來說,你似乎想返回一個字典數組:


@pytest.fixture(scope="session")

def test_messages():

    return [

        {

            "input": "hello",

            "expected": "world"

        },

        {

            "input": "hello",

            "expected": "hello"

        }

    ]

要在測試用例中使用它,您需要遍歷數組:


def test_get_message(self, test_messages):

    for test_data in test_messages:

        input = test_data["input"]

        expected = test_data["expected"]

        assert input == expected

但我不確定這是否是最好的方法,因為它仍然被視為只有一個測試用例,因此它只會在輸出/報告中顯示為一個測試用例。


查看完整回答
反對 回復 2022-12-27
  • 2 回答
  • 0 關注
  • 97 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號