Jenkins integrates main() function & custom parameter passing

1. Background:

After the automation framework is built & the automation use cases run stably, it is an important step to integrate into jenkins. If it cannot be integrated into jenkins to automatically run scheduled tasks, then the previous efforts will be in vain. This article mainly introduces , jenkins integrates the main() function

2. Introduction:

2.1, main() function update:

It can automatically judge whether the currently running system is Windows or Linux, and then execute different logic

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() function custom parameters:

2.2.1. Custom env parameters:

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. Specify the python version to execute:

At this time, open the console and enter the command line parameters in Termial - py -3.1 main.py --env sit to execute

2.3, jenkins specified configuration:

insert image description here

2.3.1. Build script shell

#!/bin/bash
echo "Enter automation project components"
cd "Current project directory"
rm -rf allure-results
python3 main.py --env ${env}
echo "Automation project build completed"

2.3.2. How to specify the py version to build locally:

Open the console: Terminal
py -3.1 main.py --env test

Guess you like

Origin blog.csdn.net/weixin_52358204/article/details/127927983