You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Enhancement: Error report for Should be equal keyword #2806
Problem happened when I compared two values with keyword Should be equal:
${value} was set to specific date: '2015-05-15'
${returned_value} was just kind of readout from external device and should be again: '2015-05-15'
I used this to compare content: Should Be Equal ${value} ${returned_value}
But error was thrown: 2015-05-15 != 2015-05-15
After further investigation (and help from @pekakklarck) I added: Log ${returned_value} resp=True
then I was able to see that this string contain also NULL character and returned value was: '2015-05-15\x00'
Therefore, error reporting in Should Be Equal should be enhanced to make it easier to see problems like this.
This is similar issue than we had when comparing values that are different have same string representation, most notably numbers and strings (#817). We should probably use repr() when showing the error message if string values differ but their string representation is same.
This gets even more "fun" with Unicode equivalence, especially on Python 3 where repr() doesn't show code points:
>>>a='ä'>>>b='a\u0308'>>>a==bFalse>>>print(a, b)
ää>>>print(repr(a), repr(b))
'ä''ä'>>>print(len(a), len(b))
12
I guess we needed to go through the individual code points to be able to construct a custom representation showing what these strings actually contain. This could be one way:
Handling the original problem with null bytes isn't that straightforward either. With items of different types (like string '1' and integer 1) we add the type information if string representations are same, but that doesn't work with strings containing null bytes. If we have 'foo' and 'foo\x00', their string representations differ in Python, but the actual value shown to the user is identical. To be able to show to the user that the other has null bytes, we basically needed to explicitly check are null bytes present. The problem with this approach is that there are also other invisible characters that would need similar special handling.
Solving this so that we'd be always able to show how values differ when they look the same may just be too complicated. A simple workaround would be adding argument like repr=False to at least to Should Be Equal and Should Be Equal As Strings. This would turn error reporting to use repr() internally regardless the values. Having a custom repr() that handles also different Unicode normal forms (NFC and NFD) in Python 3 would still be useful.
It was pointed out in the comments of the Python issue I submitted about repr() that in Python 3 we can use ascii(text) to get the same behavior as repr(text) had in Python 2. Much better than my earlier idea to use text.encode('unicode_escape').decode('ASCII').
Also, the more I think about this the more I believe that adding repr=False to keywords is better than trying to show this kind of differences automatically. Under Python 3 we should use ascii(), not repr(), though. I believe repr would still be a good argument name because we use that also with Log.
Thanks for this. That was a good tip from the Python development team. Here's the description returned by help(ascii):
Return an ASCII-only representation of an object.
As repr(), return a string containing a printable representation of an
object, but escape the non-ASCII characters in the string returned by
repr() using \x, \u or \U escapes. This generates a string similar
to that returned by repr() in Python 2.
I don't think it makes sense to try detecting when the rendered representation of two different strings are same. Better to add repr=False to Should Be Equal and possibly to other keywords to control using repr() (or ascii() in Python 3). This is proposed by #2815.
Activity
HelioGuilherme66 commentedon Apr 12, 2018
Here is a test case that shows the problem:
pekkaklarck commentedon Apr 12, 2018
This is similar issue than we had when comparing values that are different have same string representation, most notably numbers and strings (#817). We should probably use
repr()when showing the error message if string values differ but their string representation is same.pekkaklarck commentedon Apr 12, 2018
This gets even more "fun" with Unicode equivalence, especially on Python 3 where
repr()doesn't show code points:I guess we needed to go through the individual code points to be able to construct a custom representation showing what these strings actually contain. This could be one way:
adrianyorke commentedon Apr 19, 2018
Python 3.6.5 (win32) is showing the code points also when using

repr():pekkaklarck commentedon Apr 20, 2018
It seems the Windows console just doesn't handle the NFD for in general. See how
print(a, b)also yieldsä a"when it should produceä ä.Also, submitted an issue about
rerpto Python's issue tracker: https://bugs.python.org/issue33317pekkaklarck commentedon Apr 20, 2018
Handling the original problem with null bytes isn't that straightforward either. With items of different types (like string
'1'and integer1) we add the type information if string representations are same, but that doesn't work with strings containing null bytes. If we have'foo'and'foo\x00', their string representations differ in Python, but the actual value shown to the user is identical. To be able to show to the user that the other has null bytes, we basically needed to explicitly check are null bytes present. The problem with this approach is that there are also other invisible characters that would need similar special handling.pekkaklarck commentedon Apr 20, 2018
Solving this so that we'd be always able to show how values differ when they look the same may just be too complicated. A simple workaround would be adding argument like
repr=Falseto at least toShould Be EqualandShould Be Equal As Strings. This would turn error reporting to userepr()internally regardless the values. Having a customrepr()that handles also different Unicode normal forms (NFC and NFD) in Python 3 would still be useful.pekkaklarck commentedon Apr 20, 2018
It was pointed out in the comments of the Python issue I submitted about
repr()that in Python 3 we can useascii(text)to get the same behavior asrepr(text)had in Python 2. Much better than my earlier idea to usetext.encode('unicode_escape').decode('ASCII').Also, the more I think about this the more I believe that adding
repr=Falseto keywords is better than trying to show this kind of differences automatically. Under Python 3 we should useascii(), notrepr(), though. I believereprwould still be a good argument name because we use that also withLog.adrianyorke commentedon Apr 20, 2018
Thanks for this. That was a good tip from the Python development team. Here's the description returned by
help(ascii):pekkaklarck commentedon Apr 24, 2018
I don't think it makes sense to try detecting when the rendered representation of two different strings are same. Better to add
repr=FalsetoShould Be Equaland possibly to other keywords to control usingrepr()(orascii()in Python 3). This is proposed by #2815.