1 / 9

使用 pexpect 实现命令行自动化测试

使用 pexpect 实现命令行自动化测试. 徐荣中 2015-03-06 Email : xurongzhong#126.com 个人博客: http://automationtesting.sinaapp.com/blog 微博: http://weibo.com/cizhenshi 一览社群: 679983 qq 群: 241450660 实地交流地址: http://www.yl1001.com/group_article/5191422951800241.htm. 一、分享内容概述. 介绍 安装 快速入门 EOF 和 TIMEOUT 类介绍 实例.

meusebio
Download Presentation

使用 pexpect 实现命令行自动化测试

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 使用pexpect实现命令行自动化测试 徐荣中 2015-03-06 Email:xurongzhong#126.com 个人博客:http://automationtesting.sinaapp.com/blog 微博:http://weibo.com/cizhenshi 一览社群:679983 qq群:241450660 实地交流地址:http://www.yl1001.com/group_article/5191422951800241.htm

  2. 一、分享内容概述 • 介绍 • 安装 • 快速入门 • EOF和TIMEOUT • 类介绍 • 实例

  3. Pexpect介绍 • Pexpect allows easy control of interactive console applications. • Pexpect is a pure Python module for spawning child applications; controlling them; and responding to expected patterns in their output. Pexpect works like Don Libes’ Expect. Pexpect allows your script to spawn a child application and control it as if a human were typing commands. • Pexpect can be used for automating interactive applications such as ssh, ftp, passwd, telnet, etc. It can be used to a automate setup scripts for duplicating software package installations on different servers. It can be used for automated software testing. • It should work on any platform that supports the standard Python pty module. The Pexpect interface was designed to be easy to use. 参考资料: https://pypi.python.org/pypi/pexpect

  4. Pexpect安装 • pip install pexpect 或 easy_install pexpect • 要求: • This version of Pexpect(3.3) requires Python 2.6 or 3.2 or above. • Pexpect only works on POSIX systems, where the pty module is present in the standard library. It may be possible to run it on Windows using Cygwin. 参考资料: http://pexpect.readthedocs.org/en/latest/install.html

  5. Pexpect快速入门 • import pexpect • child = pexpect.spawn('ftp ftp.openbsd.org') • child.expect('Name .*: ') • child.sendline('anonymous') • child.expect('Password:') • child.sendline('http://automationtesting.sinaapp.com') • child.expect('ftp> ') • child.sendline('lcd /tmp') • child.expect('ftp> ') • child.sendline('cd pub/OpenBSD') • child.expect('ftp> ') • child.sendline('get README') • child.expect('ftp> ') • child.sendline('bye') • 两个重要方法: expect() and send() (or sendline() ) • 信息输出:before 包含预期字符串之前的信息,after包含匹配模式。 参考资料: http://pexpect.readthedocs.org/en/latest/overview.html

  6. 常量EOF和TIMEOUT • End Of File (EOF):子进程退出时会生成EOF异常。相关信息存储在before中。 • child.expect('password:') • child.sendline(my_secret_password) • # We expect any of these three patterns... • i = child.expect (['Permission denied', 'Terminal type', '[#\$] ']) • if i==0: • print('Permission denied on host. Can't login') • child.kill(0) • elif i==2: • print('Login OK... need to send terminal type.') • child.sendline('vt100') • child.expect('[#\$] ') • elif i==3: • print('Login OK.') • print('Shell command prompt', child.after) • Timeout condition (TIMEOUT):默认为30秒,超时时生成TIMEOUT异常。 • # Wait no more than 2 minutes (120 seconds) for password prompt. • child.expect('password:', timeout=120) 参考资料: http://pexpect.readthedocs.org/en/latest/overview.html

  7. 类介绍 • 核心类。 • spawn :This is the main class interface for Pexpect. Use this class to start and control child applications. • run function:This function runs the given command; waits for it to finish; then returns all output as a string. • Exceptions:pexpect.EOF pexpect.TIMEOUT pexpect.ExceptionPexpect • Utility functions:pexpect.which pexpect.split_command_line • fdpexpect :增加了文件支持 • Replwrap:处理交互式shell • Pxssh:控制ssh。 • screen :ANSI终端仿真 • ANSI:ANSI (VT100)终端仿真 参考资料: http://pexpect.readthedocs.org/en/latest/api/index.html

  8. 实例 • 官方实例 • topip.py:This runs netstat on a local or remote server. • hive.py:This script creates SSH connections to a list of hosts that you provide. • script.py:This implements a command similar to the classic BSD “script” command • ftp.py:This demonstrates an FTP “bookmark”. • monitor.py:This runs a sequence of commands on a remote host using SSH. • passmass.py:This will login to each given server and change the password of the given user. • python.py:This starts the python interpreter and prints the greeting message backwards. • ssh_tunnel.py: This starts an SSH tunnel to a remote machine. • uptime.py: This will run the uptime command and parse the output into variables. • 其他实例: • linux 的yum • 某搜索引擎监控脚本 • 使用python远程操作linux服务器 :http://automationtesting.sinaapp.com/blog/python_code_linux_control 参考资料: http://www.ibm.com/developerworks/cn/linux/l-cn-pexpect2/ http://www.ibm.com/developerworks/cn/linux/l-cn-pexpect1/

  9. Q & A

More Related