为什么py.test --last-failed会跳过很多测试,而以前没有测试失败过?

时间:2019-06-13 09:07:28

标签: python pytest

尽管在先前的测试运行中 no 测试失败,但

突然py.test --last-failed跳过了很多测试。这是第一次运行py.test --vv然后运行py.test --vv --last-failed的输出:

$venv/bin/py.test -vv
======================================================================= test session starts =======================================================================
platform darwin -- Python 3.7.3, pytest-4.6.3, py-1.8.0, pluggy-0.12.0 -- /Users/lars/coding/talea/fahrtwind/products/backend/venv/bin/python3
cachedir: .pytest_cache
rootdir: /Users/lars/coding/talea/fahrtwind/products/backend, inifile: pytest.ini, testpaths: tests
plugins: forked-1.0.2, env-0.6.2, xdist-1.28.0, cov-2.7.1
collected 888 items
...                                                                                                                                     
<HERE ALL 888 TESTS ARE RUN AND ALL PASS>
...
=================================================================== 888 passed in 63.89 seconds ===================================================================
$
$
$venv/bin/py.test -vv --last-failed
======================================================================= test session starts =======================================================================
platform darwin -- Python 3.7.3, pytest-4.6.3, py-1.8.0, pluggy-0.12.0 -- /Users/lars/coding/talea/fahrtwind/products/backend/venv/bin/python3
cachedir: .pytest_cache
rootdir: /Users/lars/coding/talea/fahrtwind/products/backend, inifile: pytest.ini, testpaths: tests
plugins: forked-1.0.2, env-0.6.2, xdist-1.28.0, cov-2.7.1
collected 303 items                                                                                                                                               
run-last-failure: 181 known failures not in selected tests (skipped 206 files)
...
<HERE ONLY 303 TESTS ARE RUN>
...

为什么py.test --last-failed会跳过很多测试,而以前没有测试失败过?

1 个答案:

答案 0 :(得分:1)

如评论中所建议,使缓存无效是解决方案:要么通过重新运行测试,要么通过

$ pytest --cache-clear

,或者,如果测试套件需要大量时间来重新运行,则只需删除项目根目录中的.pytest_cache目录(对于旧版本的.cache,则删除pytest)。另外,引用Clearing Cache content部分:

  

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

pytest --cache-clear
     

建议从Continuous Integration服务器进行调用,因为隔离和正确性比速度更重要。

因此,最好的方法是指示CI服务器在每次运行时均使缓存无效,以便在遇到由于本地缓存而导致的意外失败时获得参考。