[英]py.test multiple tests for different results
Is there a good way to do this. 有一个好的方法可以做到这一点。
@pytest.fixture(params=[
"web01-east.domain.com",
"web01-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'
Right now that'll work but I want to have the last test fail but be ok since if there is no -east in the hostname the __get_pod() function returns unknown. 现在可以了,但是我希望上一次测试失败,但是可以,因为如果主机名中没有-east,__ get_pod()函数将返回未知。
Is there a way to tell py.test that I want to pass in a param list of what tests should equal like 有没有办法告诉py.test我想通过什么测试应该相等的参数列表
[
('web01-east.domain.com', 'web')
('redis01-master-east.domain.com', 'redis-master')
('web01.domain.com', 'Unknown')
]
Instead of monkey-patching socket.gethostname
, make the __get_pod
to accept a parameter. 代替猴子修补
socket.gethostname
,使__get_pod
接受参数。 It will make the code more testable: 它将使代码更具可测试性:
Here's an example with pytest.mark.parametrize
: 这是
pytest.mark.parametrize
的示例:
import re
import pytest
def __get_pod(hostname): # dummy impl.
hostname = hostname.split('.', 1)[0]
if '-' not in hostname:
return 'Unknown'
hostname = re.sub(r'\d+', '', hostname)
return hostname.rsplit('-', 1)[0]
@pytest.mark.parametrize(['hostname', 'expected'], [
["web01-east.domain.com", 'web'],
["redis01-master-east.domain.com", 'redis-master'],
["web01.domain.com", 'Unknown'],
])
def test__get_pod(hostname, expected):
assert __get_pod(hostname) == expected
If you want to do it with mockey-patching (or, you can't change __get_pod
signature) 如果您想通过嘲笑补丁来做到这一点(或者,您无法更改
__get_pod
签名)
import re
import socket
import pytest
def __get_pod():
hostname = socket.gethostname()
hostname = hostname.split('.', 1)[0]
if '-' not in hostname:
return 'Unknown'
hostname = re.sub(r'\d+', '', hostname)
return hostname.rsplit('-', 1)[0]
@pytest.mark.parametrize(['hostname', 'expected'], [
["web01-east.domain.com", 'web'],
["redis01-master-east.domain.com", 'redis-master'],
["web01.domain.com", 'Unknown'],
])
def test__get_pod(monkeypatch, hostname, expected):
monkeypatch.setattr(socket, 'gethostname', lambda: hostname)
assert __get_pod() == expected
An alternative to falsetru's answer, which is more like what you've started like is to use the return value from the fixture. falsetru答案的替代方法(更像是您开始使用的方法)是使用灯具的返回值。 Given that
__get_pod
looks something like (modified from falsetru's dummy): 鉴于
__get_pod
看起来像(从falsetru的假人修改):
def __get_pod():
hostname = socket.gethostname().split('.', 1)[0]
if '-' not in hostname:
return 'Unknown'
hostname = re.sub(r'\d+', '', hostname)
return hostname.rsplit('-', 1)[0]
You can test it like: 您可以像这样测试它:
@pytest.fixture(params=[
('web01-east.domain.com', 'web'),
('redis01-master-east.domain.com', 'redis-master'),
('web01.domain.com', 'Unknown')
])
def patch_socket(request, monkeypatch):
def gethostname():
return request.param[0]
monkeypatch.setattr(socket, 'gethostname', gethostname)
return request.param[1]
def test__get_pod(patch_socket):
assert __get_pod() == patch_socket
This requires the less changes from your original code. 这要求您对原始代码的更改更少。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.