现在我有一个测试文件,看起来像这个人为的例子:
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()
以生成多个函数。
我应该编写这些测试的最佳方式是什么?
一种解决方案是在单独的函数中生成颜色,并将其结果用作灯具的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
中使用其他会话夹具
语法错误可以按如下方式解决:
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
正如您在输出中看到的,为每个测试用例设置/拆卸颜色夹具,为最终测试用例再次设置/拆卸颜色夹具。
为了避免这种情况,您可以按如下方式对代码进行最低限度的重构:
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
您可以在其中检查夹具是否对所有测试仅设置/拆卸一次。
您可以查看pytest文档中的相关部分