pytest

built-in
0.0% Savings
0 Commands
0 Tokens saved
4 Tests

Install

tokf install 41ad482cfb65dc9f21099bf8b6438f96a8ed172a25f17e6919e7be48931b535b
Safety checks passed

Filter definition

# pytest.toml — show failing assertions and the pass/fail summary
#
# This filter used to `run = "pytest --tb=short -q {args}"`. That answered a
# narrower question than the user asked: the full tracebacks never reached
# tokf, so `tokf raw <id>` could not recover them (issue #430). It now runs
# the user's `pytest` as typed and does the reduction here, over the complete
# output — long tracebacks included.

command = "pytest"
description = "Show failing assertions and the pass/fail summary"

# Only flags that change what pytest *does*, rather than how much it prints.
# `-q`, `-v`, `-x` and `--tb=…` all still emit the FAILURES block and the short
# summary this filter keys off, so filtering them works and is worth doing —
# they were previously listed here and that silently disabled the filter for
# the most common invocations.
passthrough_args = [
  "--collect-only", # lists tests instead of running them
  "--co",
  "--pdb",          # drops into an interactive debugger
]

# The FAILURES block holds one section per failing test. In pytest's default
# (long) traceback format the failing source line is prefixed `>` and the
# assertion detail lines `E`, both padded — the `keep` in the template below
# picks exactly those out of each block and discards the surrounding frames.
[[section]]
name = "failures"
enter = "^=+ FAILURES =+$"
exit = "^=+ short test summary"
collect_as = "failure_lines"

# Collection errors (bad import, syntax error) land in an ERRORS block rather
# than FAILURES, and their `E` line carries the actual reason — without this
# section a failed collection reported only "ERROR tests/test_x.py", leaving
# the model to guess why.
[[section]]
name = "errors"
enter = "^=+ ERRORS =+$"
exit = "^=+ short test summary"
collect_as = "error_lines"

# Per-test outcome lines from the short summary.
[[section]]
name = "summary"
match = "^(FAILED|ERROR)"
collect_as = "summary_lines"

# The final counts line, captured verbatim rather than reassembled from
# aggregates.
#
# Summing `(\d+) failed` across the whole output would double-count: with the
# full output now flowing through this filter, any traceback or captured-stdout
# line containing something like "2 passed" would be added to the totals. And
# reassembling the line from `failed`/`passed` aggregates alone reports
# "0 failed, 0 passed" for a collection error or a skip-only run — both false.
#
# Capturing pytest's own line keeps every outcome pytest reports (errors,
# skips, xfails, "no tests ran") and can never invent a number.
[[chunk]]
split_on = '^=+ .*\d+ (passed|failed|error|skipped|xfailed|xpassed).* in '
collect_as = "totals"

# Strip the `=` padding: `==== 3 failed, 2 passed in 0.01s ====` → the middle.
[chunk.extract]
pattern = '^=+ (.*[^ =]) =+$'
as = "line"

[on_success]
# Anchored to the padded summary line so a test name or captured output
# containing "N passed" can't be picked up instead.
extract = { pattern = "^=+ .*?(\\d+) passed", output = "✓ pytest: {1} passed" }

[on_failure]
output = """{failure_lines | each: "{value | lines | keep: "^[>E] "}" | join: "\n"}
{error_lines | keep: "^E  " | join: "\n"}

{summary_lines | join: "\n"}
{totals | each: "{line}" | join: "\n"}"""

[fallback]
tail = 10

Examples

a collection error reports the error, not '0 failed, 0 passed' ~317 tokens → ~30 tokens (91% saved)
Raw output
============================= test session starts ==============================
platform darwin -- Python 3.14.6, pytest-9.1.1, pluggy-1.6.0
rootdir: /home/dev/myproject
collected 0 items / 1 error

==================================== ERRORS ====================================
______________________ ERROR collecting tests/test_imp.py ______________________
ImportError while importing test module '/home/dev/myproject/tests/test_imp.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
/usr/lib/python3.14/importlib/__init__.py:88: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
tests/test_imp.py:1: in <module>
    import nonexistent_module_xyz
