zl程序教程

您现在的位置是:首页 >  其他

当前栏目

Auty自动化测试框架第六篇——垃圾代码回收、添加suite支持

测试框架自动化代码 支持 添加 垃圾 回收
2023-09-27 14:26:55 时间

[本文出自天外归云的博客园]

垃圾代码回收

添加脚本恢复机制,因为框架会自动生成一些代码,如果代码生成后出现问题导致代码没有正常删除掉,则会造成代码垃圾,在auty目录添加recovery.py文件:

# -*- coding: utf-8 -*-
import os
from lib.recovery_code import recovery_code

if __name__ == '__main__':
    autyPath = os.getcwd()
    #Scripts recovery.
    recovery_code(autyPath)

在lib中添加recovery_code.py文件:

# -*- coding: utf-8 -*-
import os
import time

def recovery_code(autyPath):
    for scriptPath in open(os.path.join(autyPath,'scripts','all_scripts_selection.txt')):
        scriptPath = scriptPath.strip('\n')
        lines = open(scriptPath).readlines()
        try:
            if ('utf' in lines[0]) and ('os' in lines[1]) and ('sys' in lines[2]) and ('sys.path.append' in lines[3]):
                print('Recovry:'+scriptPath)
                del lines[3]
                del lines[2]
                del lines[1]
                del lines[0]
                open(scriptPath,'w').writelines(lines)
        except Exception, e:
            raise
        else:
            pass
        finally:
            pass

运行recovery.py文件就可以回收脚本中自动生成的垃圾代码,需要注意的是,如果你自己在写脚本代码的时候第一行内容包含utf,第二行包含os,第三行包含sys,第四行包含sys.path.append,那就没办法了,也会被当做垃圾代码回收掉。所以自己写代码要注意一下,import的顺序打乱一下就可以了。

添加suite支持

在实际测试工作中,我们可能会有多个集合需要执行,这时就有分suite执行的必要。这里添加了selections文件夹,用来存放需要执行的suite文件,suite文件为txt类型,里面包含了需要执行的脚本路径:

而原来scripts目录下的create_selection.py文件用来生成all_scripts_selection.txt文件,将脚本中的selection.txt改为all_scripts_selection.txt即可。接下来要修改read_selection.py文件:

# -*- coding: utf-8 -*-
import sys
import os

def read_selection():
    path = os.path.abspath(os.path.dirname(__file__))
    parentPath = os.path.dirname(path)
    selectionFolderPath = os.path.join(parentPath,'scripts','selections')
    selectionFilePaths = []
    for filePath in os.walk(selectionFolderPath):
        for thePath in filePath[2]:
            selectionFilePaths.append(os.path.join(selectionFolderPath,thePath))
    selection = []
    for selectionFilePath in selectionFilePaths:
        for line in open(selectionFilePath):
            selection.append(line.replace('\n',''))
    return selection

修改后的逻辑是从selections文件夹中读取suites文件,获取并返回其中包含的脚本文件路径。