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