PytestWarning:模块已导入,因此无法重写:pytest_remotedata
PytestWarning: Module already imported so cannot be rewritten: pytest_remotedata
我创建了一些单元测试,运行 它们来自同一个文件。对于同一文件中的测试:
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
也不足以修复它。
看来 pytest 并不是真正设计用于从同一进程中重复执行 pytest.main()
调用。 pytest documentation 提到:
Calling pytest.main() will result in importing your tests and any
modules that they import. Due to the caching mechanism of python’s
import system, making subsequent calls to pytest.main() from the same
process will not reflect changes to those files between the calls. For
this reason, making multiple calls to pytest.main() from the same
process (in order to re-run tests, for example) is not recommended.
所以实际上,在同一进程中执行多个 pytest.main()
调用的真正危险在于,如果您在其间编辑了代码文件,则可能会得到假阳性或假阴性测试结果。
Spyder IDE has a nifty feature though that seems to negate this issue: the User Module Reloader (UMR)。如果启用,则当脚本文件 (re-)运行 时,任何更改的用户模块都会自动重新加载。
因此我认为只要您在 Spyder 中工作(启用 UMR 功能!),您就可以安全地重新运行 pytest.main()
而无需新的控制台。 pytest 模块已经导入的警告你可以简单地抑制,因为那些 pytest 模块不会改变。这可以通过 pytest 的 -W
flag 来完成。示例:
if __name__ == '__main__':
import pytest
pytest.main(['./test_stuff.py', "--capture=sys", "-W", "ignore:Module already imported:pytest.PytestWarning"])
如果您只关心警告,可以使用 --disable-warnings
参数。您还可以使用 pytest.main 中的 --pythonwarnings=PYTHONWARNINGS
参数仅过滤掉您现在收到的警告。 pytest --help
有关于这些参数的更多信息。
看来你是在用 ipython 或 jupyter 调用 pytest。 python.main 在 pytest 初始化时导入 pytest 特定模块。当您再次 运行 pytest.main 时,它会抛出警告。有两种可能的方法来重新加载 pytest 以避免收到此初始化警告。
您可以在 pytest.main 之后使用 pytest.exit after pytest.main or reload pytest 。
让我们知道这些方法是否有效。
使用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='')
我创建了一些单元测试,运行 它们来自同一个文件。对于同一文件中的测试:
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
也不足以修复它。
看来 pytest 并不是真正设计用于从同一进程中重复执行 pytest.main()
调用。 pytest documentation 提到:
Calling pytest.main() will result in importing your tests and any modules that they import. Due to the caching mechanism of python’s import system, making subsequent calls to pytest.main() from the same process will not reflect changes to those files between the calls. For this reason, making multiple calls to pytest.main() from the same process (in order to re-run tests, for example) is not recommended.
所以实际上,在同一进程中执行多个 pytest.main()
调用的真正危险在于,如果您在其间编辑了代码文件,则可能会得到假阳性或假阴性测试结果。
Spyder IDE has a nifty feature though that seems to negate this issue: the User Module Reloader (UMR)。如果启用,则当脚本文件 (re-)运行 时,任何更改的用户模块都会自动重新加载。
因此我认为只要您在 Spyder 中工作(启用 UMR 功能!),您就可以安全地重新运行 pytest.main()
而无需新的控制台。 pytest 模块已经导入的警告你可以简单地抑制,因为那些 pytest 模块不会改变。这可以通过 pytest 的 -W
flag 来完成。示例:
if __name__ == '__main__':
import pytest
pytest.main(['./test_stuff.py', "--capture=sys", "-W", "ignore:Module already imported:pytest.PytestWarning"])
如果您只关心警告,可以使用 --disable-warnings
参数。您还可以使用 pytest.main 中的 --pythonwarnings=PYTHONWARNINGS
参数仅过滤掉您现在收到的警告。 pytest --help
有关于这些参数的更多信息。
看来你是在用 ipython 或 jupyter 调用 pytest。 python.main 在 pytest 初始化时导入 pytest 特定模块。当您再次 运行 pytest.main 时,它会抛出警告。有两种可能的方法来重新加载 pytest 以避免收到此初始化警告。
您可以在 pytest.main 之后使用 pytest.exit after pytest.main or reload pytest 。
让我们知道这些方法是否有效。
使用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='')