Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 369
James Hiew Avatar asked Aug 09 '17 17:08

James Hiew


2 Answers

I think the problem lies in you use pytest-cov, so Pycharm cannot parse the result which is shown in text like 53% generated by pytest-cov;

So Changes option in pytest.ini to addopts = -s -v when you want to use Pycharm built in coverage tools.

like image 143
aristotll Avatar answered Nov 12 '22 06:11

aristotll


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.

like image 21
avivr Avatar answered Nov 12 '22 08:11

avivr