我正在使用Python 3和cmd模块构建一个交互式shell。我已经用py.test编写了简单的单元测试来测试单个函数,比如do_*函数。我希望创建更全面的测试,通过模拟用户的输入与shell本身进行实际交互。例如,如何测试以下模拟会话:
bash$ console-app.py
md:> show options
Available Options:
------------------
HOST The IP address or hostname of the machine to interact with
PORT The TCP port number of the server on the HOST
md:> set HOST localhost
HOST => 'localhost'
md:> set PORT 2222
PORT => '2222'
md:>发布于 2015-05-06 07:46:38
您可以通过mock input或传递给cmd的输入流注入用户输入,但我发现通过onecmd() Cmd API方法对其进行更简单和灵活的测试,并信任Cmd读取输入的方式。这样,您就不关心Cmd是如何通过用户命令直接完成脏工作和测试的:我通过控制台和套接字使用cmd,这一点我不关心流来自何处。
此外,我还使用onecmd()来测试甚至do_* (偶尔还有help_*)方法,并使测试更少地耦合到代码中。
下面是我如何使用它的一个简单示例。create()和_last_write()是分别构建MyCLI实例和获取最后输出行的辅助方法。
from mymodule import MyCLI
from unittest.mock import create_autospec
class TestMyCLI(unittest.TestCase):
def setUp(self):
self.mock_stdin = create_autospec(sys.stdin)
self.mock_stdout = create_autospec(sys.stdout)
def create(self, server=None):
return MyCLI(stdin=self.mock_stdin, stdout=self.mock_stdout)
def _last_write(self, nr=None):
""":return: last `n` output lines"""
if nr is None:
return self.mock_stdout.write.call_args[0][0]
return "".join(map(lambda c: c[0][0], self.mock_stdout.write.call_args_list[-nr:]))
def test_active(self):
"""Tesing `active` command"""
cli = self.create()
self.assertFalse(cli.onecmd("active"))
self.assertTrue(self.mock_stdout.flush.called)
self.assertEqual("Autogain active=False\n", self._last_write())
self.mock_stdout.reset_mock()
self.assertFalse(cli.onecmd("active TRue"))
self.assertTrue(self.mock_stdout.flush.called)
self.assertEqual("Autogain active=True\n", self._last_write())
self.assertFalse(cli.onecmd("active 0"))
self.assertTrue(self.mock_stdout.flush.called)
self.assertEqual("Autogain active=False\n", self._last_write())
def test_exit(self):
"""exit command"""
cli = self.create()
self.assertTrue(cli.onecmd("exit"))
self.assertEqual("Goodbay\n", self._last_write())如果cli应该终止,请注意onecmd()返回True,否则False返回。
https://stackoverflow.com/questions/30056986
复制相似问题