簡體   English   中英

py.test每個燈具進行多個測試

[英]py.test multiple tests per fixture

我有以下幾點。

@pytest.fixture
def patch_socket(monkeypatch):
    def gethostname():
        return 'web01-east.domain.com'

    monkeypatch.setattr(socket, 'gethostname', gethostname)


def test__get_pod(patch_socket):
    assert __get_pod() == 'east'

如果要測試以下主機名,正確的方法是什么

  1. web01-east.domain.com
  2. redis01-master-east.domain.com
  3. web01.domain.com

我應該為每個設備配備一個新的夾具,還是有辦法在測試本身中傳遞主機名?

使用此代碼

@pytest.fixture(params=['web01-east.domain.com', 'redis01-master-east.domain.com', 'web01.domain.com'])
def patch_socket(request, monkeypatch):
    def gethostname():
        return request.param
    monkeypatch.setattr(socket, 'gethostname', gethostname)

def test__get_pod(patch_socket):
    assert __get_pod() == 'east'

這將動態創建3個測試。 如果使用-vv運行,您將看到類似以下內容:

<FILE>::test__get_pod[web01-east.domain.comm PASSED
<FILE>::test__get_pod[redis01-master-east.domain.com] PASSED
<FILE>::test__get_pod[web01.domain.com PASSED

暫無
暫無

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

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