-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
ValueError: I/O operation on closed file #14
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 holger krekel (BitBucket: hpk42, GitHub: hpk42): fix issue14 : it was actually issue14 instead of issue8 that was fixed with please try out with the usual "pip install -i http://pypi.testrun.org -U pytest" |
Original comment by Alfredo Deza (BitBucket: alfredodeza, GitHub: alfredodeza): I can confirm this gets fixed with the update. Sorry for not reporting it sooner... |
Original comment by eduardo schettino (BitBucket: schettino72, GitHub: schettino72): there is still a problem with this when used with capsys... {{{ (py26)~/work/xxx$ cat bug14.py (py26)~/work/xxx$ py.test bug14.py bug14.py . ============================================== 1 passed in 0.01 seconds ============================================== }}} |
Original comment by holger krekel (BitBucket: hpk42, GitHub: hpk42): can you quickly try with "pip install -U -i http://pypi.testrun.org pytest" which should get you 2.0.3.dev3 and fix the issue? (To be honest, i am not yet sure why it is fixed, but i'd rather first check if it works for you as well) |
Original comment by Ronny Pfannschmidt (BitBucket: RonnyPfannschmidt, GitHub: RonnyPfannschmidt): still in trunk |
Original comment by Ronny Pfannschmidt (BitBucket: RonnyPfannschmidt, GitHub: RonnyPfannschmidt): this would probably best be fixed by integrating logcapture into pytest |
Original comment by Ronny Pfannschmidt (BitBucket: RonnyPfannschmidt, GitHub: RonnyPfannschmidt): as a extra note, it seems this is version specific to python2.7 |
Original comment by Alfredo Deza (BitBucket: alfredodeza, GitHub: alfredodeza): I am not consistently hitting this with Python 2.6
This only happens if I use |
I am hitting this with pytest 2.7.2 and python 2.7.2. The issue seems to occur if the logger I use is instantiated at the module level instead of inside a module function. |
Hi @jfitzpatrick99, can you provide a small example which reproduces the problem? |
@nicoddemus I just tried putting together a small reproducer but at the moment I can only reproduce with my entire application. I will see if I can get something together for tomorrow. |
@jfitzpatrick99 thanks! 😄 |
I have encountered this issue when running parametrised tests. The test for the first parameter value always passes, but the next ones fail. Pytest 2.8.1 |
@shapiy Could you maybe show an example which reproduces the issue, or if it happens in a public codebase, do you have a link? |
Unfortunately, the code I am working on is not available for public. The problem is very similar to @jfitzpatrick99's : the issue reproduces in the context only. Actually, I'm not sure if pytest is to blame. It could be any other component in the system. |
@The-Compiler, here's a sample: @pytest.mark.parametrize('param', [1 ,2])
def test_logging(param, capsys):
logging.basicConfig(level=logging.DEBUG, stream=sys.stdout)
logger = logging.getLogger('test')
logger.info('A')
out, err = capsys.readouterr()
print(err)
assert 'A' in out Will pass for the first test, but will fail for the second:
|
Passing a concrete captured stream that will be closed without removing its handler in teardown is bound to fail Not even the pending merge of log capturing can fix that particular usage pattern |
@RonnyPfannschmidt I just hit the exact same issue as @shapiy above when testing if logging output of my package is correct. What is the correct usage pattern to do this with pytest? |
Short answer (IMO) is to use pytest-catchlog which provides great integration with the Long answer: The problem is (please correct me @RonnyPfannschmidt): logging.basicConfig(level=logging.DEBUG, stream=sys.stdout)
You should "undo" what |
Thank you for your pointers! I have found if I leave In the end, I found that removing the stream handler manually afterwards was enough to make the tests run as expected:
|
Glad you found a solution! Notice that you can add that to a fixture and even make it Out of curiosity, did you try to use |
Yeah I know I could do a fixture, but it's just for those two tests, if I use it more I'll definitely refactor it. |
OK, thanks! |
By the way, thank you for a great tool, all you pytest devs! |
I was getting this error when disabling a custom logger in one test, which's state persisted to the next test. Be cautious :) |
This is still an issue with the latest pytest across tests (e.g. when one tests change the logging config and add a stream handler pointing to stdout, a subsequent test that doesn't setup logging and tries to write to logging will fail). I think the issue boils down to the fact that if you set up a Stream Handler and you enable capsys the actual output stream will be a pytest stream that will be closed and thrown away at the next test in the test suite. So when a subsequent test tries to write to it you get this error. Adding the following fixture fixed this: LOGGER = logging.getLogger()
@pytest.fixture(autouse=True)
def ensure_logging_framework_not_altered():
before_handlers = list(LOGGER.handlers)
yield
LOGGER.handlers = before_handlers Should this be automatically done always by pytest? Granted for this to work ideally you would need to do it for all loggers, not just the root one. In this case, for me, this was enough as I was only set up a custom stream logger onto the root logger. In an ideal world, I would expect though pytest to cleanup loggig configurations at the end tests that requested the caplog fixture. |
@gaborbernat thanks for the detailed description! I've created #5743 to track this. |
Originally reported by: Alfredo Deza (BitBucket: alfredodeza, GitHub: alfredodeza)
There is a problem with the logging module and py.test
It seems that if it runs with the '-s' flag it doesn't do this anymore.
The text was updated successfully, but these errors were encountered: