How can I import code coverage in Pycharm executed on a different machine?

Viewed 428

I posted this already in the PyCharm forums but didn't get any response after a few days. So I figured I'd ask it here.

My goal is to import coverage.py results that run in a non PyCharm process into PyCharm in order to use all of the coverage editor features that come with PyCharm.

I'm using PyCharm Professional Edition so I do have access to the coverage feature.

I have python code that cannot be run directly in PyCharm since it needs to be run from within a database server process (PL/Python in Postgres if anyone is curious).

I'm using the coverage package within the database process to produce a json report but I can also export the format in any way that coverage.py supports.

According to Pycharm's documentation of Managing сode сoverage suites there should be a way to

"Upload... a coverage suite."

The directions say in order to do that you can

"Click the Add button and select the necessary .es file in the dialog that opens."

What type of format is the .es file? Can coverage.py export into that format directly? Or does that have something to do with "build servers"? I couldn't find anything on google related to .es files + coverage.py and the only relevant result from searching PyCharm documentation for .es is the "Managing code coverage suites" page itself.

I tried importing the json report and the xml report produced by coverage.py but PyCharm wouldn't allow me to select it (or even show it and the "Add Suite" dialogue box).

What else can I try in order to import my coverage.py results into PyCharm?

Thanks for any help!

1 Answers

I've been stuck with a similar issue for the past couple of hours, and finally got it working just now.

In my case, I was trying to get code coverage reports generated inside a docker container to be displayed on my PyCharm editor. Personally I was using pytest & pytest-cov to generate the report, which behind the scenes uses coverage.py, just like PyCharm does.

I faced the same issue to begin with; importing the produced coverage.xml would simply display everything as 0% covered. After managing to produce a working coverage report by running the project via PyCharm's integrated test runner tools, I was able to track down the difference between the two generated code coverage reports.

It turns out, you need TWO FILES for the code coverage report: The SQLite database generated by coverage.py which contains all the coverage information, as well as the XML formatted coverage report. These both also need to have the same filename, care for the extension.

Example working files

  • coverage.coverage
  • coverage.xml

Obviously, coverage.xml should contain the same coverage data as the sqlite db. You will have to open the SQLite database with pycharm, not the XML report. If you open the XML report, it will not work. If you don't have the XML report next to the SQLite database, you will get an error.

My test command:

pytest . --cov --cov-report xml

My .coveragerc:

[run]
branch = True
data_file = coverage.coverage

With the above pytest configuration, you should get coverage.coverage and coverage.xml as output files. Selecting the coverage.coverage file in the Run > Show Coverage Data dialog should get you a working coverage output.

Note that the --cov and --cov-report options are from the pytest-cov python package

Tested with the following PyCharm version:

PyCharm 2021.1.2 (Professional Edition)
Build #PY-211.7442.45, built on June 1, 2021
Runtime version: 11.0.11+9-b1341.57 amd64
VM: Dynamic Code Evolution 64-Bit Server VM by JetBrains s.r.o.
Windows 10 10.0
GC: ParNew, ConcurrentMarkSweep
Memory: 1979M
Cores: 24
Registry: external.system.auto.import.disabled=true
Non-Bundled Plugins: com.andrey4623.rainbowcsv (2.0), net.seesharpsoft.intellij.plugins.csv (2.17.0), org.jetbrains.plugins.go-template (211.6693.44), name.kropp.intellij.makefile (211.6693.108), com.intellij.kubernetes (211.7442.27), org.toml.lang (0.2.149.3939-211), com.deadlock.scsyntax (211.6693.111), intellij.prettierJS (211.7142.13), org.jetbrains.plugins.vue (211.7142.13), com.intellij.lang.jsgraphql (2.9.1), com.koxudaxi.pydantic (0.3.3), com.leinardi.pycharm.mypy (0.11.2), org.rust.lang (0.4.149.3939-211), org.intellij.plugins.hcl (0.7.10)
Related