25

How do I debug py.test in PyCharm when coverage is enabled?

Coverage is enabled using --cov=project --cov-report=term-missing, removing this and breakpoints are hit.

Versions: pycharm 5.0.3, pytest==2.8.5, pytest-cache==1.0, pytest-cov==2.2.0, pytest-pep8==1.0.6, pytest-xdist==1.13.1, python-coveralls==2.6.0.

(thanks for jon's advice on further diagnosing the issue)

simonzack
  • 19,729
  • 13
  • 73
  • 118
  • Could you give a bit more information about how you've set this up? I use PyCharm with py.test with absolutely no problems; running in debug mode gives the expected behaviour. Which versions, what configuration, how are you executing, etc. Also a [mcve] of the test and actual code might help. – jonrsharpe Jan 19 '16 at 07:37
  • @jonrsharpe Apparently the coverage plugin is causing issues, but it does usually work for me for non-py.test setups when using pycharm's default coverage? I'd give more info but I think this should now be reproducible. – simonzack Jan 19 '16 at 07:43
  • I generally let PyCharm do the coverage, so it can do the fancy GUI stuff (line-by-line highlights, percentage by directory and file); run in debug mode for debugging (second run button or Ctrl-D) and coverage mode (third run button) when I want to run the suite for coverage. You could also set up separate configurations with and without coverage if you wanted. – jonrsharpe Jan 19 '16 at 07:54
  • This is a bug in PyCharm https://youtrack.jetbrains.com/issue/PY-20186 https://github.com/pytest-dev/pytest-cov/issues/131 – Morgan Thrapp Sep 02 '16 at 14:46

1 Answers1

27

There is now a flag in py.test to disable coverage which you can activate when running tests from PyCharm.

The flag to use is --no-cov. If you want this to apply to all your test runs you can add this to the default pytest configuration as below: Pycharm pytest debug

Extra tip: You probably also want a -s flag in there so output isn't swallowed by py.test. See https://stackoverflow.com/a/17810324/238166 for details.

In case you receive an "unrecognized argument" error, you may need to install pytest-cov, e.g. by pip install pytest-cov.

n1000
  • 5,058
  • 10
  • 37
  • 65
Inti
  • 3,443
  • 1
  • 29
  • 34
  • 1
    This is only available in the professional version (see https://www.jetbrains.com/help/pycharm/product-tests.html) and can be found in the Run/Debug configurations (when you want to run your project) – Jan Benes Mar 26 '19 at 11:46
  • 3
    You need to `pip install pytest-cov` first in order to pass in coverage-options! Please consider updating your response for this @inti – Mansweet Jun 04 '19 at 00:41