文章目录
一、背景:
在自动化框架搭建&自动化用例运行稳定后,解下下集成到jenkins就比较重要的一步了,如果不能集成到jenkins当中自动跑定时任务,那前面的努力也就相当于白费了,本篇主要介绍,jenkins集成main()函数
二、介绍:
2.1、main()函数更新:
可以达到自动判断当前运行的系统,是Windows还是Linux,然后执行不同的逻辑
import pytest
from conf.settings import *
import os
import time
from utils.commons.base.clearreport import clearReports
from conf.host_config import *
import platform
import argparse
import warnings
from utils.commons.base.enviroments import read_environment
from utils.commons.base.copy_file import mycopyfile, Properties
from utils.commons.base.send_email import send_email
# 忽略告警
warnings.filterwarnings("ignore")
# 创建main.py文件,主函数执行框架用例并生成allure测试报告
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='environment')
parser.add_argument('--env', dest='env', type=str, help='Specifying environment Variables')
args = parser.parse_args()
system = platform.system()
os.system('pip3 install -r requirements.txt')
env_list = ['test', 'test1', 'test2', 'test3']
if args.env in env_list:
if system.lower() == "linux":
allure_results = f'{
BASE_DIR}/allure-results/'
if not os.path.exists(allure_results):
os.makedirs(allure_results)
pytest.main(['-s', '-v', f'--env={
args.env}', f'{
BASE_DIR}/conftest.py::test_start'])
pytest.main(
['-s', '-v',
'--durations=20', # 增加用例执行时间耗时排行榜
'--alluredir', allure_results]) # 生成xml
pro_path = f'{
BASE_DIR}/allure-results/'
dir_path = f"{
BASE_DIR}/test_datas/data_type/properties/linux/environment.properties"
p = Properties(dir_path, "utf-8")
p.set('environmentVersion', read_environment('env'))
mycopyfile(dir_path, pro_path)
send_email().send_message()
else:
# 按配置清除测试报告
clearReports().clear_reports()
file_name = os.path.basename(__file__).split('.')[0] # 获取档前的文件名称(不带后缀),作为存放
time_stamp = str(int(time.time())) # 用时间戳生成一串不重复的数字
html_path = f'{
BASE_DIR}/report/html/'
if not os.path.exists(html_path):
os.makedirs(html_path)
new_dir_name = str(file_name) + time_stamp # 生成测试报告文件名称
new_html_path = f'{
BASE_DIR}/report/html/' + new_dir_name # 生成html报告文件名称
os.mkdir(new_html_path)
print(f"当前的环境为:{
args.env}")
pytest.main(['-s', '-v', f'--env={
args.env}', f'{
BASE_DIR}/conftest.py::test_start'])
pytest.main(
['-s', '-v',
'--alluredir', f'{
BASE_DIR}/report/xml/' + new_dir_name]) # 生成xml
pro_path = f'{
BASE_DIR}/report/xml/' + new_dir_name + '/'
dir_path = f"{
BASE_DIR}/test_datas/data_type/properties/windows/environment.properties"
p = Properties(dir_path, "utf-8")
p.set('environmentVersion', read_environment('env'))
mycopyfile(dir_path, pro_path)
# 此时就在项目report文件下生成以:当前文件名称+时间戳生成的.xml和.html文件,在html下即可查看生成的allure报告文件
command_word = f'allure generate {
BASE_DIR}/report/xml/{
new_dir_name} -o {
BASE_DIR}/report/html/{
new_dir_name} --clean'
os.system(command_word) # 执行cmd命令,生成html
send_email().send_message()
# 清除根目录下含有:interface-autotest-pytest路径的文件
clearReports().clear_interface()
# 直接打开生成的allure报告
os.system(f'allure open {
BASE_DIR}/report/html/{
new_dir_name}')
else:
print(f"Your Linux argument environment Variables is {
args.env} not exit")
2.2、main()函数自定义传参:
2.2.1、自定义env参数:
import argparse
parser = argparse.ArgumentParser(description='environment')
parser.add_argument('--env', dest='env', type=str, help='Specifying environment Variables')
args = parser.parse_args()
# args.env就能接收到参数了
2.2.2、指定python版本执行:
此时打开控制台在:Termial中输入命令行参数 - py -3.1 main.py --env sit 就能执行了
2.3、jenkins指定配置:
2.3.1、 构建脚本shell
#!/bin/bash
echo “进入自动化项目构件中”
cd “当前项目目录”
rm -rf allure-results
python3 main.py --env ${env}
echo “自动化项目构建完成”
2.3.2、本地如何指定py版本构建:
打开控制台:Terminal
py -3.1 main.py --env test