Skip to content

Add context_settings parameter to CliRunner.invoke - #3896

Open
kdeldycke wants to merge 1 commit into
pallets:stablefrom
kdeldycke:clirunner-context-settings
Open

kdeldycke wants to merge 1 commit into
pallets:stablefrom
kdeldycke:clirunner-context-settings

Conversation

@kdeldycke

Copy link
Copy Markdown
Collaborator

This PR adds a context_settings parameter to CliRunner.invoke(), allowing tests to pass context-specific settings to the command's Context.

CliRunner.invoke() passes its extra keyword arguments to the Context of the command, except color: its own color parameter takes that name. context_settings solves this collision. And now a test can set what its terminal supports, as well as the color of the context in the same call.

With this addition, I can now test cases that were previously difficult or impossible to cover, such as verifying how the command behaves with different Context settings.

This was proposed 5 years ago in #2110 but at that time this change was evaluated as niche. Since then, we did a lot of color handling improvements in Click (#3505, #3653, #3739) which added a lot of color-related tests, so this is no longer a niche concern.

For evidence, I used the new context_settings parameter to extend and simplify existing checks. See test_prompt_and_confirm_ansi_respects_color and test_tempfile_pager_handles_ansi.

@kdeldycke kdeldycke added docs Updates to documentation, readme, docstrings, typos test runner CliRunner and Click's test utilities feature labels Oct 26, 2026
@kdeldycke kdeldycke added this to the 8.5.1 milestone Oct 26, 2026
@kdeldycke kdeldycke linked an issue Oct 26, 2026 that may be closed by this pull request
@kdeldycke
kdeldycke force-pushed the clirunner-context-settings branch from e3a1b8d to 6140c6b Compare October 26, 2026 09:58
@kdeldycke
kdeldycke force-pushed the clirunner-context-settings branch from 6140c6b to ee07b7c Compare October 26, 2026 10:14
kdeldycke added a commit to kdeldycke/click-extra that referenced this pull request Oct 26, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs Updates to documentation, readme, docstrings, typos test runner CliRunner and Click's test utilities feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

testing.CliRunner.invoke cannot pass color for Context instantiation

1 participant