五、用例执行

实现各个环境切换,一套脚本只要调通了,基本接口没有变动,可以大大的降低测试成本,主要用的是argparse库。

代码如下:

#!/usr/bin/python
# -*- coding: UTF-8 -*-
# @Author   :zaygee
# @time     :2020/12/3 17:17
# @file     :runtest.py
import argparse
import shutil
import sys
import time
from shutil import copyfile
from allure_env import write_allure_env
from common import logger
from config import *

project_path = os.path.dirname(os.path.abspath(__file__))
sys.path.append(project_path)


def mk_report_dir():
    # 创建测试报告目录
    report_path = os.path.join(project_path, 'report')
    if not os.path.exists(report_path):
        os.mkdir(report_path)
    # 每次的测试报告
    report_dir_path = os.path.abspath('{0}/{1}'.format(report_path, time.strftime("%Y-%m-%d", time.localtime())))
    if not os.path.exists(report_dir_path):
        os.mkdir(report_dir_path)
    # 临时生成的文件
    xml_tmp_path = os.path.join(report_dir_path, "xml_tmp_dir")
    if not os.path.exists(xml_tmp_path):
        os.mkdir(xml_tmp_path)
    # html报告
    now_time = time.strftime("%H%M%S", time.localtime())
    allure_report_path = os.path.join(report_dir_path, "AllureReport{}".format(now_time))
    if not os.path.exists(allure_report_path):
        os.mkdir(allure_report_path)

    return xml_tmp_path, allure_report_path


# 生成html报告
def mk_allure_report(xml_tmp_path, allure_report_path):
    # 写入allure报告的执行环境信息
    write_allure_env()
    # 复制environment.properties文件到allure根目录
    copyfile('environment.properties', '{}'.format(os.path.join(xml_tmp_path, 'environment.properties')))
    os.system("allure generate {0} -o {1} -c".format(xml_tmp_path, allure_report_path))
    shutil.rmtree(xml_tmp_path)


# 生成json
def create_report_xml(case_path, xml_tmp_path):
    os.system("python -m  pytest {} -v -s --alluredir={}".format(case_path, xml_tmp_path)


def pytest_main():
    """用例执行主函数"""
    parser = argparse.ArgumentParser(description='命令行执行用例参数')
    parser.add_argument('--env', '-e', help="运行环境 test、staging、prod", type=str, default='dev')
    parser.add_argument('--case_path', '-c', help="执行用例,默认执行全部", type=str, default='kitten_cases')
    args = parser.parse_args()
    run_env = args.env
    case_path = args.case_path

    if run_env is not None:
        if run_env not in ('dev', 'test', 'staging', 'prod'):
            raise ValueError('环境请输入:dev、test、staging、prod')
        os.environ['env'] = run_env
        logger.info("当前运行环境为: " + str(run_env))
    xml_tmp_path, allure_report_path = mk_report_dir()
    create_report_xml(case_path, xml_tmp_path)
    if os.listdir(xml_tmp_path):
        mk_allure_report(xml_tmp_path, allure_report_path)


if __name__ == '__main__':
    pytest_main()
    """
    执行命令:  python main.py -e {运行环境} -c {执行用例}
    eg:  python main.py -e prod -c cases    """

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值