Can I combine mypy reports with pytest coverage reports?
Asked Answered
M

1

6

I can generate a type "coverage" report with mypy via

mypy . --html-report mypy-report

and a pytest coverage report with pytest-cov

pytest --cov=app --cov-report=html

Is there a way to combine both reports? I would be interested in code that is neither covered by unit tests nor by mypy.

Monarchal answered 15/10, 2020 at 11:28 Comment(1)
Don't know of an existing solution, but are you locked into HTML? Using line coverage from mypy (--lineprecision-report) and pytest would make parsing the common coverage much easier.Oxime
C
1

I spent a little time on this, and made some progress, but don't have a fully working solution.

Both pytest and mypy have flags to emit an XML-based report, and the one common schema they share is for the Java-based Cobertura tool.

Example invocation to produce an XML file for each:

pytest --cov=app --cov-report=xml:pytest-cobertura.xml
mypy app.py --cobertura-xml-report .

After this, you'll have two similar XML files named:

  • pytest-cobertura.xml
  • coverage.xml - mypy will only allow a directory, not a filename

I've explored trying to merge these two files into a single one, but haven't been able to successfully do so in the time I've spent on this - but it should be something along the lines of parsing the XML tree and merging the two sets of lines XML tags and creating a combined.xml file.

Right now, each file individually can be rendered to either terminal or HTML with the pycobertura tool, like so:

pycobertura show --format html --output coverage.html combined.xml

I haven't been able to figure out the merging just yet, but wanted to give you what I had figured out.

Crouch answered 25/10, 2020 at 12:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.