Skip to content

Enhancement: Error report for Should be equal keyword  #2806

Closed

Description

@ziakoo

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.

Activity

  1. HelioGuilherme66 commented on Apr 12, 2018

    @HelioGuilherme66
    Member

    Here is a test case that shows the problem:

    *** Test Cases ***
    Test null char
        ${date}=    Set Variable    2015-05-15\x00
        Log    ${date}
        Should Be Equal As Strings    2015-05-15    ${date}
    
  2. added this to the v3.1 milestone on Apr 12, 2018
  3. pekkaklarck commented on Apr 12, 2018

    @pekkaklarck
    Member

    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.

  4. pekkaklarck commented on Apr 12, 2018

    @pekkaklarck
    Member

    This gets even more "fun" with Unicode equivalence, especially on Python 3 where repr() doesn't show code points:

    >>> a = 'ä'
    >>> b = 'a\u0308'
    >>> a == b
    False
    >>> print(a, b)
    ä ä
    >>> print(repr(a), repr(b))
    'ä' 'ä'
    >>> print(len(a), len(b))
    1 2

    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:

    >>> print(b.encode('unicode_escape').decode('UTF-8'))
    a\u0308
  5. adrianyorke commented on Apr 19, 2018

    @adrianyorke
    Contributor

    Python 3.6.5 (win32) is showing the code points also when using repr():
    image

  6. pekkaklarck commented on Apr 20, 2018

    @pekkaklarck
    Member

    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 rerp to Python's issue tracker: https://bugs.python.org/issue33317

  7. pekkaklarck commented on Apr 20, 2018

    @pekkaklarck
    Member

    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.

  8. pekkaklarck commented on Apr 20, 2018

    @pekkaklarck
    Member

    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.

  9. pekkaklarck commented on Apr 20, 2018

    @pekkaklarck
    Member

    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.

  10. adrianyorke commented on Apr 20, 2018

    @adrianyorke
    Contributor

    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.

  11. pekkaklarck commented on Apr 24, 2018

    @pekkaklarck
    Member

    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.

  12. removed this from the v3.1 milestone on Apr 24, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions