pytest-dev / pytest-dev/pytest

Show whitespace escape code in string diffs

Open
#10,704 4 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

topic: rewrite type: enhancement
Dominant language
Python
Stars
14.5k
Forks
3.4k
Avg merge
2d 9h
Merged PRs (30d)
35

Description

What's the problem this feature will solve?

I just had an assert output the following:

>       assert expected == out
E       AssertionError: assert '/home/h/Git/...ocess, ...)` ' == '/home/h/Git/...cess, ...)`\n'
E         Skipping 160 identical leading characters in diff, use -v to show
E         - ess, ...)`
E         ?           ^
E         + ess, ...)` 
E         ?           ^

very helpfully pointing out that ... my space is not a good space? In fact I had a space instead of a newline, but this message certainly isn't terribly helpful in showing that. Throwing on more -v's also doesn't make a difference. This is particularly egregious if it's at the end of the string, but you get unhelpful messages in other cases as well.

In the "short test summary info" it is actually shown, but only if it's at the end or is a short string - and I personally only noticed that when looking closer at the output while writing this issue.
EDIT: I also now saw that in the first line with the AssertionError it's also shown. So it is only the supposedly helpful extra printout that doesn't have it.

=================================== short test summary info ===================================
FAILED tests/test_flake8_trio.py::test_anyio_from_config - AssertionError: assert '/home/h/Git/...ocess, ...)` ' == '/home/h/Git/...cess, ...)`\n'
Describe the solution you'd like

Print the repr (or \t/\n) in the AssertionError diff printout - at least if that's among the characters that differ.

This only took me a second to figure out personally in this case, and maybe that's the case in the vast majority of the cases it happens such that it's not worth the effort to implement / overly noisy. But I figured it's worth considering - esp as it's inconsistent with the other printouts.

Additional context
$ pytest --version
pytest 7.2.1

No flags, and pretty sure I have no pytest configuration playing into this.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the reported assertion with pytest 7.2.1 and compare the AssertionError diff printout with the short test summary output. Trace the pytest assertion-diff path responsible for the extra printout; done means differing whitespace is visibly escaped without making ordinary diffs unnecessarily noisy, with regression coverage for the example.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
testing
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.