编程语言
首页 > 编程语言> > python-PytestWarning:模块已导入,因此无法重写:pytest_remotedata

python-PytestWarning:模块已导入,因此无法重写: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.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-import,pytest,python
来源: https://codeday.me/bug/20191024/1923688.html