Skip to content

Cannot test output to stderr #2468

@dwt

Description

@dwt

I am not entierely sure this is a bug or more a consequence of how the testing support is integrated into click.

Here is my scenario: I am writing a small shell script that outputs an error message to stderr by calling sys.exit(some_string). This is fine, if perhaps a bit unintuitive that I have to check result.exception for this instead of result.stderr.

However earlier the program configures the logging package with logging.basicConfig(level=logging.INFO). In my testing, as well as how I read the documentation this should mean that all output from logging.warning('Fnord') and similar logging should be printed to stderr.

They are however not accessible via result.stderr.

Several reasons come to mind: I am testing via pytest, which does log capturing, so I guess that it just captures the log messages, due to the code in question being called in process, not via subprocess.run().

import logging

import click
from click.testing import CliRunner


def test_reproduction():
    @click.command
    def main():
        logging.basicConfig(level=logging.INFO)
        logging.error('fnord')
    runner = CliRunner(mix_stderr=False)
    result = runner.invoke(main, [])
    assert 'fnord' in result.stderr

I'm not quite sure how to achieve this, but I would like to not have to care in unit tests about what pytest or another test framework does and get the log messages the way I configured them. Because in essence this means that it is not easily testable that logging is wired correctly to print to stderr in the cli application.

Environment:

  • Python version: 3.11.2
  • Click version: 8.1.3
  • pytest version: 7.2.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions