Skip to content

How to programmatically disable coverage? #418

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

Open
jaraco opened this issue Jul 2, 2020 · 4 comments
Open

How to programmatically disable coverage? #418

jaraco opened this issue Jul 2, 2020 · 4 comments

Comments

@jaraco
Copy link

jaraco commented Jul 2, 2020

I'd like to be able to programmatically disable coverage tests based on some runtime condition. In particular, on Setuptools, when running tests against PyPy, enabling coverage is prohibitively expensive (~3x run time). I'd like to be able to enable coverage tests by default, but when PyPy is detected (probably in conftest.py), disable the coverage.

I'd like to enable coverage by default by setting [pytest] addopts=--cov, but have that not take effect when running on PyPy. I've tried using tox functionality to secondarily disable by passing --no-cov, but even tox can't detect when PyPy is running; it can only detect when an environment named pypy or pypy3 is indicated, but sometimes, tox is invoked from within PyPy, so the env name is py or python, but the interpreter is still PyPy.

Is there a way within conftest to disable the coverage behavior based on arbitrary Python logic? What incantations would be necessary at what hook point to disable coverage? Thanks!

@ionelmc
Copy link
Member

ionelmc commented Jul 3, 2020

See if #241 (comment) still works.

I guess there should be at least something documented about this.

@jaraco
Copy link
Author

jaraco commented Jul 3, 2020

That technique does not seem to be having the desired effect. See the referenced PR on Setuptools, where the PyPy tests take ~17 minutes to run, whereas with coverage disabled, they take ~6 minutes to run.

@ionelmc
Copy link
Member

ionelmc commented Jul 12, 2020

Ok so I have to think a bit how to give a nice api for this, but meanwhile try this:

def pytest_configure(config):
    cov = config.pluginmanager.get_plugin('_cov')
    cov.options.no_cov = True

If stuff is still slow try to add this:

    if cov.cov_controller:
        cov.cov_controller.pause()

@jaraco
Copy link
Author

jaraco commented Jul 12, 2020

In pypa/setuptools#2254, I learned that cov.cov_controller.pause() was necessary, but it's working now! Thank you so much.

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

No branches or pull requests

2 participants