Skip to content

CliRunner fails with ValueError when using pytest live logs #2156

Closed

Description

@MikesRND

Clirunner.invoke() fails with a ValueError exception when capturing logs with pytest live logs.

The following example demonstrates the exception:

import click
from click.testing import CliRunner
import logging
logger = logging.getLogger(__name__)

logging.basicConfig(level=logging.INFO)

@click.command()
def cli():
    logger.info('hello')  # Exception goes away if this is commented out
    print('hello')

def test_cli_logging():
    runner = CliRunner()
    result = runner.invoke(cli)

    assert result.exit_code == 0
    assert result.stdout == 'hello\n'

Exception:

...
            finally:
                sys.stdout.flush()
>               stdout = outstreams[0].getvalue()
E               ValueError: I/O operation on closed file.

venv\lib\site-packages\click\testing.py:434: ValueError

To reproduce the exception, run pytest using from the command line:

pytest test_cli_live_logs.py --log-cli-level INFO

There is no exception if the --log-cli-level INFO flag is removed
Also, there is no exception if the logger.info statement is removed.

Environment:

  • Python version: 3.9.9
  • Click version: 8.0.3
  • Pytest version: 6.2.5

Activity

  1. davidism commented on Dec 21, 2021

    @davidism
    Member

    Duplicate of #824

  2. locked as resolved and limited conversation to collaborators on Jan 5, 2022
  3. added
    test runner
    on Aug 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    test runner

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions