question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Coverage for installed packages + smarter reports

See original GitHub issue

@schlamar I don’t know if this could be of any use to anyone and I didn’t open a pull request since it’d require small changes to both pytest-cov and cov-core repos. I found myself hacking around the coverage reporting behaviour manually all the time (like changing current folder to location of the package to avoid ugly paths) so I wondered if it could be done upstream.

If you run coverage on an installed package (e.g. via pytest --pyargs), or more generally on a package that’s not in your current folder, your coverage report would look super ugly like so:

Name                                                                 Stmts   Miss  Cover   Missing
------------------------------------------------------
/home/smirnov/anaconda/lib/python2.7/site-packages/arrow/__init__        4      0   100%
/home/smirnov/anaconda/lib/python2.7/site-packages/arrow/api            13      0   100%
/home/smirnov/anaconda/lib/python2.7/site-packages/arrow/arrow         295      0   100%
/home/smirnov/anaconda/lib/python2.7/site-packages/arrow/factory        58      0   100%
/home/smirnov/anaconda/lib/python2.7/site-packages/arrow/formatter      78      0   100%
/home/smirnov/anaconda/lib/python2.7/site-packages/arrow/locales       371      2    99%   1182, 1189
/home/smirnov/anaconda/lib/python2.7/site-packages/arrow/parser        172      0   100%
/home/smirnov/anaconda/lib/python2.7/site-packages/arrow/util            7      0   100%
------------------------------------------------------
TOTAL                      998      2    99%

The same will go to html/xml reports.

Wouldn’t it would be nice to have reports like this instead?

Name        Stmts   Miss  Cover   Missing
--------------------------------------------
__init__        4      0   100%
api            13      0   100%
arrow         295      0   100%
factory        58      0   100%
formatter      78      0   100%
locales       371      2    99%   1182, 1189
parser        172      0   100%
util            7      0   100%
--------------------------------------------
TOTAL                      998      2    99%

Would you consider adding something like a --compact-report option to pytest-cov which would then pass some flag to CovController.summary() so it can do something like this before generating reports:

prefix = os.path.commonprefix(cov.data.measured_files())
if prefix:
    cov.file_locator.relative_dir = prefix  # +9000 nice points for all reports

That’s pretty much the entire implementation 😃

I could open pull requests to cov-core and pytest-cov if you’d want.

Thanks.

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

8reactions
aldanorcommented, Feb 14, 2016

After all this time, I still couldn’t find a proper solution to this 😿

Running tests against a site-packages version of the package as opposed to the working source tree has many benefits, e.g. it may reveal problems in packaging. However it all ends up with ugly coverage and it’s unobvious how to fix it (unless I’m missing something).

1reaction
ionelmccommented, Feb 14, 2016

@aldanor you got two options:

  • Develop installs. But this means you need to run tests two times, with and without coverage (where you don’t do develop install).
  • Use coverage’s report cli. Basically you run coverage report to get a cleaner report. See https://hynek.me/articles/testing-packaging/
Read more comments on GitHub >

github_iconTop Results From Across the Web

Specifying source files — Coverage.py 6.5.0 documentation
Specifying source files¶. When coverage.py is running your program and measuring its execution, it needs to know what code to measure and what...
Read more >
Python Coverage reports 100% coverage on an untested ...
Test coverage. I decided I wanted to evaluate the coverage of my Django app, so I installed the coverage library and ran the...
Read more >
Code Coverage - Cypress Documentation
Once the tests finish, the coverage object can be serialized and saved to disk so that a human-friendly report can be generated. The...
Read more >
Analyzing coverage of R unit tests in packages - R-statistics blog
The reported coverage in this case would therefore be 67% even though the source code is (arguably) functionally almost identical. What should ...
Read more >
Review code coverage in your Unit test case in iOS - Medium
The smarter way is that External coverage report. You can also generate reports in XML or HTML formats. This is useful when you...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found