测试repls的对话类
dialogue-tester的Python项目详细描述
对话类
Dialogue
类帮助测试repls(带有read eval print循环的交互式解释器)。
给定一个多行交互式会话脚本,一个Dialogue
实例通过提供一个fake_input
方法来模拟用户交互,该方法可以替换python 3中的input
内置函数。
如何使用
您可以编写repl函数以接受输入函数作为可选参数,也可以对python的内置input
进行monkey补丁。无论哪种方式,您都可以用绑定到Dialogue
实例的fake_input
方法替换标准的input
。每次调用该方法时,该方法都会从会话脚本中打印下一个提示和假用户输入,然后将相同的用户输入字符串返回给repl进行处理。
Dialogue.session
instance属性保存一个多行字符串,其中包含所有提示、用户输入和测试中repl的预期输出。这用于assert
测试用例。
请参阅dialogue_test.py
模块,以获取两个简单的repl,它们运行Dialogue
类。
Note: the
.session
attribute is normalized with thedialogue.normalize()
function. Use that function to normalize the output captured by your testing framework as well. This normalization removes trailing whitespace from each line, preserving only the newlines separating them, and also removes leading and trailing whitespace from the session as a whole. This avoids many issues with linters and IDEs complaining about trailing whitespace, and also makes it easier to edit the session examples in tests. See the tests in this package for use ofnormalize
in test assertions.