Exemple #1
0
def main():
    """API test: parse command line options and run commands."""
    init_logger()

    parser = argparse.ArgumentParser(description=__description__)
    parser.add_argument(
        "-V", "--version", dest="version", action="store_true", help="show version"
    )

    subparsers = parser.add_subparsers(help="sub-command help")
    init_parser_run(subparsers)
    sub_parser_make = init_make_parser(subparsers)

    if len(sys.argv) == 1:
        # httprunner
        parser.print_help()
        sys.exit(0)
    elif len(sys.argv) == 2:
        # print help for sub-commands
        if sys.argv[1] in ["-V", "--version"]:
            # httprunner -V
            print(f"{__version__}")
        elif sys.argv[1] in ["-h", "--help"]:
            # httprunner -h
            parser.print_help()
        elif sys.argv[1] == "run":
            # httprunner run
            pytest.main(["-h"])
        elif sys.argv[1] == "make":
            # httprunner make
            sub_parser_make.print_help()
        sys.exit(0)
    elif (
        len(sys.argv) == 3 and sys.argv[1] == "run" and sys.argv[2] in ["-h", "--help"]
    ):
        # httprunner run -h
        pytest.main(["-h"])
        sys.exit(0)

    extra_args = []
    if len(sys.argv) >= 2 and sys.argv[1] in ["run"]:
        args, extra_args = parser.parse_known_args()
    else:
        args = parser.parse_args()

    if args.version:
        print(f"{__version__}")
        sys.exit(0)

    if sys.argv[1] == "run":
        sys.exit(main_run(extra_args))
    elif sys.argv[1] == "make":
        main_make(args.testcase_path)
Exemple #2
0
def main_run(extra_args):
    # keep compatibility with v2
    extra_args = ensure_cli_args(extra_args)

    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)

    extra_args_new.extend(testcase_path_list)
    pytest.main(extra_args_new)
Exemple #3
0
 def test_make_testsuite(self):
     path = ["examples/postman_echo/request_methods/demo_testsuite.yml"]
     testcase_python_list = main_make(path)
     self.assertEqual(len(testcase_python_list), 2)
     self.assertIn(
         os.path.join(
             os.getcwd(),
             os.path.join(
                 "examples",
                 "postman_echo",
                 "request_methods",
                 "demo_testsuite_yml",
                 "request_with_functions_test.py",
             ),
         ),
         testcase_python_list,
     )
     self.assertIn(
         os.path.join(
             os.getcwd(),
             os.path.join(
                 "examples",
                 "postman_echo",
                 "request_methods",
                 "demo_testsuite_yml",
                 "request_with_testcase_reference_test.py",
             ),
         ),
         testcase_python_list,
     )
Exemple #4
0
def main_run(extra_args) -> enum.IntEnum:
    capture_message("start to run")
    # keep compatibility with v2
    extra_args = ensure_cli_args(extra_args)

    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)
    return pytest.main(extra_args_new)
Exemple #5
0
    def test_make_testcase_with_ref(self):
        path = [
            "examples/postman_echo/request_methods/request_with_testcase_reference.yml"
        ]
        testcase_python_list = main_make(path)
        self.assertEqual(len(testcase_python_list), 1)
        self.assertIn(
            os.path.join(
                os.getcwd(),
                "examples/postman_echo/request_methods/request_with_testcase_reference_test.py",
            ),
            testcase_python_list,
        )

        with open(
                "examples/postman_echo/request_methods/request_with_testcase_reference_test.py"
        ) as f:
            content = f.read()
            self.assertIn(
                """
from examples.postman_echo.request_methods.request_with_functions_test import (
    TestCaseRequestWithFunctions as RequestWithFunctions,
)
""",
                content,
            )
            self.assertIn(
                ".call(RequestWithFunctions)",
                content,
            )
Exemple #6
0
 def test_make_testcase_folder(self):
     path = ["examples/postman_echo/request_methods/"]
     testcase_python_list = main_make(path)
     self.assertIn(
         "examples/postman_echo/request_methods/request_with_functions_test.py",
         testcase_python_list,
     )
Exemple #7
0
def main_run(extra_args) -> enum.IntEnum:
    ga_client.track_event("RunAPITests", "hrun")
    # keep compatibility with v2
    extra_args = ensure_cli_args(extra_args)

    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)
Exemple #8
0
    def test_make_testcase_with_ref(self):
        path = [
            "examples/postman_echo/request_methods/request_with_testcase_reference.yml"
        ]
        make_files_cache_set.clear()
        testcase_python_list = main_make(path)
        self.assertEqual(len(testcase_python_list), 2)
        self.assertIn(
            "examples/postman_echo/request_methods/request_with_testcase_reference_test.py",
            testcase_python_list,
        )

        with open(
                "examples/postman_echo/request_methods/request_with_testcase_reference_test.py"
        ) as f:
            content = f.read()
            self.assertIn(
                """
from examples.postman_echo.request_methods.request_with_functions_test import (
    TestCaseRequestWithFunctions as RequestWithFunctions,
)
""",
                content,
            )
            self.assertIn(
                '"testcase": RequestWithFunctions,',
                content,
            )
