python – pytest参数化会话fixtures执行太多次

请考虑以下测试代码,该代码将模拟运行结果与预期结果进行比较.运行结果的值取决于参数化夹具paramfixture的值,该值提供两个值,因此运行结果有两种可能的变体.由于它们都是会话装置,我们应该期望run_result装置只执行两次.

现在,请查看测试用例test_run_result,它接收run_result和expected_result灯具进行比较,并接收公差夹具,该夹具使用两个值进行参数化.测试用例检查预期和结果之间的差异是否在容差范围内.请注意,运行不依赖于公差.

出于某种原因,我不明白Pytest执行run_result()夹具三次.你能解释一下原因吗?

这是使用pytest vers测试的. 2.9.1

顺便说一下,如果测试用例没有参数化或者使用decoractor而不是fixture进行参数化,run_result夹具只会执行两次,即:@ pytest.mark.parametrize(‘tolerance’,[1e-8,1e- 11]).

import pytest

runcounter = 0

@pytest.fixture(scope="session", params=[1e-8, 1e-11])
def tolerance(request):
    """Precision in floating point compare."""
    return request.param

@pytest.fixture(scope='session', params=[1, 2])
def paramfixture(request):
    return request.param

@pytest.fixture(scope="session")
def expected_result(paramfixture):
    return 1 + paramfixture

@pytest.fixture(scope='session')
def run_result(paramfixture):
    global runcounter
    runcounter = runcounter + 1
    print "Run #", runcounter, 'param:', paramfixture
    return 1 + paramfixture

def test_run_result(run_result, expected_result, tolerance):
    print "run_result: %d, expected_result: %d" % (run_result, expected_result)
    assert abs(run_result - expected_result) < tolerance

Pytest截图:

$py.test -vs test/end2end/test_temp.py
===================================================== test session starts ======================================================
platform linux2 -- Python 2.7.11, pytest-2.9.1, py-1.4.31, pluggy-0.3.1 -- /home/f557010/.conda/envs/sfpdev/bin/python
cachedir: .cache
rootdir: /home/f557010/svndev/SFP, inifile: pytest.ini
collected 4 items

test/end2end/test_temp.py::test_run_result[1e-08-1] Run # 1 param: 1
run_result: 2, expected_result: 2
PASSED
test/end2end/test_temp.py::test_run_result[1e-08-2] Run # 2 param: 2
run_result: 3, expected_result: 3
PASSED
test/end2end/test_temp.py::test_run_result[1e-11-2] 
run_result: 3, expected_result: 3
PASSED
test/end2end/test_temp.py::test_run_result[1e-11-1] Run # 3 param: 1
run_result: 2, expected_result: 2
PASSED

=================================================== 4 passed in 0.01 seconds ===================================================

最佳答案 pytest的参数化就是获取一个夹具并在合理的生命周期内保持它.它不会缓存所有输入>输出映射.这不是你在这里所说的,但是如果你认为fixtures是数据库连接或tcp连接(比如示例中的smtp),那就有意义了.

你仍然有一个不错的论据,在pytest的部分进行足够的内省和优化会使你受益(假设run_result非常昂贵并且你希望最小化运行).

为什么这里做“错误的事”?如果仔细观察夹具,公差是“第一顺序”或最接近的参数化夹具.

一个丑陋,不可思议的变化“有效”:

@pytest.fixture(scope="session", params=[0.01, 0.0002])
def tol(request):
    """Precision in floating point compare."""
    return request.param

@pytest.fixture(scope="session")
def tolerance(tol):
    """Precision in floating point compare."""
    return tol

为什么这个世界会起作用?它将公差参数移除到与其他灯具上的参数相同的“级别”.有了这个,pytest实际上只运行run_tests两次.

============================================ test session starts ============================================
<snip>
collected 4 items

test_tolerance.py::test_run_result[1-0.01] Run # 1 param: 1
run_result: 2, expected_result: 2 tolerance: 0.010000
PASSED
test_tolerance.py::test_run_result[1-0.0002]
run_result: 2, expected_result: 2 tolerance: 0.000200
PASSED
test_tolerance.py::test_run_result[2-0.0002] Run # 2 param: 2
run_result: 3, expected_result: 3 tolerance: 0.000200
PASSED
test_tolerance.py::test_run_result[2-0.01]
run_result: 3, expected_result: 3 tolerance: 0.010000
PASSED

========================================= 4 passed in 0.01 seconds ==========================================

你应该使用该代码吗?请尽量不要因为它太难以理解,如果你使用这样的黑客,请大声评论’自己sp.

你问“为什么”,这里的关键是容忍和paramfixture的参数处于不同的嵌套级别,“最接近的”是迭代最慢的一个.夹具不在这里缓存,它们只是按逻辑顺序使用,最里面的迭代最快.

点赞