# -*- coding: utf-8 -*-
# PyTest测试框架类
# 作者: 陈磊
# 时间: 2019-10-29


import pytest
import os


from datetime import datetime
from WorkBase import WorkBase
from WorkUtils.UtilsLog import UtilsLog


class UtilsPyTest:
    def __init__(self, case, case_info=None):
        """
        :param case: 用例
        :param case_info: 信息
        """
        self.log = UtilsLog()
        self.log.info("调用PyTest测试框架类")
        self.log.info(self.__class__)

        self.case_name = ""
        self.create_time = ""
        self.case = case
        self.version = self.case["version"]

        self.case_path = WorkBase().case_path
        self.case_info = case_info

        self.xml_path = ""
        self.report_path = ""

    def mkdir_case(self):
        """
        创建用例文件的报告文件夹
        :return:
        """
        self.log.debug("创建测试报告XML文件路径......")
        # 解析用例路径信息
        self.create_time = datetime.today().strftime("%Y%m%d%H%M%S")
        # 判断是多用例还是单用例模式
        if self.case_info is not None:
            # 得到测试文件名称
            self.log.debug("解析文件路径：%s" % self.case_info[0])
            self.log.debug("解析文件名称：%s" % self.case_info[1])
            self.case_name = self.case_info[1]
            self.xml_path = WorkBase().report_path + self.version + "\\" + str(self.case_name)[0:-3] + "\\" + self.create_time + "\\"
        else:
            self.log.debug("多个用例文件运行模式")
            self.case_name = self.case_path + self.case["path"]
            self.log.debug("测试路径：%s" % self.case_name)
            self.xml_path = WorkBase().report_path + self.version + "\\" + "Zmpos" + "\\" + self.create_time + "\\"

        # 判断是否创建报告路径
        if os.path.exists(self.xml_path):
            pass
        else:
            os.makedirs(self.xml_path)
        self.log.debug("创建测试报告HTML文件路径......")
        self.report_path = self.xml_path + "allure-report"

    def run_case(self):
        """
        运行测试用例
        :return:
        """
        self.log.debug("开始执行自动化测试用例......")
        self.log.debug("打开测试用例路径......")
        # case_path = self.base_path + self.path
        # sys.path.append(case_path)
        self.log.info("运行测试文件......")
        pytest.main(["-q", "-s", "--alluredir=" + self.xml_path, self.case_name])

        # 多进程
        # pytest.main(["-q", "-s", "-n 5", "--alluredir=" + self.xml_path, self.case_name])

        # 重试运行
        # pytest.main(["-q", "-s", "--reruns 5", "--alluredir=" + self.xml_path, self.case_name])
        self.log.info("运行结束......")

    def run_xml(self):
        """
        生成测试报告
        :return:
        """
        self.log.debug("测试信息：%s" % self.case_name)
        self.log.info("生成测试报告......")
        cmd = "allure generate " + self.xml_path + " -o " + self.report_path + " --clean"
        # self.log.debug(cmd)
        os.system(cmd)

    def run_main(self):
        """
        加入校验的运行方法
        :return:
        """
        if self.version is None:
            self.log.error("运行参数解析失败,请重新输入参数!")
            self.log.error("可以输入 -h,获取帮助信息!")
        else:
            self.mkdir_case()
            self.run_case()
            self.run_xml()
