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
The unittest module diff is missing/forgetting/not putting newline before + and ? for some inputs #79868
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee=Noneclosed_at=Nonecreated_at=<Date2019-01-08.21:57:48.034>labels= ['type-bug', 'tests', 'OS-windows']
title='The unittest module diff is missing/forgetting/not putting newline before + and ? for some inputs'updated_at=<Date2019-01-09.11:04:13.571>user='https://github.com/addonszz'
Also, if you can, please verify that you can reproduce the problem with a current version of Python 3. 3.7.2 is the current maintenance release and 3.6.8 was the final maintenance release of 3.6.x.
This seems to exist on master. Since they are multilines assertMultiLineEqual is used and is there something incorrect during the diff calculation using ndiff due to absence of '\n'? The current diff is calculated as below with difflib.ndiff and seems to produce incorrect output due to absence of newline. The last change was done for single string comparison with bpo-9174.
difflib.ndiff calculation done internally for the below reproducer
$ ./python.exe
Python 3.8.0a0 (heads/master:a234e14839, Jan 8 2019, 21:57:35)
[Clang 7.0.2 (clang-700.1.81)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import difflib
>>> print(''.join(difflib.ndiff(['a\n', 'b'], ['c\n', 'd'])))
- a
- b+ c
+ d
>>> print(''.join(difflib.ndiff(['a\n', 'b\n'], ['c\n', 'd\n']))) # Possible correct candidate?
- a
- b
+ c
+ d
A simpler reproducer
importunittestclassStdErrUnitTests(unittest.TestCase):
deftest_function_name(self):
expected="a\nb"actual="c\nd"self.assertEqual(expected, actual)
deftest_function_name_newlines_end(self):
expected="a\nb\n"actual="c\nd\n"self.assertEqual(expected, actual) # produces extra new line at the diff in the end with \d\nif__name__=='__main__':
unittest.main()
Searching further this seems to be reported earlier with bpo-24780 with caret displayed wrongly which was also due to newline missing. But the sample case reported here is also listed at one of the examples in https://bugs.python.org/issue24780#msg258466 . There is an open patch with review in the issue. I guess this is a duplicate of bpo-24780 then?
Activity
addonszz commentedon Jan 8, 2019
Create this program and run with
Python 3.6.3:### Actual output
### Expected output
### Differences between actual and expected output
This kind of bug frequently happens. On this case, it is not putting a new line before the
+. Other cases, it is not putting a new line before the?.zooba commentedon Jan 8, 2019
This doesn't appear to be Windows-specific or related to our test suite, so I updated the tags and added the unittest experts.
ned-deily commentedon Jan 8, 2019
Also, if you can, please verify that you can reproduce the problem with a current version of Python 3. 3.7.2 is the current maintenance release and 3.6.8 was the final maintenance release of 3.6.x.
tirkarthi commentedon Jan 9, 2019
This seems to exist on master. Since they are multilines assertMultiLineEqual is used and is there something incorrect during the diff calculation using ndiff due to absence of '\n'? The current diff is calculated as below with difflib.ndiff and seems to produce incorrect output due to absence of newline. The last change was done for single string comparison with bpo-9174.
difflib.ndiff calculation done internally for the below reproducer
A simpler reproducer
======================================================================
FAIL: test_function_name (main.StdErrUnitTests)
----------------------------------------------------------------------
Traceback (most recent call last): File "../backups/bpo35687_1.py", line 9, in test_function_name self.assertEqual(expected, actual) AssertionError: 'a\nb' != 'c\nd' - a - b+ c + d======================================================================
FAIL: test_function_name_newlines_end (main.StdErrUnitTests)
----------------------------------------------------------------------
Traceback (most recent call last): File "../backups/bpo35687_1.py", line 15, in test_function_name_newlines_end self.assertEqual(expected, actual) AssertionError: 'a\nb\n' != 'c\nd\n' - a - b + c + dRan 2 tests in 0.003s
FAILED (failures=2)
tirkarthi commentedon Jan 9, 2019
Searching further this seems to be reported earlier with bpo-24780 with caret displayed wrongly which was also due to newline missing. But the sample case reported here is also listed at one of the examples in https://bugs.python.org/issue24780#msg258466 . There is an open patch with review in the issue. I guess this is a duplicate of bpo-24780 then?
Related sample case at msg258466
======================================================================
FAIL: test_notrailingnewline_2 (main.AssertEqualTest)
----------------------------------------------------------------------
addonszz commentedon Jan 9, 2019
The issue is also reproducible, by directly using difflib:
It outputs:
But it should be: (still missing the new line on the same place as
assertEqualdoes)addonszz commentedon Jan 9, 2019
Outputs:
May be it should be:
addonszz commentedon Jan 9, 2019
I applied the patch from https://bugs.python.org/file44679 and it fixed the issue for this example using the unittest module:
evandrocoan commentedon Dec 9, 2022
Related to: #68968
maurycy commentedon Dec 6, 2026
Fixed in 46361bb (3.12; #103937):
cpython/Lib/test/test_unittest/test_case.py
Line 1322 in f9704be
cc @StanFromIreland