基于Python的cmd模块创建交互式shell的自动化测试

doz*_*oza 6 python tdd pytest python-3.x python-cmd

我正在使用 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:>
Run Code Online (Sandbox Code Playgroud)

Mic*_*ico 5

您可以mock input将输入流传递给 cmd 来注入用户输入,但我发现通过onecmd() CmdAPI 方法更简单、更灵活地测试它,并信任如何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())
Run Code Online (Sandbox Code Playgroud)

如果您的 cli 应终止,请注意onecmd()返回,否则。TrueFalse