Skip to content

CliRunner.invoke doesn't capture stdout from logging #2497

Closed

Description

@Kamforka

The result of CliRunner().invoke doesn't capture stdout logs from logging loggers.

Example:

# capture.py
import logging
import sys

import click
from click.testing import CliRunner


def test_output():
    handler = logging.StreamHandler(sys.stdout)
    logger = logging.getLogger("default")
    logger.addHandler(handler)
    logger.setLevel(logging.INFO)
    sample_log = "Info log from the default logger."

    @click.command()
    def cli():
        logger.info(sample_log)

    runner = CliRunner()
    result = runner.invoke(cli)
    assert result.exit_code == 0
    assert result.output == sample_log

Then to verify that the capture doesn't work:

$ pytest capture.py -vv
======================================================== test session starts ========================================================
platform linux -- Python 3.11.2, pytest-7.2.2, pluggy-1.0.0 -- <...>/venv/bin/python
cachedir: .pytest_cache
rootdir: <...>
collected 1 item                                                                                                                    

capture.py::test_output FAILED                                                                                                [100%]

============================================================= FAILURES ==============================================================
____________________________________________________________ test_output ____________________________________________________________

    def test_output():
        handler = logging.StreamHandler(sys.stdout)
        logger = logging.getLogger("default")
        logger.addHandler(handler)
        logger.setLevel(logging.INFO)
        sample_log = "Info log from the default logger."
    
        @click.command()
        def cli():
            logger.info(sample_log)
    
        runner = CliRunner()
        result = runner.invoke(cli)
        assert result.exit_code == 0
>       assert result.output == sample_log
E       AssertionError: assert '' == 'Info log from the default logger.'
E         - Info log from the default logger.

capture.py:22: AssertionError
------------------------------------------------------- Captured stdout call --------------------------------------------------------
Info log from the default logger.
--------------------------------------------------------- Captured log call ---------------------------------------------------------
INFO     default:capture.py:17 Info log from the default logger.
====================================================== short test summary info ======================================================
FAILED capture.py::test_output - AssertionError: assert '' == 'Info log from the default logger.'
========================================================= 1 failed in 0.10s =========================================================

From the above one can see that the content of result.output is an empty string, but judging from the stdout content captured by the test it should be "Info log from the default logger.".

Environment:

  • Python version: 3.11.2
  • Click version: 8.0.4

Activity

  1. davidism commented on Apr 24, 2023

    @davidism
    Member

    Duplicate of existing issues about pretest's logging capture.

  2. Kamforka commented on Apr 24, 2023

    @Kamforka
    Author

    Hi @davidism, thanks for the heads up.
    Can you please link me to the related issue so I can follow the conversation?

  3. locked as resolved and limited conversation to collaborators on May 9, 2023
  4. added this to the 8.4.0 milestone on Apr 29, 2026
  5. added
    test runner
    on Apr 29, 2026
  6. changed the title [-]CliRunner.invoke doesn't capture stdout from logging[/-] [+]`CliRunner.invoke` doesn't capture `stdout` from `logging`[/+] on Apr 29, 2026
  7. kdeldycke commented on Apr 29, 2026

    @kdeldycke
    Collaborator

    This is going to be addressed in v8.4.0 by #3391 which introduce a new capture="fd" mode to CliRunner.

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

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions