Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python py.对每个夹具进行多次试验_Python_Unit Testing_Pytest - Fatal编程技术网

Python py.对每个夹具进行多次试验

Python py.对每个夹具进行多次试验,python,unit-testing,pytest,Python,Unit Testing,Pytest,我有以下几点 @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' 如果要测

我有以下几点

@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'
如果要测试以下主机名,正确的方法是什么

web01-east.domain.com redis01-master-east.domain.com web01.domain.com 我应该为每个测试设置一个新的fixture,还是有办法在测试本身中传递主机名?

使用此代码

@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