E   ModuleNotFoundError: No module named 'nonexistent_module_xyz'
=========================== short test summary info ============================
ERROR tests/test_imp.py
!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!
=============================== 1 error in 0.04s ===============================
Filtered output
E   ModuleNotFoundError: No module named 'nonexistent_module_xyz'

ERROR tests/test_imp.py
1 error in 0.04s
counts come from pytest's summary, not from traceback text ~271 tokens → ~80 tokens (70% saved)
Raw output
============================= test session starts ==============================
platform darwin -- Python 3.14.6, pytest-9.1.1, pluggy-1.6.0
rootdir: /home/dev/myproject
collected 2 items

tests/test_decoy.py F.                                                   [100%]

=================================== FAILURES ===================================
_____________________________ test_reports_counts ______________________________

    def test_reports_counts():
        summary = "9 passed, 7 failed"
>       assert summary == "all good"
E       AssertionError: assert '9 passed, 7 failed' == 'all good'
E         
E         - all good
E         + 9 passed, 7 failed

tests/test_decoy.py:3: AssertionError
=========================== short test summary info ============================
FAILED tests/test_decoy.py::test_reports_counts - AssertionError: assert '9 p...
========================= 1 failed, 1 passed in 0.01s ==========================
Filtered output



>       assert summary == "all good"
E       AssertionError: assert '9 passed, 7 failed' == 'all good'
E         
E         - all good
E         + 9 passed, 7 failed




FAILED tests/test_decoy.py::test_reports_counts - AssertionError: assert '9 p...
1 failed, 1 passed in 0.01s
failures reduce to assertion lines plus the summary ~496 tokens → ~151 tokens (70% saved)
Raw output
============================= test session starts ==============================
platform darwin -- Python 3.14.6, pytest-9.1.1, pluggy-1.6.0
rootdir: /home/dev/myproject
collected 5 items

tests/test_math.py F..FF                                                 [100%]

=================================== FAILURES ===================================
___________________________________ test_add ___________________________________

    def test_add():
>       assert add(1, 2) == 4
E       assert 3 == 4
E        +  where 3 = add(1, 2)

tests/test_math.py:16: AssertionError
_____________________________ test_nested_failure ______________________________

    def test_nested_failure():
>       helper(4)

tests/test_math.py:28: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
tests/test_math.py:7: in helper
    return deeper(intermediate)
           ^^^^^^^^^^^^^^^^^^^^
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

value = 8

    def deeper(value):
>       assert value < 5, f"value too large: {value}"
E       AssertionError: value too large: 8
E       assert 8 < 5

tests/test_math.py:11: AssertionError
__________________________________ test_error __________________________________

    def test_error():
>       raise RuntimeError("boom")
E       RuntimeError: boom

tests/test_math.py:32: RuntimeError
=========================== short test summary info ============================
FAILED tests/test_math.py::test_add - assert 3 == 4
FAILED tests/test_math.py::test_nested_failure - AssertionError: value too la...
FAILED tests/test_math.py::test_error - RuntimeError: boom
========================= 3 failed, 2 passed in 0.01s ==========================
Filtered output


>       assert add(1, 2) == 4
E       assert 3 == 4
E        +  where 3 = add(1, 2)





>       helper(4)











>       assert value < 5, f"value too large: {value}"
E       AssertionError: value too large: 8
E       assert 8 < 5





>       raise RuntimeError("boom")
E       RuntimeError: boom




FAILED tests/test_math.py::test_add - assert 3 == 4
FAILED tests/test_math.py::test_nested_failure - AssertionError: value too la...
FAILED tests/test_math.py::test_error - RuntimeError: boom
3 failed, 2 passed in 0.01s
all tests pass shows summary ~100 tokens → ~5 tokens (95% saved)
Raw output
============================= test session starts ==============================
platform darwin -- Python 3.14.6, pytest-9.1.1, pluggy-1.6.0
rootdir: /home/dev/myproject
collected 5 items

tests/test_ok.py .....                                                   [100%]

============================== 5 passed in 0.00s ===============================
Filtered output
✓ pytest: 5 passed
Warning: Community filters are third-party code. Review the filter definition above before installing it in production environments.
Browse all filters