【发布时间】:2015-04-26 10:18:16
【问题描述】:
我在 PyCharm 社区 3.4.1 中使用 Python 2.7 和 unittest2。我需要将 CLI 命令的文本输出与字符串的内容进行匹配以进行自动化测试。
此命令的输出经常在行尾有尾随空格;如果我在用于存储预期文本的 heredoc 的行尾添加空格,它们会在编辑器中神秘地被删除并且不会进入文件。为了解决这个问题,我不得不拆分我的heredoc并用空格重新组合它;这很丑陋,但这是我让它工作的唯一方法。
我试过谷歌搜索和搜索解释,但没有找到;我怀疑这可能是 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 错误的方法
【问题讨论】:
标签: python pycharm multiline heredoc