我在PyCharm community3.4.1中使用python2.7和unittest2。我需要将CLI命令的文本输出与字符串的内容进行匹配,以便进行自动测试。在
这个命令的输出在行尾经常有尾随空格;如果我在用于存储预期文本的herdoc中的行尾添加空格,它们会在编辑器中神秘地被删除,而不会被放入文件中。为了解决这个问题,我不得不将heredoc拆分并用空格重新组合;这很难看,但这是我能让它工作的唯一方法。在
我试着在google上搜索解释,但没有找到任何解释;我怀疑可能是PyCharm的自动格式化变得混乱,并将其视为一行Python代码,这样可以安全地删除尾部空格。在
这是我的代码;它在unittest2类中:def test_help_command(self):
textgot = run_the_help_command()
partone = """
blah blaah blah blah blah
This line has a space at the end in the help output"""
parttwo = """
foo foo foo foo foo
This line also ends with a trailing space in the help output"""
partthree = """
baz baz baz
"""
# Recombine parts with spaces
helptext = partone + " " + parttwo + " " + partthree
self.assertMultiLineEqual(helptext, textgot, 'Help text')
欢迎提出以下建议:如何修复此问题,以便可以使用单个heredoc字符串而不是拆分?我有更复杂的例子来测试哪些特性会让这种方法非常恼人
其他比heredocs更好的存储字符串的方法
证明这是不是PyCharm bug的方法