-
Notifications
You must be signed in to change notification settings - Fork 213
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
coverage starts too late when a plugin imports the project under test #635
Comments
Oh, gosh. And now I notice there is already code in pytest-enabler to handle removing dependencies from the cache. So maybe that's the best approach (just keep that list up-to-date). |
Kind of chicken and egg problem. Not much I can do to make coverage start early enough without creating more problems (messing with sys.modules is a big no-no, already getting "lets just blame pytest-cov" bug reports like #620 when other libraries or projects are doing it). Right now you only have 2 options (neither are good if you're really committed to using pytest-enabler to activate pytest-cov):
|
I've read this page several times and I still don't understand what it's recommending.
Which of these settings affects "using the append feature"? I assume it's the Which of these settings affects the "manually starting |
Those env vars will make the subprocess recorder start really early (via the pth file, see https://github.com/pytest-dev/pytest-cov/blob/master/src/pytest-cov.pth and https://github.com/pytest-dev/pytest-cov/blob/master/src/pytest_cov/embed.py). Obviously it's contrived, and you'd only do this if really want to avoid using The append setup is there so cov data can be merged when coverage is restarted by the normal invocation of pytest-cov (when pytest decides it's time for the pytest-cov plugin to run its hooks). |
I do really want to avoid having to wrap the pytest invocation. There are some nice advantages to using a plugin like pytest-cov. If I were to wrap the pytest invocation in the tox config with coverage, it would be difficult for a user to disable. As it's currently configured for my projects, one can readily disable coverage with I think I can see now how the pytest-cov plugins guidance works now and may be applicable to my use-case. It's a little annoying that it would essentially need to be applied to every project that uses pytest-enabler (since it would be difficult to anticipate in advance which projects pytest-enabler uses). That's going to be a lot of cruft for the few projects that need it.
I agree - it's hacky and prone to error, but it has the huge advantage of being manageable within a single project (in this case pytest-enabler). Since the approach is working, I'll probably stick with it for now. It sure would be nice, though, if there were some way for pytest to isolate the imports that happen prior to running tests such that in the general case, even for dependencies of pytest and its plugins, the tests themselves actually import the modules as they likely would in the real world. That is, in an ideal world, the pytest setup would be hermetic and wouldn't have any effect on the behavior at test-time. 💡 This makes me wonder if Python itself should offer a contextual cache for imports, such that pytest could "enter" an import context for its setup, then exit it before running tests, effectively isolating the imports that happen during setup and collection from the test run. As time permits, I may pursue something in that vein. In the meantime, I'm closing this issue as I don't have any recommendations for this project at this time. Thanks for all the support. |
Consider the scenario described in python/importlib_resources#304:
importlib_resources
.pytest-enabler
) imports the project under test.pytest-enabler
must run to determine if/how to enablepytest-cov
).This issue is similar to #437, but also different.
This issue also affects coverage of
jaraco.context
,jaraco.functools
and any other project on which pytest-enabler depends.I wonder if perhaps pytest-cov couldn't somehow avoid scenarios like these, such as by clearing
sys.modules
orimportlib.reload
ing any modules that are being examined for coverage.Or perhaps it's not that big of an issue and only really affects running tests on any projects that
pytest-enabler
imports (because most other plugins run after pytest-cov). So maybe there's something thatpytest-enabler
should do (such as removing its imports fromsys.modules
after getting their objects.The text was updated successfully, but these errors were encountered: