Pycharm/IntelliJ shows 0% coverage for pytest even though coverage was generated

Viewed 5607

I have a Python project and a tests task, set up to run pytest from the project's working directory.

enter image description here

Doing Run 'tests' with coverage from the Run menu successfully runs the tests, and the console results shows that coverage was measured - e.g. 53% cover for mws.py.

enter image description here

The automatically applied coverage (as on the right) is 0% for all files, I'm not sure why. I'm using IntelliJ 2017.2.2 EAP.

NB: there is a related five year old question here, but the top rated solution there doesn't apply. There is no error message in the results console in this case.

3 Answers

I had a similar issue, but the accepted solution here didn't solve it.

I had pytest automatically run coverage in its configuration file. In PyCharm, I added a Run Configuration to run all my tests with pytest. It seemed to work, and I saw all tests running and got their results to display in PyCharm's run window.

But soon I noticed there were two problems:

  1. When I selected "Run with coverage" I got an error like "coverage results not found", and all files showed 0% coverage.
  2. Breakpoints in tests were not hit when running test in Debug mode.

Both problems disappeared when I added --no-cov to the "Additional Arguments" passed to to pytest (this option is in the Run Configuration).

So It seems the fix was to tell pytest to not run coverage when running it from PyCharm. Both "Run with coverage" option and the Breakpoints in tests now work.

Related