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的config对象获取缓存的值。以下是一个基本示例插件,它实现了一个funcarg,该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
有关pytest的更多信息:https://pytest.cn
项目详情
关闭
pytest-cache-1.0.tar.gz的哈希
| 算法 | 哈希摘要 | |
|---|---|---|
| SHA256 | be7468edd4d3d83f1e844959fd6e3fd28e77a481440a7118d430130ea31b07a9 |
|
| MD5 | e51ff62fec70a1fd456d975ce47977cd |
|
| BLAKE2b-256 | d115082fd0428aab33d2bafa014f3beb241830427ba803a8912a5aaeaf3a5663 |