具有跨测试运行缓存机制的pytest插件
pytest-cache的Python项目详细描述
用法
安装方式:
pip install pytest-cache
之后其他插件可以访问新的config.cache对象 这有助于在py.test调用之间共享值。
插件提供了两个重新运行失败的选项,即--lfto 仅重新运行失败和--ff以运行除失败之外的所有测试 从最后一次跑步开始。对于清理(通常不需要),a --clearcache选项允许删除所有跨会话缓存 测试运行前的内容。
只重新运行故障或先运行故障
首先,我们创建50个测试调用,其中只有2个失败:
# content of test_50.py import pytest @pytest.mark.parametrize("i", range(50)) def test_num(i): if i in (17,25): pytest.fail("bad luck")
如果您第一次运行此程序,您将看到两个失败:
$ py.test -q .................F.......F........................ =================================== FAILURES =================================== _________________________________ test_num[17] _________________________________ i = 17 @pytest.mark.parametrize("i", range(50)) def test_num(i): if i in (17,25): > pytest.fail("bad luck") E Failed: bad luck test_50.py:6: Failed _________________________________ test_num[25] _________________________________ i = 25 @pytest.mark.parametrize("i", range(50)) def test_num(i): if i in (17,25): > pytest.fail("bad luck") E Failed: bad luck test_50.py:6: Failed
如果用--lf运行它,那么只运行两个失败的测试 从上次运行开始:
$ py.test --lf ============================= test session starts ============================== platform linux2 -- Python 2.7.3 -- pytest-2.3.5 run-last-failure: rerun last 2 failures plugins: cache collected 50 items test_50.py FF =================================== FAILURES =================================== _________________________________ test_num[17] _________________________________ i = 17 @pytest.mark.parametrize("i", range(50)) def test_num(i): if i in (17,25): > pytest.fail("bad luck") E Failed: bad luck test_50.py:6: Failed _________________________________ test_num[25] _________________________________ i = 25 @pytest.mark.parametrize("i", range(50)) def test_num(i): if i in (17,25): > pytest.fail("bad luck") E Failed: bad luck test_50.py:6: Failed =================== 2 failed, 48 deselected in 0.02 seconds ====================
最后一行表示48个测试尚未运行。
如果使用--ff选项运行,则所有测试都将运行,但第一个测试除外 将首先执行失败(从FF和 点):
$ py.test --ff ============================= test session starts ============================== platform linux2 -- Python 2.7.3 -- pytest-2.3.5 run-last-failure: rerun last 2 failures first plugins: cache collected 50 items test_50.py FF................................................ =================================== FAILURES =================================== _________________________________ test_num[17] _________________________________ i = 17 @pytest.mark.parametrize("i", range(50)) def test_num(i): if i in (17,25): > pytest.fail("bad luck") E Failed: bad luck test_50.py:6: Failed _________________________________ test_num[25] _________________________________ i = 25 @pytest.mark.parametrize("i", range(50)) def test_num(i): if i in (17,25): > pytest.fail("bad luck") E Failed: bad luck test_50.py:6: Failed ===================== 2 failed, 48 passed in 0.07 seconds ======================
新的config.cache对象
插件或conftest.py支持代码可以获取缓存值 使用pytestconfig对象。下面是一个基本的例子 实现funcarg的插件 在py.test调用中重复使用以前创建的状态:
# content of test_caching.py import time def pytest_funcarg__mydata(request): val = request.config.cache.get("example/value", None) if val is None: time.sleep(9*0.6) # expensive computation :) val = 42 request.config.cache.set("example/value", val) return val def test_function(mydata): assert mydata == 23
如果您运行此命令一次,将需要一段时间,因为 睡眠时间:
$ py.test -q F =================================== FAILURES =================================== ________________________________ test_function _________________________________ mydata = 42 def test_function(mydata): > assert mydata == 23 E assert 42 == 23 test_caching.py:12: AssertionError
如果您再次运行它,将从 缓存和这将很快:
$ py.test -q F =================================== FAILURES =================================== ________________________________ test_function _________________________________ mydata = 42 def test_function(mydata): > assert mydata == 23 E assert 42 == 23 test_caching.py:12: AssertionError
咨询pytest-cache API 更多细节。
检查缓存内容
您始终可以使用 --cache命令行选项:
$ py.test --cache ============================= test session starts ============================== platform linux2 -- Python 2.7.3 -- pytest-2.3.5 plugins: cache cachedir: /tmp/doc-exec-6/.cache --------------------------------- cache values --------------------------------- example/value contains: 42 cache/lastfailed contains: set(['test_caching.py::test_function']) =============================== in 0.01 seconds ===============================
清除缓存内容
您可以指示pytest清除所有缓存文件和值 通过这样添加--clearcache选项:
py.test --clearcache
对于来自连续集成的调用,建议这样做 隔离和正确性更重要的服务器 比速度还要快。
注释
存储库:http://bitbucket.org/hpk42/pytest-cache
问题:存储库:http://bitbucket.org/hpk42/pytest-cache/issues
关于py.test的更多信息:http://pytest.org