# -*- coding: utf-8 -*- # @Version: Python 3.9 # @Time : 2023/1/9 17:09 # @Author : chenyinhua # @File : run.py # @Software: PyCharm # @Desc: 框架主入口 """ 说明: 1、用例创建原则,测试文件名必须以“test”开头,测试函数必须以“test”开头。 2、运行方式: > python run.py 默认在test环境运行测试用例, 生成allure测试报告 > python run.py -m demo 在test环境仅运行打了标记demo用例,生成allure测试报告 > python run.py -env live 在live环境运行测试用例 > python run.py -env=test 在test环境运行测试用例 > python run.py -report=no 在test环境下允许测试用例,不生成allure测试报告 pytest相关参数:以下也可通过pytest.ini配置 --reruns: 失败重跑次数 --reruns-delay 失败重跑间隔时间 --count: 重复执行次数 -v: 显示错误位置以及错误的详细信息 -s: 等价于 pytest --capture=no 可以捕获print函数的输出 -q: 简化输出信息 -m: 运行指定标签的测试用例 -x: 一旦错误,则停止运行 --cache-clear 清除pytest的缓存,包括测试结果缓存、抓取的fixture实例缓存和收集器信息缓存等 --maxfail: 设置最大失败次数,当超出这个阈值时,则不会在执行测试用例 "--reruns=3", "--reruns-delay=2" allure相关参数: –-alluredir这个选项用于指定存储测试结果的路径 """ # 标准库导入 import os import shutil # 第三方库导入 import pytest from loguru import logger import click # 本地应用/模块导入 from utils.files_utils.files_handle import load_yaml_file from utils.case_generate_utils.case_fun_generate import generate_cases from utils.report_utils.send_result_handle import send_result from settings import REPORT_DIR, LOG_DIR, ENV_DIR, ALLURE_RESULTS_DIR, ALLURE_HTML_DIR, AUTO_CASE_DIR, ALLURE_CONFIG_DIR from settings import LOG_LEVEL, GLOBAL_VARS, REPORT, RERUN, RERUN_DELAY, MAX_FAIL from utils.logger_utils.loguru_log import capture_logs from utils.report_utils.allure_handle import generate_allure_report from utils.report_utils.push_allure_report import push_allure_report # 主函数 @click.command() @click.option("-report", default="yes", help="是否生成allure html report,支持如下类型:yes, no") @click.option("-env", default="test", help="输入运行环境:test 或 live或者其他你配置的环境") @click.option("-m", default=None, help="选择需要运行的用例:python.ini配置的名称") def run(env, m, report): try: # ------------------------ 捕获日志---------------------------- capture_logs(level=LOG_LEVEL, filename=os.path.join(LOG_DIR, "service.log")) logger.info("""\n\n _ _ _ _____ _ __ _ _ __ (_) / \\ _ _| |_ __|_ _|__ ___| |_ / _` | "_ \\| | / _ \\| | | | __/ _ \\| |/ _ \\/ __| __| | (_| | |_) | |/ ___ \\ |_| | || (_) | | __/\\__ \\ |_ \\__,_| .__/|_/_/ \\_\\__,_|\\__\\___/|_|\\___||___/\\__| |_| Starting ... ... ... """) # ------------------------ 处理一下获取到的参数---------------------------- # # 根据指定的环境参数,将运行环境所需相关配置数据保存到GLOBAL_VARS env_file = os.path.join(ENV_DIR, f"{env}.yml") __env = load_yaml_file(env_file) GLOBAL_VARS.update(__env) # ------------------------ 自动生成测试用例 ------------------------ # 删除原有的测试用例,以便生成新的测试用例 if os.path.exists(AUTO_CASE_DIR): shutil.rmtree(AUTO_CASE_DIR) # 根据data里面的yaml/excel文件,自动生成测试用例 generate_cases() # ------------------------ 设置pytest相关参数 ------------------------ arg_list = [f"--maxfail={MAX_FAIL}", f"--reruns={RERUN}", f"--reruns-delay={RERUN_DELAY}", f'--alluredir={ALLURE_RESULTS_DIR}', '--clean-alluredir'] if m: arg_list.append(f"-m {m}") # ------------------------ pytest执行测试用例 ------------------------ pytest.main(args=arg_list) # ------------------------ 生成测试报告 ------------------------ if report == "yes": report_path, attachment_path = generate_allure_report(allure_results=ALLURE_RESULTS_DIR, allure_report=ALLURE_HTML_DIR, windows_title=REPORT["项目名称"], report_name=REPORT["报告标题"], env_info={ "运行环境": GLOBAL_VARS.get("host", None)}, allure_config_path=ALLURE_CONFIG_DIR, attachment_path=os.path.join(REPORT_DIR, f'autotest_report.zip')) # ------------------------ 发送测试结果 ------------------------ send_result(report_info=REPORT, report_path=report_path, attachment_path=attachment_path) # 推送allure报告到个人建站仓库 # push_allure_report(allure_report_dir=ALLURE_HTML_DIR, remote_url=GLOBAL_VARS["remote_url"], # username=GLOBAL_VARS["remote_username"], password=GLOBAL_VARS["remote_password"]) except Exception as e: raise e if __name__ == "__main__": run()