- 需求:
自动化测试中,需要操作多台硬件设备(通过Telnet连接)。
- 需求分解:
- 在测试CASE中,支持连接多个Telnet.
- 记录测试log要清晰
- 能够获取到某次expect到的行之前的log(如输入ls命令, expect到命令提示符后,希望可以获取到ls产生的详细内容做二次分析)
- 能获取到expect到的行的log内容
- 解决方案
上述需求,单纯用telnetlib库,需要额外做较多的工作,才能满足。刚好最近有个测试项目有这样的需求,封装起来给需要的人提供便利和参考。测试开发人员可以把更多的精力放在CASE的业务逻辑设计上。
移步码云:
TelnetLibForTester: 基于Telnetlib库封装的库。方便测试开发工程师,快速方便的控制多台telnet设备,同时可以自动抓取测试log。节省CASE开发时间。
- 使用示例
from custom_telnet import CustomTelnet
def login(client, name, pwd):
if client.custom_expect(['login']):
client.custom_write(name + b'\n')
if client.custom_expect(['Password']):
client.custom_write(pwd + b'\n')
if client.custom_expect([':~#39;]):
return True
return False
if __name__ == '__main__':
'''
测试CASE:
client1 和 client2 连接,login
client1输入ls后,expect到指定log后,client2做操作ls
'''
client_1 = CustomTelnet('10.10.10.10', 23)
client_2 = CustomTelnet('10.10.10.11', 23)
if not login(client_1, b'user1', b'hello'):
raise Exception('client1 login fail')
if not login(client_2, b'user2', b'hello1'):
raise Exception('client2 login fail')
client_1.flush_log()
client_2.flush_log()
client_1.custom_write(b'ls\n')
if client_1.custom_expect([':~#39;]):
print(client_1.match) # client_1.match 匹配到的那一行的内容
for _ in client_1.before: # client_1.before flush_log()时刻之后,expect到的log之前的log内容,是一个String数组,每一个数组元素代表一行log
if '测试客户端安装流程图' in _:
print(_)
client_2.custom_write(b'ls\n')
client_2.custom_expect([':~#39;])
for tmp in client_2.before:
if 'anaconda3' in tmp:
print(tmp)
# print(client_2.before)
- LOG示例
2021-07-24 23:00:40,033 # Ubuntu 16.04.7 LTS
2021-07-24 23:00:40,543 # user1 login:
2021-07-24 23:00:40,624 # => user1
2021-07-24 23:00:41,051 # Password:
2021-07-24 23:00:41,066 # => hello
2021-07-24 23:00:41,084 #
2021-07-24 23:00:41,141 # Last login: Sat Jul 24 23:00:03 CST 2021 from user1-lt.com on pts/22
2021-07-24 23:00:41,221 # Welcome to Ubuntu 16.04.7 LTS (GNU/Linux 4.15.0-117-generic x86_64)
2021-07-24 23:00:41,221 #
2021-07-24 23:00:41,222 # * Documentation: https://help.ubuntu.com
2021-07-24 23:00:41,222 # * Management: https://landscape.canonical.com
2021-07-24 23:00:41,223 # * Support: https://ubuntu.com/advantage
2021-07-24 23:00:41,223 #
2021-07-24 23:00:41,224 # 294 个可升级软件包。
2021-07-24 23:00:41,224 # 232 个安全更新。
2021-07-24 23:00:41,224 #
2021-07-24 23:00:41,225 # New release '18.04.5 LTS' available.
2021-07-24 23:00:41,226 # Run 'do-release-upgrade' to upgrade to it.
2021-07-24 23:00:41,226 #
2021-07-24 23:00:41,738 # user1@user1:~$
2021-07-24 23:00:42,950 # => ls
2021-07-24 23:00:42,962 # ls
2021-07-24 23:00:42,974 # 123.zip lava2_docker
2021-07-24 23:00:42,975 # 1.txt learncode
2021-07-24 23:00:42,975 # 1.txt.1 liteide
2021-07-24 23:00:42,999 # IMG_20181112_095712.jpg 测试客户端安装流程图.dia
2021-07-24 23:00:43,000 # index.html 公共的
2021-07-24 23:00:43,000 # IOT_CASE_LOG_KEY_WORD.xlsx 模板
2021-07-24 23:00:43,003 # lava 音乐
2021-07-24 23:00:43,003 # lava2-0227 桌面
2021-07-24 23:00:43,505 # user1@user1:~$
本文暂时没有评论,来添加一个吧(●'◡'●)