MisterY

There are always people or things that need to be remembered

0%

自动化测试框架介绍

一、自动化环境搭建

环境安装资源获取: 10.190.67.105 root/123456 /home/package_shared/业务软件自动化.zip

自动化环境安装步骤:

  1. 安装python-3.6.5-amd64.exe
  2. 替换Lib.rar中内容到python安装目录(注意:`Lib.rar中有一个site-package.rar也要替换)
  3. 解压FOS_Python.rar到任意目录,并拷贝Run_List.txt本地运行调试python脚本.pyFOS_PYTHON目录下
  4. 安装python脚本开发IDE PyCharm.zip(可选)

二、自动化脚本目录结构

FOS_PYTHON 自动化框架根目录
- arp 功能模块目录
— ARP_FUNC_001_1DUT_standard 场景模型(单设备)
—- DEV_022062_ARP.PY 自动化用例脚本
—- DEV_022062_ARP 脚本同名文件夹(存放脚本执行需要的初始化配置命令行文件,以及配置恢复命令行文件)
- flexe 功能模块目录
— FlexE_FUNC_001_1DUT_standard 场景模型(单设备)
—- flexe_test.py 自动化用例脚本
- 本地运行调试python脚本.py 自动化本地执行入口
- Run_List.txt 需要执行的脚本路径组(相对路径格式)
- Testbed.ini 自动化设备变量初始化配置文件。

三、自动化脚本编写介绍

3.1 Testbed.ini文件介绍

如下图,该文件定义了一个测试拓扑,包括设备类型,登录设备ip,测试中用到的端口等参数变量。
变量名和变量值之间由“,”分割

image-20211201220059337

3.2 自动化用例样例

image-20211201215539148

image-20211201215925917

用例中下发配置和检查配置实现,通过在site-package文件夹新增一个功能对应模块的库文件FlexE_lib.py,并将其在Public.py文件中导入。

image-20211201221246693

3.3 功能模块库文件样例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import re
import time


class FlexE():

def __init__(self, dut):
self.dut = dut

def check_flexe_client(self, clientid, phyname, timeslot, n=3):

flag = False
if clientid == 'all':
clientid = 'brief'

phyname = [self.dut.gldict[name] if name in self.dut.gldict.keys() else name for name in phyname]

try:
for _ in range(n):
if flag:
break
self.flexe_client_table = []
flexeclientstate = self.dut.send_cmd(f'show flexe-client {clientid}')
value = flexeclientstate.split('\n')
client_item = {}
for line in value:
if 'Flexeclient Name' in line:
if (len(client_item.items()) != 0):
self.flexe_client_table.append(client_item)
res = re.findall('\w+ ?\w+: *[\w\-/]* ?[\w\-/]*\w', line)
for item in res:
key, value = item.split(':')
client_item[key.strip()] = value.strip()
elif 'Client Timeslot' in line:
client_item['Timeslot'] = []
elif 'Phy Name' in line:
res = re.findall('\w+ ?\w+: *[\w\-/,]* ?[\w\-/,]*\w', line)
timeslotinfo = {}
for item in res:
key, value = item.split(':')
timeslotinfo[key.strip()] = value.strip()
client_item['Timeslot'].append(timeslotinfo)
elif self.dut.prompt in line:
break

if (len(client_item.items()) != 0):
self.flexe_client_table.append(client_item)

if len(self.flexe_client_table) == 0:
self.dut.sys_log('无flexe client表项, 继续检查')
time.sleep(10)
continue

for phy, slot in zip(phyname, timeslot):
for clientinfo in self.flexe_client_table:
for phydict in clientinfo['Timeslot']:
if (phy == phydict['Phy Name']) and (slot == phydict['Timeslot']):
flag = True
break
else:
flag = False

if flag:
break

if not flag:
self.dut.sys_log(
f"{'flexe-phy:' if phyname else ''} {phyname if phyname else ''}, {'timeslot:' if timeslot else ''} {timeslot if timeslot else ''} 检查异常,继续检查"
)
time.sleep(10)
else:
break

except Exception as e:
self.dut.sys_log(e)

finally:
return flag

def flexe_client_proc(self, phyports, timeslots, groupid, clientid, mode=1):
flag = False
phyports = [self.dut.gldict[name] if name in self.dut.gldict.keys() else name for name in phyports]
if mode == 1:
self.dut.send_cmd(f'config terminal\n')
for port in phyports:
rs1 = self.dut.send_cmd(f'interface {port} \nexit', )

rs1 = self.dut.send_cmd(f'interface flexe-group {groupid}')
# if 'Commit Success.' in rs1:
# flag = (True and flag)
# else:
# flag = (False and flag)

for index, port in enumerate(phyports, 1):
self.dut.send_cmd(f'bind {port} phy-num {index}')

self.dut.send_cmd(f'exit')

self.dut.send_cmd(f'interface flexe-client {clientid}\n' f'timeslot {timeslots}\n' f'commit\n' f'withdraw')
elif mode == 2:
self.dut.send_cmd(f'config terminal\n'
f'interface flexe-client {clientid}\n'
f'no timeslot\n'
f'exit\n'
f'no interface flexe-client {clientid}')

self.dut.send_cmd(f'interface flexe-group {groupid}')

for port in phyports:
self.dut.send_cmd(f'no bind {port}')
self.dut.send_cmd(f'exit\nno interface flexe-group {groupid}')

for port in phyports:
self.dut.send_cmd(f'no interface {port}')
self.dut.send_cmd('commit')
return flag

3.4 自动化脚本调试运行

python脚本调试主要依赖IDE,pycharm、vscode等都支持断点调试功能,需要调试脚本时,在自动化脚本最末尾添加如下语句:

1
2
if __name__ == '__main__':
flexe_test() # 用例函数名

即可在IDE中选中脚本,执行debug操作。