我想要为我的命令行接口使用Python ( prompt-toolkit,https://github.com/jonathanslenders/python-prompt-toolkit)创建单元测试。
示例代码:
from os import path
from prompt_toolkit import prompt
def csv():
csv_path = prompt('\nselect csv> ')
full_path = path.abspath(csv_path)
return full_path发布于 2016-08-20 00:13:07
您可以mock的提示电话。
app_file
from prompt_toolkit import prompt
def word():
result = prompt('type a word')
return resulttest_app_file
import unittest
from app import word
from mock import patch
class TestAnswer(unittest.TestCase):
def test_yes(self):
with patch('app.prompt', return_value='Python') as prompt:
self.assertEqual(word(), 'Python')
prompt.assert_called_once_with('type a word')
if __name__ == '__main__':
unittest.main()请注意,您应该模拟来自app.py的提示符,而不是来自app.py的提示,因为您希望从文件中截取调用。
如果您使用这个库从用户检索某些输入(作为GNU的纯Python ),那么可能90%的用例都需要:func:
.prompt函数。
正如method docstring所说:
从用户那里获取输入并返回。这是许多
prompt_toolkit功能的包装器,可以替代raw_input。(或.)
遵循项目中的Getting started:
>>> from prompt_toolkit import prompt
>>> answer = prompt('Give me some input: ')
Give me some input: Hello World
>>> print(answer)
'Hello World'
>>> type(answer)
<class 'str'>当prompt方法返回字符串类型时,您可以使用mock.return_value来模拟用户与应用程序的集成。
https://stackoverflow.com/questions/38975025
复制相似问题