Add PytestSessionLexer to properly highlight pytest session output by emilymamcarz · Pull Request #2996 · pygments/pygments

Summary

This PR adds a dedicated PytestSessionLexer to Pygments to properly highlight pytest session output in documentation, examples, and testing outputs. Previously, the subtest lines in documentation were incorrectly highlighted, implying all subtests passed. This lexer fixes that while preserving existing highlighting for section headers, captured output, assertions, and tracebacks.

Details

PytestSessionLexer is a RegexLexer with:

  • Section header highlighting (==== FAILURES ====)
  • Subtest line highlighting (e.g., FAILED [100%] tests/test_file.py::TestClass::test_method[subtest x])
  • Captured stdout/stderr blocks
  • Assertion errors (E AssertionError)
  • File:line references (tests/test_file.py:14: AssertionError)
  • Fallback to Text for other lines
    The lexer is registered under the alias "pytest".

This PR resolves #2995