Ulysses
Ulysses

Reputation: 6025

Python unit-test assertions are being joined by newlines

While using unittest.TestCase.run(test_class(test)), the correct errors are being reported but they are being joined by \n.

AssertionError: False is not true : Failures: [],  Errors: [(<module1 testMethod=method1>, 'Traceback (most recent call last):\n  File "<file_name>", line 20, in method1\n    \'resource_partitions\')\n  File "error_source_file_path", line 23, in error_function\n    error_line_statement\nKeyError: \'gen_data\'\n')]

How can these be removed and replaced with actual newlines instead?

Has it got something to do with line-endings on my machine (currently set to \n)

Upvotes: 0

Views: 736

Answers (1)

h4z3
h4z3

Reputation: 5478

That is an intended behaviour.

The string is being displayed as a part an object. Such displays always print escape sequences rather to convert them to their specific char.

Look in this short example with string in interpreter:

>>> "spam\neggs"
'spam\neggs'
>>> print("spam\neggs")
spam
eggs

The first one gets displayed because it's an interactive console, in normal code it would never happen. But that's also how string in other object behave.

Printing list containing a string vs printing each element separately:

>>> print(["spam\neggs"])
['spam\neggs']
>>> for element in ["spam\neggs"]: print(element)
... 
spam
eggs

Upvotes: 1

Related Questions