Django + django-nose的PyCharm代码覆盖率

时间:2016-05-14 13:47:50

标签: python django pycharm coverage.py

当试图在PyCharm中使用Django + django-nose进行覆盖测试时,我似乎无法使覆盖率报告正常工作。我已经尝试使用捆绑的coverage.py而没有。没有捆绑的coverage.py,我看到了:

Traceback (most recent call last):
  File "/Applications/PyCharm.app/Contents/helpers/coverage_runner/run_coverage.py", line 44, in <module>
    main()
  File "/Users/dane/.virtualenvs/dhp/lib/python3.5/site-packages/coverage/cmdline.py", line 741, in main
    status = CoverageScript().command_line(argv)
  File "/Users/dane/.virtualenvs/dhp/lib/python3.5/site-packages/coverage/cmdline.py", line 481, in command_line
    return self.do_run(options, args)
  File "/Users/dane/.virtualenvs/dhp/lib/python3.5/site-packages/coverage/cmdline.py", line 625, in do_run
    self.coverage.stop()
  File "/Users/dane/.virtualenvs/dhp/lib/python3.5/site-packages/coverage/control.py", line 692, in stop
    self.collector.stop()
  File "/Users/dane/.virtualenvs/dhp/lib/python3.5/site-packages/coverage/collector.py", line 277, in stop
    "Expected current collector to be %r, but it's %r" % (self, self._collectors[-1])
AssertionError: Expected current collector to be <Collector at 0x10e0cfbe0: CTracer>, but it's <Collector at 0x10fa368d0: CTracer>

有了它,我看到了:

Traceback (most recent call last):
  File "/Applications/PyCharm.app/Contents/helpers/coverage_runner/run_coverage.py", line 44, in <module>
    main()
  File "/Applications/PyCharm.app/Contents/helpers/coveragepy/coverage/cmdline.py", line 721, in main
    status = CoverageScript().command_line(argv)
  File "/Applications/PyCharm.app/Contents/helpers/coveragepy/coverage/cmdline.py", line 438, in command_line
    self.do_execute(options, args)
  File "/Applications/PyCharm.app/Contents/helpers/coveragepy/coverage/cmdline.py", line 580, in do_execute
    self.coverage.stop()
  File "/Applications/PyCharm.app/Contents/helpers/coveragepy/coverage/control.py", line 410, in stop
    self.collector.stop()
  File "/Applications/PyCharm.app/Contents/helpers/coveragepy/coverage/collector.py", line 294, in stop
    assert self._collectors[-1] is self
AssertionError

我的测试和覆盖率报告通过命令行正常工作,即:

$ python manage.py test

我已经使用Django测试模板设置了一个运行配置,并且我没有做任何特别的或与众不同的事情,据我所知。有没有人碰到这个并解决了它?覆盖整合非常有用!

更新

此问题已在JetBrains here打开。

1 个答案:

答案 0 :(得分:0)

我假设您在配置中有类似的内容:

NOSE_ARGS = [
    '--with-coverage',
    '--cover-package=foo,bar'
]

PyCharm的覆盖范围在配置时会爆炸。我假设它要么运行两个覆盖实例,要么覆盖输出不是预期要解析的。无论哪种方式,目前似乎都无法使用它。

现在的解决方案:

  • 删除NOSE_ARGS配置,如果您的任何自动/持续集成工具需要它,请使用命令行中的coverage选项运行它们:python manage.py test --with-coverage --cover-package=foo(我选择此选项是因为PyCharm)
  • 不要在PyCharm中使用覆盖

注意:我能够找到遇到此问题的其他人here