Consolidate CLI testing helpers - #51
Merged
Merged
Conversation
Move common helpers into conftest, so that behaviors can be easily shared. This change also adds an `assert_exit_code=0` kwarg to the test runner invocations, so that running CLI tests always asserts exit codes unless we explicitly opt-out. This prevents a common mistake when tests are added for a CLI tool, in which the exit code is not checked.
Contributor
|
Nice! You can simplify that return value: diff --git a/tests/conftest.py b/tests/conftest.py
index a138f13..f1f7fd1 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -1,6 +1,7 @@
from __future__ import annotations
import dataclasses
+import functools
import typing as t
import pytest
@@ -44,7 +45,4 @@ class CliRunner:
@pytest.fixture
def runner_factory(capsys: pytest.CaptureFixture[str]):
- def bind(entry_point: CliEntryPoint, /) -> CliRunner:
- return CliRunner(entry_point, capsys)
-
- return bind
+ return functools.partial(CliRunner, capsys=capsys)If you want, I can follow up and add types to the tests; I thought it would be easy (-> missing on runner_factory), so I asked Claude to add them locally, but I see from the diff it's pretty involved as they are missing in a few other places. |
Contributor
Author
|
Oh, nice Let's save more type coverage for the tests for later? It can get somewhat involved. The last time I worked on this for a package, I found that it's sometimes better to rephrase a fixture entirely rather than trying to describe what's there. (Stuff like protocols with |
- use `partial()` - annotate the return type as a callable type
henryiii
approved these changes
Aug 10, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Move common helpers into conftest, so that behaviors can be easily shared.
This change also adds an
assert_exit_code=0kwarg to the test runner invocations, so that running CLI tests always asserts exit codes unless we explicitly opt-out. This prevents a common mistake when tests are added for a CLI tool, in which the exit code is not checked.I write slightly different versions of this same testing tool in each Python CLI that I maintain.
Each one comes out slightly different, and in this case there ws good prior art to build upon. I like this iteration very well: it's short, simple, and effective.
📚 Documentation preview 📚: https://dependency-groups--51.org.readthedocs.build/en/51/