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

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