繁体   English   中英

在更改的测试文件上重复运行 pytest

[英]Running pytest repeatedly on changed test files

我想在 Python 中多次运行 pytest 而无需重新启动脚本/解释器。

问题是 pytest 正在缓存测试内容/结果。 也就是说,如果您在两次运行之间修改测试文件,pytest 不会获取更改,显示与以前相同的结果。 (除非您重新启动脚本/退出解释器,从命令行使用 pytest 时您自然会这样做。)

再生产

test_foo.py

def test_me():
    assert False

在 Python 外壳中:

>>> import pytest
>>> pytest.main(['test_foo.py'])
(...)
    def test_me():
>       assert False
E       assert False

test_foo.py:2: AssertionError

目前很好。 现在不要退出解释器,而是将测试更改为assert True并重新运行 pytest。

>>> pytest.main(['test_foo.py'])
(...)
    def test_me():
>       assert True
E       assert False

test_foo.py:2: AssertionError

预期结果

Pytest 应该已经获取了文件中的更改并通过了重写的测试。

不起作用的解决方案

  • 使用importlib.reload(pytest)在运行之间重新加载 pytest。

  • 运行 pytest 并清除缓存: pytest.main(['--cache-clear', test_foo.py'])

(将 pytest 作为子进程运行不是一个选项,因为我想在我的应用程序中引用 pytest 模块。)

任何提示如何使 pytest 获取这些更改或如何正确重新加载模块?

任何登陆这里的人,另一个可能有帮助的答案,只需安装pytest-xdist插件,然后调用pytest --looponfail

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM