琉璃小屋-如何批量执行脚本文件?

前言:

不同的业务功能点在不同的目录下积累了很多py文件,如何批量执行脚本文件?一个一个执行脚本,效率实在太低,借助python批量执行脚本文件 效率相当的给力

第一种

import os
def func(path):
if os.path.isfile(path) and path.endswith('.py'):
os.system('python %s' % path)
elif os.path.isdir(path):
for name in os.listdir(path):
abs_path = os.path.join(path,name)
print(abs_path)
if abs_path.endswith('.py'):
os.system('python %s' % abs_path)

批量的py文件路径

func(r'D:\python\WorkProject\xxx')

第二种

import unittest,time
import HTMLTestReportCN
from HTMLTestRunner import HTMLTestRunner

test_dir = "D:\pythonspace\WorkProjectIfallure\isvHello"
discover=unittest.defaultTestLoader.discover(test_dir,pattern='test_*.py')
if name=='main':
current_time=time.strftime('%Y-%m-%d %H-%M-%S')
filename = "D:\pythonspace\WorkProjectIfallure\isllo\report\ISVHello-" + current_time + "_result.html"
fp=open(filename,'wb')
#runner=HTMLTestRunner(stream=fp,title='ISVHello接口测试用例',description=u'环境:windows 10 浏览器:chrome HelloHello接口测试用例执行情况',verbosity=2)
runner = HTMLTestReportCN.HTMLTestRunner( stream=fp, title='I接口测试用例',description=u'环境:windows 10 浏览器:chrome Hello接口测试用例执行情况', verbosity=2,tester="琉璃小屋" )
runner.run(discover)
fp.close()

附上测试报告

第三种

pytest是什么?
pytest是一个非常成熟的全功能的Python测试框架
pip install pytest

allure高大上的测试报告

安装Allure
下载Allure的zip安装包,

解压到allure-commandline目录
进入bin目录,运行allure.bat
添加allure到环境变量PATH(\安装路径\allure-commandline\bin)

import pytest
import os
path = os.path.dirname(file)
path = path + "report/"
pytest.main(['-s', '-q', '--alluredir', 'testcase'])
os.system('allure serve report')

附上测试报告图

Allure集成Jenkins执行更方便,Jenkins开源的自动化服务器 构建测试部署 很方便!!!!

posted @ 2020-01-07 10:19  琉璃小屋-英菇  阅读(190)  评论(0编辑  收藏  举报