python - 我怎样才能使用 pytest 选项作为固定装置而不是重复自己?

标签 python pytest dry fixtures

我有一个带有 conftest.py 的测试套件,它定义了一些选项和一些用于检索它们的固定装置:

def pytest_addoption(parser):
  parser.addoption("--ip", action="store")
  parser.addoption("--port", action="store")

@pytest.fixture
def ip(request):
  return request.config.getoption("ip")

@pytest.fixture
def port(request):
  return request.config.getoption("ip")

(我在复制粘贴错误中漏掉了一个观点)

我的测试可以非常 Eloquent 地表达他们需要的选项:

def test_can_ping(ip):
  ...

def test_can_net_cat(ip, port):
  ...

但是……

我尽量避免在这里重复自己:我必须在三个地方指定配置参数的名称才能使其正常工作。

如果我有这样的东西,我本可以避免复制粘贴错误:

# does not exist:
@pytest.option_fixture
def ip(request, parser):
  return request.config.getoption(this_function_name)

或者这个

def pytest_addoption(parser):
  # does not exist: an as_fixture parameter
  parser.addoption("--ip", action="store", as_fixture=True)
  parser.addoption("--port", action="store", as_fixture=True)

有没有办法告诉pytest添加一个选项相应的 fixture 来实现 DRY/SPOT 代码?

最佳答案

经过一些测试,我找到了一些有用的东西。这可能不是最好的方法,但我认为它非常令人满意。 下面的所有代码都已添加到 conftest.py 模块,除了两个测试。

首先定义一个包含选项数据的字典:

options = {
    'port': {'action': 'store', 'help': 'TCP port', 'type': int},
    'ip': {'action': 'store', 'help': 'IP address', 'type': str},
}

我们可以不用helptype,但稍后会有一定的实用性。

然后你可以使用这个options来创建pytest选项:

def pytest_addoption(parser):
    for option, config in options.items():
        parser.addoption(f'--{option}', **config)

此时,pytest --help 给出了这个(注意 help 数据使用,它提供了方便的文档):

usage: pytest [options] [file_or_dir] [file_or_dir] [...]
...
custom options:
  --port=PORT           TCP port
  --ip=IP               IP address

最后我们必须定义固定装置。我通过提供一个 make_fixture 函数来做到这一点,该函数在 conftest.py 读取的循环中使用,以动态创建固定装置并将它们添加到模块的全局范围内:

def make_fixture(option, config):
    func = lambda request: request.config.getoption(option)
    func.__doc__ = config['help']
    globals()[option] = pytest.fixture()(func)


for option, config in options.items():
    make_fixture(option, config)

同样,“帮助”数据用于为创建的固定装置构建文档字符串并记录它们。因此,调用 pytest --fixtures 打印如下:

...
---- fixtures defined from conftest ----
ip
    IP address
port
    TCP port

调用 pytest --port 80 --ip 127.0.0.1,通过以下两个非常简单的测试,似乎验证了这个技巧(这里的 type 数据显示了它实用程序,它使 pytest 将端口转换为 int,而不是字符串):

def test_ip(ip):
    assert ip == '127.0.0.1'


def test_ip_port(ip, port):
    assert ip == '127.0.0.1'
    assert port == 80

(非常有趣的问题,我希望看到更多这样的问题)

关于python - 我怎样才能使用 pytest 选项作为固定装置而不是重复自己?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55339470/

相关文章:

python - 如何从 python 循环中获取特定术语

python - 记录循环生成的数字

python - 在tox+pytest中使用自己的包

python - 如何为多个测试打补丁

python - 使用相同参数重复初始化类的最佳实践(金字塔)?

c - 为什么特定的 C 数据类型在多个标准头文件中声明?

Javascript、Rails View 、content_for 和 DRYness

python - tkinter 删除所有但未选择的项目

python - 将 jupyter 笔记本 (ipython) 集成到组织模式笔记本中

python - 在pytest中,如何确保在类级之前调用模块级 fixture