# main.py
import argparse
import os
import shutil
import sys
import time
from allure_env import write_allure_env
from common import logger
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()
shutil.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 {} --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='prod')
parser.add_argument('--case_path', '-c', help="执行用例,默认执行全部", type=str, default='case')
parser.add_argument('--headless', '-hl', help="是否切换无头模式,可选true/false", type=str, default="false")
args = parser.parse_args()
env = args.env
case_path = args.case_path
head_less = args.headless
if head_less is not None:
if head_less not in ('true', 'false'):
raise ValueError('切换无头模式,请输入:true,否则输入:false')
os.environ['headless'] = head_less
if env is not None:
if env not in ('test', 'test02', 'test03', 'test04', 'test05', 'test06', 'staging', 'prod'):
raise ValueError('环境请输入:test、test02、test03、test04、test05、test06、staging、prod')
os.environ['env'] = env
logger.info("当前运行环境为: " + str(env))
logger.info("当前运行模式为headless: " + str(head_less))
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 {执行用例} -hl {是否无头模式}
eg: python main.py -e prod -c case/test_1_jump_shequ.py -hl true
eg: python main.py -e prod -c player_case
"""
allure报告写入执行环境:
# allure_env.py
import os
import platform
from pathlib import Path, PurePath
def write_allure_env():
current_path = Path.cwd()
file = PurePath.joinpath(current_path, 'environment.properties')
with open(file, 'w', encoding='utf-8') as env_file:
env_data = {
"environment": os.environ.get("env").capitalize(),
"platform": platform.platform()
}
for key, value in env_data.items():
print(key, value)
env_file.write(''.join([key, '=', value]) + '\n')