使用 returns 列表的夹具来参数化 pytest 函数的最佳方法是什么?
What is the best way to parameterize a pytest function using a fixture that returns a list?
现在我有一个看起来像这个人为示例的测试文件:
import pytest
def colors():
# Expensive operation
return ['red', 'yellow', 'blue']
@pytest.mark.parametrize('color', colors())
def test_colors(color):
assert color != 'mauve'
这工作正常,但由于 colors()
是一项昂贵的操作,我想利用 pytest 的缓存并使其成为会话范围的固定装置。此外,我还想使用它作为固定装置编写其他测试,如
def test_colors_list(colors):
assert len(colors) == 3
理想情况下,我的测试文件应该类似于
@pytest.fixture(scope='session')
def colors():
# Expensive operation
return ['red', 'yellow', 'blue']
@pytest.mark.parametrize('color', colors)
def test_colors(color):
assert color != 'mauve'
def test_colors_list(colors):
assert len(colors) == 3
但这会导致错误,所以我没有正确处理这个问题。
理想情况下,我还想引用 colors()
中的其他灯具,以及仍然参数化 test_colors()
以生成多个函数。
编写这些测试的最佳方式是什么?
语法错误可按如下方式解决:
test_1.py:
import pytest
@pytest.fixture(scope='session')
def colors():
# Expensive operation
return ['red', 'yellow', 'blue']
@pytest.mark.parametrize('color',colors())
def test_colors(color):
assert color != 'mauve'
def test_colors_list(colors):
assert len(colors) == 3
但是,运行 pytest 中带有显示夹具设置和拆卸详细信息的 --setup-show
选项的先前代码将向您显示以下内容:
test_1.py::test_colors[red]
SETUP F color[red]
test_1.py::test_colors[red] (fixtures used: color)PASSED
TEARDOWN F color[red]
test_1.py::test_colors[yellow]
SETUP F color[yellow]
test_1.py::test_colors[yellow] (fixtures used: color)PASSED
TEARDOWN F color[yellow]
test_1.py::test_colors[blue]
SETUP F color[blue]
test_1.py::test_colors[blue] (fixtures used: color)PASSED
TEARDOWN F color[blue]
test_1.py::test_colors_list
SETUP S colors
test_1.py::test_colors_list (fixtures used: colors)PASSED
TEARDOWN S colors
正如您在输出中看到的那样,每个测试用例的颜色夹具为 setup/teardown,最终测试用例的颜色夹具再次为 setup/teardown。
为避免这种情况,您可以按照以下方式最低限度地重构代码:
test_1.py:
@pytest.fixture(scope='session')
def colors():
# Expensive operation
return ['red', 'yellow', 'blue']
def test_colors(colors):
for color in colors:
assert color != 'mauve'
def test_colors_list(colors):
assert len(colors) == 3
运行 再次使用 --setup-show
进行 pytest 将输出以下内容:
test_1.py::test_colors
SETUP S colors
test_1.py::test_colors (fixtures used: colors)PASSED
test_1.py::test_colors_list
test_1.py::test_colors_list (fixtures used: colors)PASSED
TEARDOWN S colors
您可以在其中检查夹具是否仅 setup/teardown 一次用于所有测试。
您可以查看 pytest docs
中的相关部分
一个解决方案是在单独的函数中生成颜色,并将其结果用作灯具的 params
参数,然后您可以多次使用该参数
import pytest
def colors():
print('expensive')
return ['blue', 'red', 'mauve']
@pytest.fixture(params=colors())
def color(request):
return request.param
def test_bla(color):
print(color, end='')
def test_foo(color):
print(color, end='')
如果你 运行 pytest -s
你只会在输出中看到字符串 expensive
一次:
$py.test -sv
======================== test session starts =========================
platform linux -- Python 3.4.3, pytest-3.4.1, py-1.5.2, pluggy-0.6.0 -- /home/nils/test/bin/python3
cachedir: .pytest_cache
rootdir: /home/nils/test/bla, inifile:
collecting 0 items expensive
collected 6 items
test_bla.py::test_bla[blue] bluePASSED
test_bla.py::test_bla[red] redPASSED
test_bla.py::test_bla[mauve] mauvePASSED
test_bla.py::test_foo[blue] bluePASSED
test_bla.py::test_foo[red] redPASSED
test_bla.py::test_foo[mauve] mauvePASSED
====================== 6 passed in 0.04 seconds ======================
但是,昂贵的函数在导入时是 运行,这不是一个好的行为。
作为解决 colors()
结果的简单缓存的工作,适用于会话范围:
@pytest.fixture(scope='session')
def colors():
try:
return colors._res
except AttributeError:
# Expensive operation
print()
print('expensive')
colors._res = ['red', 'yellow', 'blue']
return colors._res
@pytest.mark.parametrize('color', colors())
def test_colors(color):
assert color != 'mauve'
def test_colors_list(colors):
assert len(colors) == 3
虽然函数 colors()
在其中被调用了两次,但昂贵的计算对于所有测试只进行了一次:
$ pytest -sv test_colors.py
================================================= test session starts ==================================================
platform darwin -- Python 3.6.4, pytest-3.0.7, py-1.4.33, pluggy-0.4.0 -- /Users/mike/miniconda3/envs/py36/bin/python
cachedir: .cache
rootdir: /Users/mike/tmp, inifile:
plugins: click-0.1, cov-2.4.0, mock-1.6.0, pylint-0.7.1, xdist-1.15.0, xonsh-0.5.8
collecting 0 items
expensive
collected 4 items
test_colors.py::test_colors[red] PASSED
test_colors.py::test_colors[yellow] PASSED
test_colors.py::test_colors[blue] PASSED
test_colors.py::test_colors_list PASSED
另一种解决方案是在会话灯具中生成颜色并在常规灯具中迭代返回的结果,然后您可以多次使用它
import pytest
@pytest.fixture(scope='session')
def a():
return 'purple'
@pytest.fixture(scope='session')
def colors(a):
print('expensive')
return ['blue', 'red', 'mauve', a]
@pytest.fixture(params=range(4)) # unfortunately we need to know the number of values returned by `colors()`
def color(request, colors):
return colors[request.param]
def test_bla(color):
print(color, end='')
def test_foo(color):
print(color, end='')
如果你 运行 pytest -s
你只会在输出中看到字符串 expensive
一次:
$ py.test -sv
======================== test session starts =========================
platform linux -- Python 3.4.3, pytest-3.4.1, py-1.5.2, pluggy-0.6.0 -- cwd
cachedir: .pytest_cache
rootdir: cwd, inifile:
collected 8 items
test_bla.py::test_bla[0] expensive
bluePASSED
test_bla.py::test_bla[1] redPASSED
test_bla.py::test_bla[2] mauvePASSED
test_bla.py::test_bla[3] purplePASSED
test_bla.py::test_foo[0] bluePASSED
test_bla.py::test_foo[1] redPASSED
test_bla.py::test_foo[2] mauvePASSED
test_bla.py::test_foo[3] purplePASSED
====================== 8 passed in 0.03 seconds ======================
另外:昂贵的函数不会在导入时调用(查看 expensive
出现在输出中的位置)并且您可以在 colors
中使用其他会话装置
现在我有一个看起来像这个人为示例的测试文件:
import pytest
def colors():
# Expensive operation
return ['red', 'yellow', 'blue']
@pytest.mark.parametrize('color', colors())
def test_colors(color):
assert color != 'mauve'
这工作正常,但由于 colors()
是一项昂贵的操作,我想利用 pytest 的缓存并使其成为会话范围的固定装置。此外,我还想使用它作为固定装置编写其他测试,如
def test_colors_list(colors):
assert len(colors) == 3
理想情况下,我的测试文件应该类似于
@pytest.fixture(scope='session')
def colors():
# Expensive operation
return ['red', 'yellow', 'blue']
@pytest.mark.parametrize('color', colors)
def test_colors(color):
assert color != 'mauve'
def test_colors_list(colors):
assert len(colors) == 3
但这会导致错误,所以我没有正确处理这个问题。
理想情况下,我还想引用 colors()
中的其他灯具,以及仍然参数化 test_colors()
以生成多个函数。
编写这些测试的最佳方式是什么?
语法错误可按如下方式解决:
test_1.py:
import pytest
@pytest.fixture(scope='session')
def colors():
# Expensive operation
return ['red', 'yellow', 'blue']
@pytest.mark.parametrize('color',colors())
def test_colors(color):
assert color != 'mauve'
def test_colors_list(colors):
assert len(colors) == 3
但是,运行 pytest 中带有显示夹具设置和拆卸详细信息的 --setup-show
选项的先前代码将向您显示以下内容:
test_1.py::test_colors[red]
SETUP F color[red]
test_1.py::test_colors[red] (fixtures used: color)PASSED
TEARDOWN F color[red]
test_1.py::test_colors[yellow]
SETUP F color[yellow]
test_1.py::test_colors[yellow] (fixtures used: color)PASSED
TEARDOWN F color[yellow]
test_1.py::test_colors[blue]
SETUP F color[blue]
test_1.py::test_colors[blue] (fixtures used: color)PASSED
TEARDOWN F color[blue]
test_1.py::test_colors_list
SETUP S colors
test_1.py::test_colors_list (fixtures used: colors)PASSED
TEARDOWN S colors
正如您在输出中看到的那样,每个测试用例的颜色夹具为 setup/teardown,最终测试用例的颜色夹具再次为 setup/teardown。
为避免这种情况,您可以按照以下方式最低限度地重构代码:
test_1.py:
@pytest.fixture(scope='session')
def colors():
# Expensive operation
return ['red', 'yellow', 'blue']
def test_colors(colors):
for color in colors:
assert color != 'mauve'
def test_colors_list(colors):
assert len(colors) == 3
运行 再次使用 --setup-show
进行 pytest 将输出以下内容:
test_1.py::test_colors
SETUP S colors
test_1.py::test_colors (fixtures used: colors)PASSED
test_1.py::test_colors_list
test_1.py::test_colors_list (fixtures used: colors)PASSED
TEARDOWN S colors
您可以在其中检查夹具是否仅 setup/teardown 一次用于所有测试。
您可以查看 pytest docs
中的相关部分一个解决方案是在单独的函数中生成颜色,并将其结果用作灯具的 params
参数,然后您可以多次使用该参数
import pytest
def colors():
print('expensive')
return ['blue', 'red', 'mauve']
@pytest.fixture(params=colors())
def color(request):
return request.param
def test_bla(color):
print(color, end='')
def test_foo(color):
print(color, end='')
如果你 运行 pytest -s
你只会在输出中看到字符串 expensive
一次:
$py.test -sv
======================== test session starts =========================
platform linux -- Python 3.4.3, pytest-3.4.1, py-1.5.2, pluggy-0.6.0 -- /home/nils/test/bin/python3
cachedir: .pytest_cache
rootdir: /home/nils/test/bla, inifile:
collecting 0 items expensive
collected 6 items
test_bla.py::test_bla[blue] bluePASSED
test_bla.py::test_bla[red] redPASSED
test_bla.py::test_bla[mauve] mauvePASSED
test_bla.py::test_foo[blue] bluePASSED
test_bla.py::test_foo[red] redPASSED
test_bla.py::test_foo[mauve] mauvePASSED
====================== 6 passed in 0.04 seconds ======================
但是,昂贵的函数在导入时是 运行,这不是一个好的行为。
作为解决 colors()
结果的简单缓存的工作,适用于会话范围:
@pytest.fixture(scope='session')
def colors():
try:
return colors._res
except AttributeError:
# Expensive operation
print()
print('expensive')
colors._res = ['red', 'yellow', 'blue']
return colors._res
@pytest.mark.parametrize('color', colors())
def test_colors(color):
assert color != 'mauve'
def test_colors_list(colors):
assert len(colors) == 3
虽然函数 colors()
在其中被调用了两次,但昂贵的计算对于所有测试只进行了一次:
$ pytest -sv test_colors.py
================================================= test session starts ==================================================
platform darwin -- Python 3.6.4, pytest-3.0.7, py-1.4.33, pluggy-0.4.0 -- /Users/mike/miniconda3/envs/py36/bin/python
cachedir: .cache
rootdir: /Users/mike/tmp, inifile:
plugins: click-0.1, cov-2.4.0, mock-1.6.0, pylint-0.7.1, xdist-1.15.0, xonsh-0.5.8
collecting 0 items
expensive
collected 4 items
test_colors.py::test_colors[red] PASSED
test_colors.py::test_colors[yellow] PASSED
test_colors.py::test_colors[blue] PASSED
test_colors.py::test_colors_list PASSED
另一种解决方案是在会话灯具中生成颜色并在常规灯具中迭代返回的结果,然后您可以多次使用它
import pytest
@pytest.fixture(scope='session')
def a():
return 'purple'
@pytest.fixture(scope='session')
def colors(a):
print('expensive')
return ['blue', 'red', 'mauve', a]
@pytest.fixture(params=range(4)) # unfortunately we need to know the number of values returned by `colors()`
def color(request, colors):
return colors[request.param]
def test_bla(color):
print(color, end='')
def test_foo(color):
print(color, end='')
如果你 运行 pytest -s
你只会在输出中看到字符串 expensive
一次:
$ py.test -sv
======================== test session starts =========================
platform linux -- Python 3.4.3, pytest-3.4.1, py-1.5.2, pluggy-0.6.0 -- cwd
cachedir: .pytest_cache
rootdir: cwd, inifile:
collected 8 items
test_bla.py::test_bla[0] expensive
bluePASSED
test_bla.py::test_bla[1] redPASSED
test_bla.py::test_bla[2] mauvePASSED
test_bla.py::test_bla[3] purplePASSED
test_bla.py::test_foo[0] bluePASSED
test_bla.py::test_foo[1] redPASSED
test_bla.py::test_foo[2] mauvePASSED
test_bla.py::test_foo[3] purplePASSED
====================== 8 passed in 0.03 seconds ======================
另外:昂贵的函数不会在导入时调用(查看 expensive
出现在输出中的位置)并且您可以在 colors