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

I am building an interactive shell using Python 3 and the cmd module. I have already written simple unit tests using py.test to test the individual functions, such as the do_* functions. I'd like to create more comprehensive tests that actually interact with the shell itself by simulating a user's input. For example, how could I test the following simulated session:

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:>

解决方案

You can mock input or input stream passed to cmd to inject user input but I find more simple and flexible test it by onecmd() Cmd API method and trust how Cmd read input. In this way you cannot care how Cmd do the dirty work and test directly by users command: I use cmd both by console and socket and this I cannot care where the stream come from.

Moreover I use onecmd() to test even do_* (and occasionally help_*) methods and make my test less coupled to the code.

Follow a simple example of how I use it. create() and _last_write() are helper methods to build a MyCLI instance and take the last output lines respectively.

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())

Take care that onecmd() return True if your cli should terminate, False otherwise.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值