Skip to content
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

Exclude code coverage based on operating system #1699

Open
ericzolf opened this issue Nov 12, 2023 · 3 comments
Open

Exclude code coverage based on operating system #1699

ericzolf opened this issue Nov 12, 2023 · 3 comments
Labels
enhancement New feature or request

Comments

@ericzolf
Copy link

Is your feature request related to a problem? Please describe.
I have code which is operating system dependent and hence can't be covered on a single operating system, which doesn't allow me to understand how well my code is covered on each single operating system.
That probably also encourages me to accept a lower than necessary code coverage as I can't anyway reach 100%, and don't know how much of the missing 14% are due to this issue.

Describe the solution you'd like
I'd like to have conditional # pragma: no cover, e.g. # pragma: no cover if os.name == "nt" or # pragma: cover if os.name == "nt" or # pragma: no cover if os.name != "nt" (the 2 last being equivalent). sys.platform or any equivalent approach resp. syntax would be fine.

Describe alternatives you've considered
I didn't find any alternative beside ignoring missing coverage "in my head".

Additional context
https://github.com/rdiff-backup/rdiff-backup/blob/master/tox.ini shows only 86% coverage among other, because src/rdiffbackup/meta/acl_win.py is only covered at 26% and src/rdiffbackup/utils/locking.py at 36%, both being very specific to Windows (and I've completely ignored the MacOS code at src/rdiffbackup/meta/stdattr.py).

I have in mind something like @unittest.skipUnless resp. @unittest.skipIf but it doesn't need to be as flexible/dynamic.

@ericzolf ericzolf added enhancement New feature or request needs triage labels Nov 12, 2023
@nedbat
Copy link
Owner

nedbat commented Nov 12, 2023

I won't be adding this as a built-in feature of coverage, but there are third-party tools that do it: https://github.com/asottile/covdefaults. You can also do this yourself in a tox.ini file or similar, by defining an environment variable based on the OS, then using that variable in your .coveragerc file to specify exclude_also patterns. (I thought I knew where an example was, but I can't find it now.)

@nedbat
Copy link
Owner

nedbat commented Nov 12, 2023

The other thing to remember is that you can combine coverage results from multiple runs. Test on Windows and Unix, then combine the results to get the total.

@freakboy3742
Copy link
Contributor

Requested by @nedbat on Mastodon: If you want an example of this in practice: Briefcase has an extensive set of Python version and OS conditional coverage definitions, with coverage merging in CI: https://github.com/beeware/briefcase/blob/main/pyproject.toml#L30

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants