-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
ValueError: I/O operation on closed file in testing #824
Comments
I am seeing this also when using |
This also helps with failures during testing, e.g. when using `pdb.set_tracce()` somewhere inside the runner's isolation - likely since it handles `write` already. Ref: pallets#824
Reminded me of an old stash I had: #951. |
As for pytest itself I've created pytest-dev/pytest#3344. |
See also #654 (duplicate?!). |
I hit a similar error when writing a CLI of my own & thought I'd share my fix in the hope it helps isolate where there might be a fix (and this is the top hit in Google for me). In one of my commands I was opening stdout using with click.open_file(input) as src, click.get_text_stream('stdout') as sink:
data = parse_input()
sink.write(json.dumps(data) + '\n') ...and I think the closing of the sink stream at the end of the context was killing the mocked version of stdout in the test? Anyway, the workaround was to shift to |
I got the issue recently as well. It was due to the use of a logger within the function. |
The workaround I found is to write my pytest function with the def test_xxxx(caplog):
caplog.set_level(100000)
... |
I recently ran into this and created a repo which reproduces the issue standalone: https://github.com/cdleonard/click-pytest-issue The problem seems to be that the stdio capturing of pytest and click does not mix well. My workaround is to just avoid CliRunner and call main() directly because pytest caplog/capsys already covers everything. Maybe there could be an option to disable the stdio isolation feature of CliRunner? |
See #2156 for a simple example that reproduces the error on Windows. The example only fails if pytest live logs is enabled. |
Not sure yet, but pallets/click#824 and then https://docs.pytest.org/en/6.2.x/logging.html#live-logs suggested turning this off and it seems to have worked.
Not sure yet, but pallets/click#824 and then https://docs.pytest.org/en/6.2.x/logging.html#live-logs suggested turning this off and it seems to have worked.
Doing the following seems to work well for me: def test_no_args(capsys: CaptureFixture):
with capsys.disabled() as disabled:
result = CliRunner.invoke(...) |
… get commands (#447) * Test: Add tests for get from-ansible * CI: Update pre-commit * fix `tags` for nrfu foo commands * add custom type for loglevel * add error handling for None arguments * add logs to anta.device * Cleanup anta.cli * add logs to anta.inventory * update unit tests for anta.cli * fix pallets/click#824 * update unit tests for anta.cli.nrfu * refactor cli modules * update anta.cli.check and anta.cli.debug * update cli unit tests * update unit tests for anta.cli * small fix for anta get from-ansible * refactor wrappers * consume ignore_status and ignore_error * remove shebangs * test revision and version in unit tests * fix anta nrfu --help * Test: Fix tox coloring issues * Test: Adjust pytest default logging level * Refactor(anta): Make sure test name is not truncated in table * Test: Set width of CliRunner * Update documentation * Add warning about CLI changes in documentation * change -log option to -l --------- Co-authored-by: Matthieu Tâche <mtache@arista.com>
FYI, building on previous answers in this thread, we ended up creating the following fixture to disable capsys on all invoke calls (truncated to simplify) @pytest.fixture
def click_runner(capsys: CaptureFixture[str]) -> Iterator[CliRunner]:
"""
Convenience fixture to return a click.CliRunner for cli testing
"""
class MyCliRunner(CliRunner):
"""Override CliRunner to disable capsys"""
def invoke(self, *args, **kwargs) -> Result:
# Way to fix https://github.com/pallets/click/issues/824
with capsys.disabled():
result = super().invoke(*args, **kwargs)
return result
yield MyCliRunner() |
I had the issue as well for pytest live logs. I actually wanted to see logs for passed tests though (not necessarily "live"). So running |
I was running into this with the |
I am getting a
ValueError
when usingCliRunner
to test my CLI tool. Unfortunately I can't isolate, but I can give a reproduction:pip install git+git://github.com/ResidentMario/datablocks.git@d6601c8321e7fb050bb62760067dc63ca4600003 pip install pytest cd datablocks/tests pytest cli_tests.py
Which raises the following traceback:
This is on Ubuntu 16.04.
The text was updated successfully, but these errors were encountered: