-
-
Notifications
You must be signed in to change notification settings - Fork 424
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Don't measure coverage from certain lines #668
Comments
Hmm, this seems like the opposite of the nocover pragma: a line that is executed, but you don't want counted as covered. There is no way to do that now. I'm not sure how you would use it in your scenario, since running test_data.py should count it as covered. Perhaps if #170 (who tests what) is ever implemented, it will give you the information you need? Can you say more how you would use this in a real project? Are you trying to make the total percentage more accurate, or are you trying to make the red/green line markers more accurate? Or something else? |
Original comment by pckroon (Bitbucket: pckroon, GitHub: pckroon) Thanks for the lightning fast reply. And indeed, running test_data should count is as covered, but currently running test_x also covers it. What I want to use it for is to make sure the parser code is also actually tested and produces the expected output, so it's mostly about the percentages. |
How do you envision running your tests so that you can run test_data and get the lines counted, and also run other tests and not get the lines counted? |
Original comment by pckroon (Bitbucket: pckroon, GitHub: pckroon) I think (from coverage's perspective) I can make a file like such: background.py
and then do |
@pckroon The new context feature in 5.0a3 might be usable for this: https://nedbatchelder.com/blog/201810/who_tests_what_is_here.html |
Cheers. I glossed over it and it looks good. As it is now I see two ways of implementing what I need:
With the new feature, option 2 seems easier to implement. |
Definitely option 2 could be done now. If you use 5.0a3, you can delete the recorded data for the empty context, and then report on what is left. |
Then from a practical point of view: do I try to convince someone from pytest-cov to implement this, or write my own plugin for either coverage.py, pytest-cov or pytest. I guess for short term making my own plugin for coverage would be the quickest test/implementation. For long term adaptation I should make/revive an issue on pytest-cov. I'll have a look at that soon. |
What's the status here ? |
There's been no progress on this. Do you have a new scenario that could help us find a solution? |
With static contexts, you could run the test suite once with no tests and
|
In commit 6a1c275 I threw together a quick program to do this: select_contexts.py. I'm not sure it does what we need yet. Maybe one of these two scenarios is what you want... Try it out and let me know: Excluding code outside of any testIn your .coveragerc file, add:
This will record which test function was running for each data point. Run your test suite as usual. The code running outside the test functions will have an empty context recorded. Use select_context.py to subset the data file, then report the resulting data:
Excluding code run when no tests are run
|
@nedbat Your Script helped me out really well. Would be nice to be able to ignore those lines found all together and mark them as "not executable". In my Django Project I really want to ignore those unnecessary loading/declaration statements. Though I saw this data is not in the objects that are currently traversed by your script. |
Originally reported by pckroon (Bitbucket: pckroon, GitHub: pckroon)
Hi all,
first off, thanks for the great work you've been doing so far with this :)
This is a duplicate of the issue I filed on pytest-cov earlier (pytest-dev/pytest-cov#207).
I have a project that parses data files on import, which means that the parser code is always considered as covered, even though it is not actually tested. Is there any way to either record a "background" which is later substracted, or a way to specifically exclude coverage coming from certain lines (i.e. the import statements)?
Minimal example:
project/init.py:
project/data.py:
project/func.py:
tests/test_x.py
tests/test_data.py:
I expect project/data.py to be 0% covered, unless I also run test_data.py. I can't make that work with a .coveragerc or pragmas. Any advice is highly welcome :)
The text was updated successfully, but these errors were encountered: