How to test Python readline completion?

帅比萌擦擦* 提交于 2020-01-01 08:33:07

问题


I'm writing a command-line interface in Python. It uses the readline module to provide command history and completion.

While everything works fine in interactive mode, I'd like to run automated tests on the completion feature. My naive first try involved using a file for standard input:

my_app < command.file

The command file contained a tab, in the hopes that it would invoke the completion feature. No luck. What's the right way to do the testing?


回答1:


For this I would use Pexpect (Python version of Expect). The readline library needs to be speaking to a terminal to do interactive tab-completion and such—it can't do this if it is only getting one-way input from a redirected file.

Pexpect works for this because it creates a pseudo terminal, which consists of two parts: the slave, where the program you are testing runs, and the master, where the Python pexpect code runs. The pexpect code emulates the human running the test program. It is responsible for sending characters to the slave, including characters such as newline and tab, and reacting to the expected output (this is where the phrase "expect" comes from).

See the program ftp.py from the examples directory for a good example of how you would control your test program from within expect. Here is a sample of the code:

child = pexpect.spawn('ftp ftp.openbsd.org')
child.expect('(?i)name .*: ')
child.sendline('anonymous')
child.expect('(?i)password')
child.sendline('pexpect@sourceforge.net')
child.expect('ftp> ')



回答2:


rlcompleter might accomplish what you want

From the documentation:

The rlcompleter module is designed for use with Python’s interactive mode. A user can add the following lines to his or her initialization file (identified by the PYTHONSTARTUP environment variable) to get automatic Tab completion:

try:
    import readline
except ImportError:
    print "Module readline not available."
else:
    import rlcompleter
    readline.parse_and_bind("tab: complete")

https://docs.python.org/2/library/rlcompleter.html




回答3:


Check out ScriptTest:

from scripttest import TestFileEnvironment
env = TestFileEnvironment('./scratch')

def test_script():
    env.reset()
    result = env.run('do_awesome_thing testfile --with extra_win --file %s' % filename)

And play around with passing the arguments as you please.




回答4:


You can try using Sikuli to test the end-user interaction with your application.

However, this is a complete overkill, requires a lot of extra dependencies, will work slowly, and will fail if the terminal font/colors change. But, still, you will be able to test actual user interaction.

The documentation homepage links to a slideshow and a FAQ question about writing tests using Sikuli.



来源:https://stackoverflow.com/questions/22795767/how-to-test-python-readline-completion

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!