Skip to content

UnicodeDecodeError from pytest if object representation contains non-ascii, utf8 encoded, characters #877

Closed
@magopian

Description

@magopian

Given the following test code:

    def test_foobar(self):
        class Foo(object):
            name = u'é'

            def __repr__(self):
                return self.name.encode('utf8')

        foo = Foo()
        assert not foo

Running it gives

Traceback (most recent call last):
  File "/Users/mathieu/olympia/apps/addons/tests/test_models.py", line 529, in test_foobar
    assert not foo
  File "/Users/mathieu/.virtualenvs/olympia/lib/python2.7/site-packages/_pytest/assertion/util.py", line 33, in format_explanation
    return u('\n').join(result)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 11: ordinal not in range(128)

result is ['assert not \xc3\xa9']. I believe that python's default encoding used to decode from str to unicode is ascii, which fails while decoding the utf-8 encoded object representation.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions