gpt4 book ai didi

python - Pytest 警告 : Module already imported so cannot be rewritten: pytest_remotedata

转载 作者:太空宇宙 更新时间:2023-11-03 13:57:10 25 4
gpt4 key购买 nike

我创建了一些单元测试并从同一个文件运行它们。对于同一文件中的测试:

if __name__ == "__main__":
import pytest
pytest.main(['--tb=short', __file__])

对于另一个文件中的测试:

if __name__ == '__main__':
import pytest
pytest.main(['./test_stuff.py', "--capture=sys"])

在任何一种情况下,当我第一次执行该文件时,它工作正常,但第二次和随后的时间它给出了一堆警告:

============================== warnings summary ===============================
C:\Anaconda3\lib\site-packages\_pytest\config\__init__.py:754
C:\Anaconda3\lib\site-packages\_pytest\config\__init__.py:754: PytestWarning: Module already imported so cannot be rewritten: pytest_remotedata
self._mark_plugins_for_rewrite(hook)
C:\Anaconda3\lib\site-packages\_pytest\config\__init__.py:754: PytestWarning: Module already imported so cannot be rewritten: pytest_openfiles
self._mark_plugins_for_rewrite(hook)
C:\Anaconda3\lib\site-packages\_pytest\config\__init__.py:754: PytestWarning: Module already imported so cannot be rewritten: pytest_doctestplus
self._mark_plugins_for_rewrite(hook)
C:\Anaconda3\lib\site-packages\_pytest\config\__init__.py:754: PytestWarning: Module already imported so cannot be rewritten: pytest_arraydiff
self._mark_plugins_for_rewrite(hook)

-- Docs: https://docs.pytest.org/en/latest/warnings.html
==================== 1 passed, 4 warnings in 0.06 seconds

有什么方法可以让这些警告消失吗?

重新启动内核可以,但 IPython 的 %reset%clear 也不足以修复它。

最佳答案

使用subprocess代替pytest.main:

if __name__ == "__main__":
import subprocess
subprocess.call(['pytest', '--tb=short', str(__file__)])

如果以上没有打印任何内容,请尝试解决方法(如评论中所建议):

if __name__ == "__main__":
from subprocess import Popen, PIPE
with Popen(['pytest',
'--tb=short', # shorter traceback format
str(__file__)], stdout=PIPE, bufsize=1,
universal_newlines=True) as p:
for line in p.stdout:
print(line, end='')

关于python - Pytest 警告 : Module already imported so cannot be rewritten: pytest_remotedata,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54009371/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com