1. # main.py
    2. import argparse
    3. import os
    4. import shutil
    5. import sys
    6. import time
    7. from allure_env import write_allure_env
    8. from common import logger
    9. project_path = os.path.dirname(os.path.abspath(__file__))
    10. sys.path.append(project_path)
    11. def mk_report_dir():
    12. # 创建测试报告目录
    13. report_path = os.path.join(project_path, 'report')
    14. if not os.path.exists(report_path):
    15. os.mkdir(report_path)
    16. # 每次的测试报告
    17. report_dir_path = os.path.abspath('{0}/{1}'.format(report_path, time.strftime("%Y-%m-%d", time.localtime())))
    18. if not os.path.exists(report_dir_path):
    19. os.mkdir(report_dir_path)
    20. # 临时生成的文件
    21. xml_tmp_path = os.path.join(report_dir_path, "xml_tmp_dir")
    22. if not os.path.exists(xml_tmp_path):
    23. os.mkdir(xml_tmp_path)
    24. # html报告
    25. now_time = time.strftime("%H%M%S", time.localtime())
    26. allure_report_path = os.path.join(report_dir_path, "AllureReport{}".format(now_time))
    27. if not os.path.exists(allure_report_path):
    28. os.mkdir(allure_report_path)
    29. return xml_tmp_path, allure_report_path
    30. # 生成html报告
    31. def mk_allure_report(xml_tmp_path, allure_report_path):
    32. # 写入allure报告的执行环境信息
    33. write_allure_env()
    34. shutil.copyfile('environment.properties', '{}'.format(os.path.join(xml_tmp_path, 'environment.properties')))
    35. os.system("allure generate {0} -o {1} -c".format(xml_tmp_path, allure_report_path))
    36. shutil.rmtree(xml_tmp_path)
    37. # 生成json
    38. def create_report_xml(case_path, xml_tmp_path):
    39. os.system("python -m pytest {} --alluredir={}".format(case_path, xml_tmp_path))
    40. def pytest_main():
    41. """用例执行主函数"""
    42. parser = argparse.ArgumentParser(description='命令行执行用例参数')
    43. parser.add_argument('--env', '-e', help="运行环境 test、staging、prod", type=str, default='prod')
    44. parser.add_argument('--case_path', '-c', help="执行用例,默认执行全部", type=str, default='case')
    45. parser.add_argument('--headless', '-hl', help="是否切换无头模式,可选true/false", type=str, default="false")
    46. args = parser.parse_args()
    47. env = args.env
    48. case_path = args.case_path
    49. head_less = args.headless
    50. if head_less is not None:
    51. if head_less not in ('true', 'false'):
    52. raise ValueError('切换无头模式,请输入:true,否则输入:false')
    53. os.environ['headless'] = head_less
    54. if env is not None:
    55. if env not in ('test', 'test02', 'test03', 'test04', 'test05', 'test06', 'staging', 'prod'):
    56. raise ValueError('环境请输入:test、test02、test03、test04、test05、test06、staging、prod')
    57. os.environ['env'] = env
    58. logger.info("当前运行环境为: " + str(env))
    59. logger.info("当前运行模式为headless: " + str(head_less))
    60. xml_tmp_path, allure_report_path = mk_report_dir()
    61. create_report_xml(case_path, xml_tmp_path)
    62. if os.listdir(xml_tmp_path):
    63. mk_allure_report(xml_tmp_path, allure_report_path)
    64. if __name__ == '__main__':
    65. pytest_main()
    66. """
    67. 执行命令: python main.py -e {运行环境} -c {执行用例} -hl {是否无头模式}
    68. eg: python main.py -e prod -c case/test_1_jump_shequ.py -hl true
    69. eg: python main.py -e prod -c player_case
    70. """

    allure报告写入执行环境:

    1. # allure_env.py
    2. import os
    3. import platform
    4. from pathlib import Path, PurePath
    5. def write_allure_env():
    6. current_path = Path.cwd()
    7. file = PurePath.joinpath(current_path, 'environment.properties')
    8. with open(file, 'w', encoding='utf-8') as env_file:
    9. env_data = {
    10. "environment": os.environ.get("env").capitalize(),
    11. "platform": platform.platform()
    12. }
    13. for key, value in env_data.items():
    14. print(key, value)
    15. env_file.write(''.join([key, '=', value]) + '\n')