Dane Hillard
Dane Hillard

Reputation: 900

PyCharm code coverage for Django + django-nose

When trying to run tests with coverage under Django + django-nose in PyCharm, I can't seem to get the coverage report to work. I've tried with the bundled coverage.py and without. Without the bundled coverage.py I see:

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>

While with it I see:

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

My tests and coverage reporting work correctly via the command line, i.e.:

$ python manage.py test

I've set up a Run Configuration using the Django tests template and I'm not doing anything special or out of the ordinary, as far as I'm aware. Has anyone run into this and solved it? Coverage integration would be very useful!

Update

This issue has been opened with JetBrains here.

Upvotes: 2

Views: 892

Answers (1)

KFunk
KFunk

Reputation: 3152

I'm assuming that you have something similar to this in your config:

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

PyCharm's coverage blows up when this is configured. I assume that it's either running two instances of coverage, or the coverage output is something other than it expected to parse. Either way, it seems that you can't use it at the moment.

Solutions for now:

  • remove NOSE_ARGS config, and if any of your automated/continuous integration tools need it, run them with the coverage options on the command line: python manage.py test --with-coverage --cover-package=foo (I chose this option because of the benefits in PyCharm)
  • don't use coverage in PyCharm

Note: I was able to find someone else with this problem here.

Upvotes: 1

Related Questions