httprunner源码学习(二)main_run()、main_make()

main_run()

接上一篇60行第一个if语句,如果执行命令:httprunner run [用例1路径] [用例2路径] ...

  • 执行到第22行,有正确路径时,跳转main_make()方法
# # httprunner/cli.py :: main_run
def main_run(extra_args) -> enum.IntEnum:
    capture_message("start to run")  # 这个是sentry的埋点监听事件,不用管
    # keep compatibility with v2
    extra_args = ensure_cli_args(extra_args)  # 自定义方法,对少数几个参数进行兼容性处理(V2 -> V3)

    tests_path_list = []  # 存放输入正确的路径
    extra_args_new = []  # 存放输入错误的路径
    for item in extra_args:  # 遍历传过来的路径列表
        if not os.path.exists(item):  # 如果路径不存在
            # item is not file/folder path
            extra_args_new.append(item)
        else:
            # item is file/folder path
            tests_path_list.append(item)

    if len(tests_path_list) == 0:  # 如果没有正确路径,退出程序
        # has not specified any testcase path
        logger.error(f"No valid testcase path in cli arguments: {extra_args}")
        sys.exit(1)

    testcase_path_list = main_make(tests_path_list)
    if not testcase_path_list:
        logger.error("No valid testcases found, exit 1.")
        sys.exit(1)

    if "--tb=short" not in extra_args_new:
        extra_args_new.append("--tb=short")

    extra_args_new.extend(testcase_path_list)
    logger.info(f"start to run tests with pytest. HttpRunner version: {__version__}")
    return pytest.main(extra_args_new)

main_make()

mian_run()方法的第22行调用此方法,接收传过来的正确路径,进行处理

  • 前8行都是路径处理
  • 路径处理完成后,在第12行执行__make()方法
# httprunner/make.py :: main_make
def main_make(tests_paths: List[Text]) -> List[Text]:
    if not tests_paths:  # 如果路径列表是空
        return []

    for tests_path in tests_paths:
        tests_path = ensure_path_sep(tests_path)  # 自定义方法,对路径进行处理,Linux/Windows,这里值得学习,可以借鉴源码
        if not os.path.isabs(tests_path):  # 如果不是绝对路径
            tests_path = os.path.join(os.getcwd(), tests_path)  # 拼接绝对路径

        try:
            __make(tests_path)
        except exceptions.MyBaseError as ex:
            logger.error(ex)
            sys.exit(1)

    # format pytest files
    pytest_files_format_list = pytest_files_made_cache_mapping.keys()
    format_pytest_with_black(*pytest_files_format_list)

    return list(pytest_files_run_set)

下一篇 httprunner源码学习(三)__make()

posted @ 2022-01-18 17:25  寡淡的白开水  阅读(379)  评论(0)    收藏  举报