Skip to content

trailing newline in str comparison is hard to spot #324

@jugmac00

Description

@jugmac00

testtools

$ python test_diff.py 
F
======================================================================
FAIL: test_newline (__main__.TestNewline)
__main__.TestNewline.test_newline
----------------------------------------------------------------------
testtools.testresult.real._StringException: Traceback (most recent call last):
  File "test_diff.py", line 11, in test_newline
    self.assertEqual(
  File "/tmp/venv/lib/python3.8/site-packages/testtools/testcase.py", line 393, in assertEqual
    self.assertThat(observed, matcher, message)
  File "/tmp/venv/lib/python3.8/site-packages/testtools/testcase.py", line 480, in assertThat
    raise mismatch_error
testtools.matchers._impl.MismatchError: !=:
reference = '''\
INFO Requesting CI build for  972c6d2dc6dd5efdad1377c0d224e03eb8f276f7 on focal/amd64
'''
actual    = 'INFO Requesting CI build for  972c6d2dc6dd5efdad1377c0d224e03eb8f276f7 on focal/amd64'

unittest

$ python test_diff.py 
F
======================================================================
FAIL: test_newline (__main__.TestNewline)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "test_diff.py", line 11, in test_newline
    "INFO Requesting CI build for  972c6d2dc6dd5efdad1377c0d224e03eb8f276f7 on focal/amd64"
AssertionError: 'INFO[21 chars]for  972c6d2dc6dd5efdad1377c0d224e03eb8f276f7 on focal/amd64\n' != 'INFO[21 chars]for  972c6d2dc6dd5efdad1377c0d224e03eb8f276f7 on focal/amd64'
- INFO Requesting CI build for  972c6d2dc6dd5efdad1377c0d224e03eb8f276f7 on focal/amd64
?                                                                                      -
+ INFO Requesting CI build for  972c6d2dc6dd5efdad1377c0d224e03eb8f276f7 on focal/amd64

pytest

$ pytest test_diff.py 
======================================== test session starts ========================================
platform linux -- Python 3.8.10, pytest-7.0.1, pluggy-1.0.0
rootdir: /tmp
collected 1 item                                                                                    

test_diff.py F                                                                                [100%]

============================================= FAILURES ==============================================
_____________________________________ TestNewline.test_newline ______________________________________

self = <test_diff.TestNewline testMethod=test_newline>

    def test_newline(self):
>       self.assertEqual(
            newline(),
            "INFO Requesting CI build for  972c6d2dc6dd5efdad1377c0d224e03eb8f276f7 on focal/amd64"
        )
E       AssertionError: 'INFO[21 chars]for  972c6d2dc6dd5efdad1377c0d224e03eb8f276f7 on focal/amd64\n' != 'INFO[21 chars]for  972c6d2dc6dd5efdad1377c0d224e03eb8f276f7 on focal/amd64'
E       - INFO Requesting CI build for  972c6d2dc6dd5efdad1377c0d224e03eb8f276f7 on focal/amd64
E       ?                                                                                      -
E       + INFO Requesting CI build for  972c6d2dc6dd5efdad1377c0d224e03eb8f276f7 on focal/amd64

test_diff.py:11: AssertionError

Both unittest and pytest indicate what the problem is, but testtools only shows a rather unfortunate multiline representation of the string.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions