Skip to content

Codex/assertion text diff blocks#14425

Open
hamza-mobeen wants to merge 6 commits intopytest-dev:mainfrom
hamza-mobeen:codex/assertion-text-diff-blocks
Open

Codex/assertion text diff blocks#14425
hamza-mobeen wants to merge 6 commits intopytest-dev:mainfrom
hamza-mobeen:codex/assertion-text-diff-blocks

Conversation

@hamza-mobeen
Copy link
Copy Markdown

@hamza-mobeen hamza-mobeen commented Apr 27, 2026

closes #6757

Description

This PR introduces a new configuration option, assertion_text_diff_style, to allow users to customize how multiline string assertion failures are displayed.

As highlighted in #6757, when pytest uses ndiff to display differences for multiline strings (like output from capsys), it can sometimes become an unreadable "soup", especially when changes involve indentation or leading/trailing whitespace. The current behavior often results in a confusing line-by-line diff that is difficult to parse.

This PR adds an optional block diff style that displays the exact contents of the "Left" and "Right" text blocks intact and sequentially, making it significantly easier to read the literal multiline strings being compared without the interspersed ndiff noise.

Key changes:

  • Added assertion_text_diff_style configuration option. Defaults to ndiff to preserve backwards compatibility.
  • Implemented the block diff style (_diff_text_block) for multiline string comparisons, solving the readability issue raised in assertion diffs for multiline-string can become unreadable soup #6757.
  • Added fallback logic so that single-line comparisons still use ndiff even if block is selected.
  • Added strict validation to ensure assertion_text_diff_style is either ndiff or block.
  • Included extensive test coverage in testing/test_assertion.py verifying block formatting, fallback logic, blank line handling, and configuration validation.

Checklist

  • Include documentation when adding new features.
  • Include new tests or update existing tests when applicable.
  • Allow maintainers to push and squash when merging my commits. Please uncheck this if you prefer to squash the commits yourself.
  • Add text like closes #XYZW to the PR description and/or commits (where XYZW is the issue number).
  • If AI agents were used, they are credited in Co-authored-by commit trailers.
  • Create a new changelog file in the changelog directory, with a name like <ISSUE NUMBER>.<TYPE>.rst.
  • Add yourself to AUTHORS in alphabetical order.

@psf-chronographer psf-chronographer Bot added the bot:chronographer:provided (automation) changelog entry is part of PR label Apr 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot:chronographer:provided (automation) changelog entry is part of PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

assertion diffs for multiline-string can become unreadable soup

1 participant