Arm*_*ius 5 python pytest python-3.x
我想在 Python 中多次运行 pytest 而无需重新启动脚本/解释器。
问题是 pytest 正在缓存测试内容/结果。也就是说,如果您在两次运行之间修改测试文件,pytest 不会接受更改,并显示与之前相同的结果。(除非您重新启动脚本/退出解释器,当您从命令行使用 pytest 时,您自然会这样做。)
test_foo.py:
def test_me():
assert False
Run Code Online (Sandbox Code Playgroud)
在 Python shell 中:
>>> import pytest
>>> pytest.main(['test_foo.py'])
(...)
def test_me():
> assert False
E assert False
test_foo.py:2: AssertionError
Run Code Online (Sandbox Code Playgroud)
目前很好。现在不要退出解释器,而是将测试更改为assert True并重新运行 pytest。
>>> pytest.main(['test_foo.py'])
(...)
def test_me():
> assert True
E assert False
test_foo.py:2: AssertionError
Run Code Online (Sandbox Code Playgroud)
Pytest 应该已经发现文件中的更改并通过重写的测试。
用于importlib.reload(pytest)在运行之间重新加载 pytest。
运行 pytest 并清除缓存:pytest.main(['--cache-clear', test_foo.py'])
(不能选择将 pytest 作为子进程运行,因为我想从应用程序中引用 pytest 模块。)
有任何提示如何让 pytest 接受这些更改或如何正确重新加载模块吗?
| 归档时间: |
|
| 查看次数: |
2280 次 |
| 最近记录: |