|
8 | 8 |
|
9 | 9 | from tests import configs |
10 | 10 | from tests.conftest import RUN_SLOW_ARG_NAME |
| 11 | +import argparse |
11 | 12 |
|
12 | | -if __name__ == '__main__': |
13 | | - # TODO: use argparse |
14 | | - sys_args_dict = pbt.cmds.get_cmd_kwargs( |
15 | | - { |
16 | | - 1 : os.path.join(os.getcwd(), "tests"), |
17 | | - "N_RANDOM_TESTS_PER_CASE": configs.N_RANDOM_TESTS_PER_CASE, |
18 | | - "save_report" : "True", |
19 | | - "cov" : "src", |
20 | | - "cov-report" : "xml", |
21 | | - RUN_SLOW_ARG_NAME : "True", |
22 | | - "durations" : 10, |
23 | | - } |
| 13 | + |
| 14 | +def get_args_parser(): |
| 15 | + parser = argparse.ArgumentParser(description="Tests Runner") |
| 16 | + parser.add_argument( |
| 17 | + "--tests_folder", |
| 18 | + type=str, |
| 19 | + default=os.path.join(os.getcwd(), "tests"), |
| 20 | + help="Path to the folder containing tests.", |
| 21 | + ) |
| 22 | + parser.add_argument( |
| 23 | + "--N_RANDOM_TESTS_PER_CASE", |
| 24 | + type=int, |
| 25 | + default=configs.N_RANDOM_TESTS_PER_CASE, |
| 26 | + help="Number of random tests to run per test case.", |
| 27 | + ) |
| 28 | + parser.add_argument( |
| 29 | + "--save_report", |
| 30 | + type=bool, |
| 31 | + default=False, |
| 32 | + action=argparse.BooleanOptionalAction, |
| 33 | + help="Whether to save the report in JSON format.", |
| 34 | + ) |
| 35 | + parser.add_argument( |
| 36 | + "--cov", |
| 37 | + type=str, |
| 38 | + default="src", |
| 39 | + help="Path to the source code for coverage.", |
| 40 | + ) |
| 41 | + parser.add_argument( |
| 42 | + "--cov-report", |
| 43 | + type=str, |
| 44 | + default="xml:tests/.tmp/coverage.xml", |
| 45 | + help="Format of the coverage report.", |
| 46 | + ) |
| 47 | + parser.add_argument( |
| 48 | + f"--{RUN_SLOW_ARG_NAME}", |
| 49 | + type=bool, |
| 50 | + default=False, |
| 51 | + action=argparse.BooleanOptionalAction, |
| 52 | + help="Whether to run slow tests.", |
24 | 53 | ) |
25 | | - sys_args_dict["cov-report"] = sys_args_dict["cov_report"] # fix |
26 | | - configs.N_RANDOM_TESTS_PER_CASE = sys_args_dict["N_RANDOM_TESTS_PER_CASE"] |
27 | | - configs.RUN_SLOW_TESTS = 't' in str(sys_args_dict[RUN_SLOW_ARG_NAME]).lower() |
| 54 | + parser.add_argument( |
| 55 | + "--durations", |
| 56 | + type=int, |
| 57 | + default=10, |
| 58 | + help="Number of slowest test durations to report.", |
| 59 | + ) |
| 60 | + return parser |
| 61 | + |
| 62 | + |
| 63 | +def main(): |
| 64 | + parser = get_args_parser() |
| 65 | + args = parser.parse_args() |
| 66 | + configs.N_RANDOM_TESTS_PER_CASE = args.N_RANDOM_TESTS_PER_CASE |
| 67 | + configs.RUN_SLOW_TESTS = args.run_slow |
28 | 68 | json_plugin = JSONReport() |
29 | | - pytest_main_args_names = ["cov", "cov-report", "durations"] |
30 | | - pytest_main_args = [f"--{k}={sys_args_dict[k]}" for k in pytest_main_args_names] |
31 | | - pytest.main([sys_args_dict[1], *pytest_main_args], plugins=[json_plugin]) |
32 | | - json_path = os.path.join(os.getcwd(), "tests", "tmp", f"tests_report_rn{configs.N_RANDOM_TESTS_PER_CASE}.json") |
33 | | - save_report = 't' in str(sys_args_dict["save_report"]).lower() |
34 | | - if save_report: |
| 69 | + pytest_main_args = [ |
| 70 | + args.tests_folder, |
| 71 | + f"--cov={args.cov}", |
| 72 | + f"--cov-report={args.cov_report}", |
| 73 | + f"--cov-report=term-missing", |
| 74 | + f"--durations={args.durations}", |
| 75 | + ] |
| 76 | + pytest.main(pytest_main_args, plugins=[json_plugin]) |
| 77 | + json_path = os.path.join(args.tests_folder, ".tmp", f"tests_report_rn{configs.N_RANDOM_TESTS_PER_CASE}.json") |
| 78 | + if args.save_report: |
35 | 79 | json_plugin.save_report(json_path) |
36 | 80 | json_data = json.load(open(json_path)) |
37 | 81 | with open(json_path, "w") as f: |
38 | 82 | json.dump(json_data, f, indent=4) |
| 83 | + return 0 |
| 84 | + |
| 85 | + |
| 86 | +if __name__ == '__main__': |
| 87 | + exit(main()) |
0 commit comments