Exemple #9
0
 def test_make_testcase(self):
     path = [
         "examples/postman_echo/request_methods/request_with_variables.yml"
     ]
     testcase_python_list = main_make(path)
     self.assertEqual(
         testcase_python_list[0],
         "examples/postman_echo/request_methods/request_with_variables_test.py",
     )
Exemple #10
0
 def test_make_testcase_folder(self):
     path = ["examples/postman_echo/request_methods/"]
     testcase_python_list = main_make(path)
     self.assertIn(
         os.path.join(
             os.getcwd(),
             os.path.join(
                 "examples",
                 "postman_echo",
                 "request_methods",
                 "request_with_functions_test.py",
             ),
         ),
         testcase_python_list,
     )
Exemple #11
0
 def test_make_testcase(self):
     path = ["examples/postman_echo/request_methods/request_with_variables.yml"]
     testcase_python_list = main_make(path)
     self.assertEqual(
         testcase_python_list[0],
         os.path.join(
             os.getcwd(),
             os.path.join(
                 "examples",
                 "postman_echo",
                 "request_methods",
                 "request_with_variables_test.py",
             ),
         ),
     )
Exemple #12
0
def main_locusts():
    """ locusts entrance
    """
    from httprunner.utils import init_sentry_sdk
    from sentry_sdk import capture_message

    init_sentry_sdk()
    capture_message("start to run locusts")

    # avoid print too much log details in console
    logger.remove()
    logger.add(sys.stderr, level="WARNING")

    sys.argv[0] = "locust"
    if len(sys.argv) == 1:
        sys.argv.extend(["-h"])

    if sys.argv[1] in ["-h", "--help", "-V", "--version"]:
        locust_main.main()

    def get_arg_index(*target_args):
        for arg in target_args:
            if arg not in sys.argv:
                continue

            return sys.argv.index(arg) + 1

        return None

    # get testcase file path
    testcase_index = get_arg_index("-f", "--locustfile")
    if not testcase_index:
        print("Testcase file is not specified, exit 1.")
        sys.exit(1)

    from httprunner.make import main_make

    global pytest_files
    testcase_file_path = sys.argv[testcase_index]
    pytest_files = main_make([testcase_file_path])
    if not pytest_files:
        print("No valid testcases found, exit 1.")
        sys.exit(1)

    sys.argv[testcase_index] = os.path.join(os.path.dirname(__file__),
                                            "locustfile.py")

    locust_main.main()
Exemple #13
0
 def test_make_testsuite(self):
     path = ["examples/postman_echo/request_methods/demo_testsuite.yml"]
     make_files_cache_set.clear()
     testcase_python_list = main_make(path)
     self.assertEqual(len(testcase_python_list), 3)
     self.assertIn(
         "examples/postman_echo/request_methods/demo_testsuite_yml/request_with_functions_test.py",
         testcase_python_list,
     )
     self.assertIn(
         "examples/postman_echo/request_methods/demo_testsuite_yml/request_with_testcase_reference_test.py",
         testcase_python_list,
     )
     self.assertIn(
         "examples/postman_echo/request_methods/request_with_functions_test.py",
         testcase_python_list,
     )
Exemple #14
0
def main_run(extra_args, config) -> enum.IntEnum:
    capture_message("start to run")
    logger.debug(f"[main run] config ==> {config}")
    logger.debug(f"[main run] extra_args ==> {extra_args}")
    # keep compatibility with v2
    extra_args = ensure_cli_args(extra_args)

    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 config:
        extra_args_new.append(f"--config={config}")

    if len(tests_path_list) == 0:
        # has not specified any test_cases path
        logger.error(f"No valid testcase path in cli arguments: {extra_args}")
        sys.exit(1)

    testcase_path_list = main_make(tests_path_list)
    # config_to_debugtalk_make(config)
    # move_pytest_files_to_target(os.path.join(BASE_DIR, "test_cases"))
    # 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")
    if "-v" not in extra_args_new:
        extra_args_new.append("-v")
    for index, testcase_path in enumerate(testcase_path_list):
        testcase_path_list[index] = testcase_path.replace("test_cases", "target")
    extra_args_new.extend(testcase_path_list)
    logger.info(f"start to run tests with pytest. HttpRunner version: {__version__}")
    logger.info(f"extra_args_new ==> {extra_args_new}")
    return pytest.main(extra_args_new)