-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
py.test doesn't reimport changed modules #762
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
Original comment by Bruno Oliveira (BitBucket: nicoddemus, GitHub: nicoddemus): I'm just guessing that Pytest doesn't try to reimport modules before executing test cases; all modules are collected during startup before the first test is even executed, so this is certainly not a bug and is working as intended. If you post more details of what you are trying to accomplish (instead of how) we might be able to help you better. |
Original comment by BitBucket: caryoscelus, GitHub: caryoscelus:
Passing tests when there should be fail (reverting example above gives exactly this) is working as intended? Well, that's kinda surprising. |
Original comment by Bruno Oliveira (BitBucket: nicoddemus, GitHub: nicoddemus): Global changes will carry over from one module to the next; pytest (and any other testing framework AFAIK) does not attempt to restore global state between each test. You should take care to restore any global state changed during each test run, and pytest's fixtures are a great way to accomplish that. For your example specifically, you could use #test_a.py
import module
def do_nothing():
pass
def test_nofail(monkeypatch):
monkeypatch.setattr(module, 'fail', do_nothing)
module.fail()
|
Original comment by BitBucket: caryoscelus, GitHub: caryoscelus: With more real-world examples (e.g. test_a.py doesn't change module directly, but through other module import) using monkeypatch (or other explicit methods) won't help simply because the fail would not be recognized in the first place. |
Original comment by Bruno Oliveira (BitBucket: nicoddemus, GitHub: nicoddemus): It doesn't matter if the global state is changed by Please keep in mind that changing global state during import is usually a bad idea, and as I mentioned before you will encounter this problem with any other testing framework, not just pytest. |
Original comment by BitBucket: caryoscelus, GitHub: caryoscelus: It does matter if the module changing global state is third-party. You can propose whatever good solution there is, but as long as it requires something to do locally, it won't help. Let me illustrate this with example, if that's not clear enough for you:
End-user won't be able to find the fail without running |
Original comment by Bruno Oliveira (BitBucket: nicoddemus, GitHub: nicoddemus): You are correct, that will be a problem, but that's why it is not a good idea to change global state during import. :/ |
Originally reported by: BitBucket: caryoscelus, GitHub: caryoscelus
Test case:
pytest test_b.py
runs fine, but justpytest
failsThe text was updated successfully, but these errors were encountered: