-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Presence of a plugin causes doctests a shared namespace to fail traceback with assertion #8059
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
Comments
In the referenced commit, I've confirmed and committed the second workaround, strengthening my suspicion that assert rewrite is implicated. |
I took a quick look at _pytest.assertion.rewrite, I'm reminded that rewrites are done through a special importer, so it's probably not a surprise that if a namespace ( I notice #2371 and #2419 may be related, though neither of those reference doctests. Probably the most helpful question to be answered: how is it that in the simple, general case, doctests that trigger assertions don't stumble on rewritten assertions? |
Hi @jaraco, Sorry for the delay. The assertion rewriter is triggered automatically in test modules (normal The code that marks plugin files for rewriting is found here: pytest/src/_pytest/config/__init__.py Lines 1115 to 1133 in 775ba63
This iterates over all the files in a plugin package and marks their asserts for later rewriting on import. As an example, So your assessment is correct: this mechanism is rewriting all asserts inside the I suspect your case is not isolated, the rewriter is probably being triggered in other packages which include a pytest plugin, although it should only cause problems if someone expects assertion errors in a specific format (like doctests), as the rewriter only changes the assertion message/traceback.
In the general case doctests are not part of a plugin, but normal user code, so they don't get rewritten. As for workarounds, your two first suggestions are on-point, as you confirmed by using About fixing this in pytest, I'm not sure how we could detect that part of a plugin is normal code, which shouldn't be changed by the assertion rewriter, and other part is "testing" code, which should have their assertions rewritten. |
In jaraco/jaraco.test#3, I've described an issue that appears to implicate some of the most edgy features of pytest and Python, in particular:
Oh, joy.
In summary, the issue seems to be that:
The presence of an empty module as a plugin in a namespace shared by the package under test causes assert exceptions in doctests to be re-written, causing the output from those doctests to fail where they wouldn't otherwise, even if the plugin is disabled with
-p no:...
.As you can see from the tox execution in the downstream report, I'm using pytest 6.1.2 on Python 3.9.0 with jaraco.test 4.0.1 (the implicated plugin) on macOS 11.0.1 (latest stable everything as of this writing).
I don't yet have a minimal example, though you can check out jaraco/jaraco.itertools@e9f23a4 and run
tox -- -k assert
to see the error.I expect (though haven't verified) that any of the following remedies might be possible:
jaraco
namespace. I was considering creatingpytest-enabler
anyway.assert
with araise AssertionError
. I'll test that soon.None of these workarounds would address the root cause, but instead avoid the confounding factors leading to the failed expectation.
I recognize this issue is probably too obscure to expect it to be fixed upstream, but I could really use some advice on how to proceed with an investigation. In particular, can you explain or point me to the implementation where doctests get special treatment for assert-rewrite (or vice-versa), especially any code that might be relevant for selectively affecting the modules-under-test or plugins?
The text was updated successfully, but these errors were encountered: