Auty自动化测试框架第五篇——框架内部的调用支持、自动化安装库与配置说明

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

本次对Auty自动化测试框架做些收尾工作,由于在scripts文件夹中的脚本会需要调用其他包结构文件夹中的脚本,所以这里需要添加一下框架对于内部脚本间互相调用的支持,这里需要动态的在脚本运行时为脚本添加四行内容:

# -*- coding: utf-8 -*-
import os
import sys
sys.path.append("'+Auty的根目录路径+'")

并且在脚本运行完成后删除这四行。对应要修改execute_selection.py文件:

# -*- coding: utf-8 -*-
from .read_selection import read_selection
import os
import time
from .exe_deco import exe_deco
from .write_log import write_log
from utils.utils import str_2_tuple
from utils.utils import get_local_time
from utils.utils import get_specific_time
from generate_result import generate_result

def execute_selection():
    selection = read_selection()
    genTime = get_local_time()
    resultFileName = genTime+' test_result.csv'
    autyPath = os.getcwd()
    #Save the auty path into file.
    pathFilePath = os.path.join(autyPath,'utils','root_path.py')
    writeContent = '# -*- coding: utf-8 -*-\n'+'autyPath=\''+autyPath+'\''
    open(pathFilePath,'w').write(writeContent)
    #Result generation.
    resultFilePath = os.path.join(autyPath,'results',resultFileName)
    generate_result(resultFilePath,('scriptPath','detail','startTime','endTime','duration'))
    for scriptPath in selection:
        result = str_2_tuple(scriptPath)
        startTime = get_specific_time()
        ret,result2 = execute_script(scriptPath,autyPath)
        endTime = get_specific_time()
        duration = (endTime-startTime).microseconds*0.000001
        result = result+result2+str_2_tuple(startTime)+str_2_tuple(endTime)+str_2_tuple(duration)
        generate_result(resultFilePath,result)

@exe_deco
def execute_script(scriptPath,autyPath):
    #Auto-created code.
    with open(scriptPath,'r') as original:
        data = original.read()
    with open(scriptPath,'w') as modified:
        autyCode = '# -*- coding: utf-8 -*-\nimport os\nimport sys\nsys.path.append("'+autyPath+'")\n'
        modified.write(autyCode+data)
    #Execute script.
    write_log('execute_script: '+scriptPath)
    os.system('python '+scriptPath)
    #Auto-deleted code.
    lines = open(scriptPath).readlines()
    del lines[3]
    del lines[2]
    del lines[1]
    del lines[0]
    open(scriptPath,'w').writelines(lines)

其中我们把autyPath变量的值(Auty根目录路径)保存到了utils目录下的root_path.py文件中,以便以后的工作中动态的获取与调用根目录的值。

至此就可以在我们的测试脚本中调用其他包结构文件夹中的脚本啦~例如(注意第二行和第五行,我们可以从utils文件夹中引用脚本了):

接下来为框架添加自动化安装库功能,写一下config.txt文件,对框架功能进行简单说明。在Auty中编写setup.py文件,用来自动化安装requirements文件中所配置的python库(可以通过在当前目录执行pip freeze-> requirements命令自动生成该文件,将会包含你目前环境已经安装过的python库版本信息):

为setup.py文件添加内容如下:

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

if __name__ == '__main__':
    installFile = os.path.join(os.getcwd(),'requirements')
    os.system('/usr/local/bin/pip install -r '+installFile)

运行setup.py文件,自动化安装库:

至此我的Auty框架已经搭建完成,后续功能可根据工作需要陆续进行完善。现在写一下config.txt文件:

特别感谢大师兄:朱勃给予的指点和帮助~

 

 

 
 
 
 
posted @ 2016-09-29 18:12  天外归云  阅读(1268)  评论(0编辑  收藏  举报