Python 单元测试断言被换行加入
Python unit-test assertions are being joined by newlines
使用 unittest.TestCase.run(test_class(test))
时,报告了正确的错误,但 \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')]
如何删除这些并用实际的换行符代替?
是否与我机器上的 line-endings 有关(目前设置为 \n
)
这是预期的行为。
字符串作为对象的一部分显示。这样的显示 总是 打印转义序列而不是将它们转换为特定的字符。
在解释器中查看这个带有字符串的简短示例:
>>> "spam\neggs"
'spam\neggs'
>>> print("spam\neggs")
spam
eggs
显示第一个是因为它是一个交互式控制台,在正常代码中它永远不会发生。但这也是其他对象中字符串的行为方式。
打印包含字符串的列表与分别打印每个元素:
>>> print(["spam\neggs"])
['spam\neggs']
>>> for element in ["spam\neggs"]: print(element)
...
spam
eggs
使用 unittest.TestCase.run(test_class(test))
时,报告了正确的错误,但 \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')]
如何删除这些并用实际的换行符代替?
是否与我机器上的 line-endings 有关(目前设置为 \n
)
这是预期的行为。
字符串作为对象的一部分显示。这样的显示 总是 打印转义序列而不是将它们转换为特定的字符。
在解释器中查看这个带有字符串的简短示例:
>>> "spam\neggs"
'spam\neggs'
>>> print("spam\neggs")
spam
eggs
显示第一个是因为它是一个交互式控制台,在正常代码中它永远不会发生。但这也是其他对象中字符串的行为方式。
打印包含字符串的列表与分别打印每个元素:
>>> print(["spam\neggs"])
['spam\neggs']
>>> for element in ["spam\neggs"]: print(element)
...
spam
eggs