Confused about the diff output of assert failures #14674
Replies: 2 comments 1 reply
-
|
The short answer: in your second screenshot, pytest evaluates and displays the comparison left-to-right as assert some_function() == generate_expected_output()
# left/actual right/expectedThe text above the full diff already gives that mapping: The confusing part is that the - expected/right: present in expected, missing from actual
+ actual/left: present in actual, not expectedThat matches the first screenshot too: This is consistent with the direction discussed in #3333 / #6673: the signs are not “left vs right”; they are “expected baseline vs observed value”, assuming the test is written as Your Git-diff intuition is a real issue here. In a source diff, A small output change would probably make this much easier to learn. Something like this before the full diff would remove most of the guesswork: or, when expression text is available: For string assertions, pytest -o assertion_text_diff_style=blockFrom a usability perspective, a docs clarification and/or a compact legend in the failure output seems reasonable. The current behavior is explainable, but your examples show that it is not self-evident, even to someone who reads this output constantly. If my answer solved your problem, you can click answered the question. I'm really here to help, and along the way I'm also collecting Galaxy Brain badges haha 😆 |
Beta Was this translation helpful? Give feedback.
-
|
I think the main takeaway here is that, with Personally, the mental model from #6673 works well for me:
Using Inspired from #3721 (comment), maybe a nice middle ground would be a small "legend" in the output? Perhaps something like: or if we can make it work, even: |
Beta Was this translation helpful? Give feedback.

Uh oh!
There was an error while loading. Please reload this page.
-
I'm a daily user of pytest in work and private projects for the past 9.5 years. I'm looking at pytest's output for test failures tens to hundreds of times a day, from locally running tests, to CI to colleagues' computers. Over that time, I have not built any intuition for reading the diffs that pytest is showing me for some data types (strings, lists, etc.).
I know there has been previous discussion about this (#3333, #6673) and I think something has been changed. I've read both threads and I'm still not sure if what was proposed and/or implemented is what pytest currently (9.x) does, or if it has been reverted in the meantime.
Most of the time, I just ignore the diff, but from time to time, when I think it could help me to understand what went wrong, I'll give it a try to understand the output. But my assumption on which parts in the output relate to the UUT's actual output and which parts relate to the expected output (which both might be complex expressions) turn out to be wrong more often than not.
Imagine that the lists above are some large, deeply nested data structures that just look like noise. This is what I often have to deal with.
For the life of me, I would not be able to guess on which side the
40above is coming from. Is it fromsome_function()or fromgenerate_expected_output()? If I don't have any other clues, I'll try to guess. It feels like I'm guessing wrong about 75% of the time, no matter how hard I try to say to myself "your first intuition is going to be wrong, flip it around". No matter whether I follow that self-given advice, I'm wrong more often than not.Now normally I don't give it too much thought and just give up, finding a different approach (e.g. printing the UUT's output or using a debugger).
But writing this down has forced me to think about the mechanisms in detail and this is what I have come up with:
If I had to guess what's happening in my brain is that there's a lot of intuition about looking at Git diffs (and other places where source code diffs are displayed, e.g. GitHub, GitLab, my IDE, …), where there's a very consistent correlation in presentation and meaning:
git diff $left $right$left$right-+So when I look at the
40above, because it is green and it has a+in front of it, all the neurons associated with the table's right column in my brain light up, producing the following associations:+and it's green. The40must come from the right side of the==.40is on the line after the4, so it makes sense to relate to the right side. The right side is after the left side.+, this is what I need to add to the UUT to make it not broken anymore.But at the same time I've learned to not trust this intuition, my brain has probably learned to invert these signals in this context, but not all of them and not all of the time, leading to a feeling of being confused, leading to me having no grasp on when and when not to trust my intuition (i.e. inverting what my intuition is telling me).
This is a real downer for me. I guess that some people have an easier time building a strong intuition for a context, even when it contradicts the existing intuition from a different more common context, but it seems I can't even after many years. The negative correlation with diff output in other contexts is just too strong.
Other's in my work team have a similar experience with pytest. I'm neurodivergent and so are most in my team, so maybe there's an accessibility issue here as well.
How do you all deal with this? Is it always intuitive for you? Do you struggle with it from time to time? If so, have you found workarounds?
Beta Was this translation helpful? Give feedback.
All reactions