Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a hook to modify final error text #10666

Open
Rizhiy opened this issue Jan 15, 2023 · 7 comments
Open

Add a hook to modify final error text #10666

Rizhiy opened this issue Jan 15, 2023 · 7 comments
Labels
topic: rewrite related to the assertion rewrite mechanism type: proposal proposal for a new feature, often to gather opinions or design the API around the new feature

Comments

@Rizhiy
Copy link

Rizhiy commented Jan 15, 2023

What's the problem this feature will solve?

I have a test where I need to compare long strings.
A common failure is for spaces to be changed to tabs or other invisible characters.
I need a way to visualise invisible characters.

Describe the solution you'd like

Being able to modify the output string would enable me to replace invisible characters with visible substitutes and make debugging easier.

Concise example:

def test_tabs_spaces():
    pad = "foobar\n" * 10
    first_str = pad + "	" + pad  # Tab in the middle
    second_str = pad + "    " + pad  # Spaces in the middle
    assert first_str == second_str

This produces:

E       AssertionError: assert 'foobar\nfoob...bar\nfoobar\n' == 'foobar\nfoob...bar\nfoobar\n'
E         Skipping 60 identical leading characters in diff, use -v to show
E           ar
E           foobar
E         - 	foobar
E         +     foobar
E           foobar
E           foobar...
E
E         ...Full output truncated (8 lines hidden), use '-vv' to show

With two lines indistinguishable because my terminal doesn't show tabs as visible characters.

Alternative Solutions

I have tried pytest_assertrepr_compare, but then nice formatting is gone and it is impossible to find the error in a long string.

I guess, I can set my terminal to display tabs visibly, but that is not very user-friendly since that is not the default for the vast majority of people.

Additional context

TBH, might be a good idea to invisible characters using visible substitutes, e.g. "▸" for tab and "·" for space.

@Rizhiy
Copy link
Author

Rizhiy commented Jan 15, 2023

@RonnyPfannschmidt Already explained.

@RonnyPfannschmidt
Copy link
Member

as for nice formatting, the builtin variant does quite some extra work

i recommend taking a look

@Rizhiy
Copy link
Author

Rizhiy commented Jan 15, 2023

Why not allow us to modify the final string?

Alternatively the "extra work" can be exposed as a function to more easily implement in the current hook.

@RonnyPfannschmidt
Copy link
Member

Hookwappers may force/alter outcomes

@Rizhiy
Copy link
Author

Rizhiy commented Jan 16, 2023

Not really sure what you mean.

@Zac-HD Zac-HD added type: proposal proposal for a new feature, often to gather opinions or design the API around the new feature topic: rewrite related to the assertion rewrite mechanism labels Jan 17, 2023
@RonnyPfannschmidt
Copy link
Member

# untested 
@pytest.hookimpl(hookwapper=true) 
def pytest_assertrepr_compare(...):
  outcome = yield 
  Stuff.do_with(outcome)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: rewrite related to the assertion rewrite mechanism type: proposal proposal for a new feature, often to gather opinions or design the API around the new feature
Projects
None yet
Development

No branches or pull requests

3 participants