简体   繁体   中英

py.test multiple tests per fixture

I have the following.

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

What is the correct way if I want to test for the following hostnames

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

Should I have a new fixture for each or is there a way to pass in a hostname in the test itself?

use this code

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

This will create on the fly 3 tests. If you run with -vv you will see something like:

<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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM