Py.test收集阶段耗时很长

时间:2014-08-15 15:11:37

标签: python ubuntu pycharm pytest

我对Python的开发一般都很陌生,更不用说用pytest进行测试了。我的问题是pytest收集阶段运行异常缓慢。我指定的测试目录只包含少量文件,只有一个文件包含三个测试。该集合需要花费大约一分钟,之后实际测试将在几秒钟内完成。我看过类似的问题,但无法找到解决方案。我不认为这很重要(因为即使从命令行py.test也很慢),但我使用的是pycharm IDE。操作系统是Ubuntu。

这可能是相关的:如果我在几秒钟后终止该过程,我通常最终得到一个堆栈跟踪结束如下:

<A FEW LINES OMITTED...>
File "/usr/local/lib/python2.7/dist-packages/_pytest/core.py", line 413, in __call__
    return self._docall(methods, kwargs)
  File "/usr/local/lib/python2.7/dist-packages/_pytest/core.py", line 424, in _docall
    res = mc.execute()
  File "/usr/local/lib/python2.7/dist-packages/_pytest/core.py", line 315, in execute
    res = method(**kwargs)
  File "/usr/local/lib/python2.7/dist-packages/_pytest/helpconfig.py", line 27, in pytest_cmdline_parse
    config = __multicall__.execute()
  File "/usr/local/lib/python2.7/dist-packages/_pytest/core.py", line 315, in execute
    res = method(**kwargs)
  File "/usr/local/lib/python2.7/dist-packages/_pytest/config.py", line 636, in pytest_cmdline_parse
    self.parse(args)
  File "/usr/local/lib/python2.7/dist-packages/_pytest/config.py", line 747, in parse
    self._preparse(args)
  File "/usr/local/lib/python2.7/dist-packages/_pytest/config.py", line 709, in _preparse
    self._initini(args)
  File "/usr/local/lib/python2.7/dist-packages/_pytest/config.py", line 704, in _initini
    self.inicfg = getcfg(args, ["pytest.ini", "tox.ini", "setup.cfg"])
  File "/usr/local/lib/python2.7/dist-packages/_pytest/config.py", line 861, in getcfg
    if exists(p):
  File "/usr/local/lib/python2.7/dist-packages/_pytest/config.py", line 848, in exists
    return path.check()
  File "/usr/local/lib/python2.7/dist-packages/py/_path/local.py", line 352, in check
    return exists(self.strpath)
  File "/usr/lib/python2.7/genericpath.py", line 18, in exists
    os.stat(path)
KeyboardInterrupt

或者有时......

<STACK TRACE...>
  File "/usr/local/lib/python2.7/dist-packages/py/_iniconfig.py", line 50, in __init__
    f = open(self.path)
KeyboardInterrupt

也许在KeyboardInterrupt之前的两个最后一次调用之一非常慢?

如果您需要,请提出更多详细信息!

干杯!

2 个答案:

答案 0 :(得分:1)

我有同样的问题。我的解决方法是将Working directory中的Run/Debug Configuration设置为manage.py所在的文件夹。

答案 1 :(得分:0)

PYTHONDONTWRITEBYTECODE=1添加到您的环境变量中!

  • Windows批处理:set PYTHONDONTWRITEBYTECODE=1
  • Unix:export PYTHONDONTWRITEBYTECODE=1
  • subprocess.run:添加关键字env={'PYTHONDONTWRITEBYTECODE': '1'}

请注意,前两个选项仅对您当前的终端会话有效。


这是我发现的方式:pytest 在命令行上运行速度异常缓慢,但在PyCharm内部运行正常。将PyCharm命令复制到cmd.exe(执行一个小的帮助程序脚本)也非常缓慢。因此,我在os.environ上打印了环境变量,并以此进行了尝试-速度很快!然后我一一淘汰。