Skip to main content

具有跨测试运行缓存机制的 pytest 插件

项目描述

用法

通过安装:

pip install pytest-cache

之后其他插件可以访问一个新的config.cache对象,该对象有助于在py.test调用之间共享值。

该插件提供了两个选项来重新运行失败,即--lf仅重新运行失败和--ff运行所有测试,但首先运行上次运行的失败。对于清理(通常不需要), --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 支持代码可以使用 pytest配置对象获取缓存值。这是一个基本示例插件,它实现了一个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 ===============================

清除缓存内容

您可以通过添加--clearcache选项来指示 pytest 清除所有缓存文件和值,如下所示:

py.test --clearcache

对于从隔离和正确性比速度更重要的持续集成服务器的调用,建议这样做。

笔记

存储库:http ://bitbucket.org/hpk42/pytest-cache

问题:存储库:http ://bitbucket.org/hpk42/pytest-cache/issues

有关 py.test 的更多信息: http ://pytest.org

项目详情


下载文件

下载适用于您平台的文件。如果您不确定要选择哪个,请了解有关安装包的更多信息。

源分布

pytest-cache-1.0.tar.gz (16.2 kB 查看哈希)

已上传 source