promptflow
128 строк · 4.4 Кб
1import argparse
2import os
3import sys
4from pathlib import Path
5
6from utils import Color, run_command, print_red
7
8if __name__ == "__main__":
9parser = argparse.ArgumentParser(description=Color.RED + "Test Coverage for Promptflow!" + Color.END + "\n")
10
11parser.add_argument("-p", required=True, nargs="+", help="The paths to calculate code coverage")
12parser.add_argument("-t", required=True, nargs="+", help="The path to the tests")
13parser.add_argument("-l", required=True, help="Location to run tests in")
14parser.add_argument(
15"-m",
16required=True,
17help="Pytest marker to identify the tests to run",
18default="all",
19)
20parser.add_argument(
21"-o",
22required=False,
23help="Pytest output file name",
24default="test-results.xml",
25)
26parser.add_argument("-n", help="Pytest number of process to run the tests", default="auto")
27parser.add_argument(
28"--model-name",
29help="The model file name to run the tests",
30type=str,
31default="",
32)
33parser.add_argument("--timeout", help="Timeout for individual tests (seconds)", type=str, default="")
34parser.add_argument(
35"--coverage-config",
36help="The path of code coverage config file",
37type=str,
38default="",
39)
40parser.add_argument(
41"--disable-cov-branch",
42action="store_true",
43help="Whether to enable branch coverage calculation",
44)
45parser.add_argument(
46"--ignore-glob",
47help="The path of ignored test file",
48type=str,
49default="",
50)
51
52args = parser.parse_args()
53print("Working directory: " + str(os.getcwd()))
54print("Args.p: " + str(args.p))
55print("Args.t: " + str(args.t))
56print("Args.l: " + str(args.l))
57print("Args.m: " + str(args.m))
58print("Args.n: " + str(args.n))
59print("Args.o: " + str(args.o))
60print("Args.model-name: " + str(args.model_name))
61print("Args.timeout: " + str(args.timeout))
62print("Args.coverage-config: " + str(args.coverage_config))
63print("Args.ignore-glob: " + str(args.ignore_glob))
64print("Args.disable-cov-branch: " + str(args.disable_cov_branch))
65
66test_paths_list = [str(Path(path).absolute()) for path in args.t]
67
68# display a list of all Python packages installed in the current Python environment
69run_command(["pip", "list"])
70run_command(["pip", "show", "promptflow", "promptflow-sdk"])
71
72pytest_command = ["pytest", f"--junitxml={args.o}"]
73pytest_command += test_paths_list
74if args.coverage_config:
75if args.p:
76cov_path_list = [f"--cov={path}" for path in args.p]
77pytest_command += cov_path_list
78if not args.disable_cov_branch:
79pytest_command += ["--cov-branch"]
80pytest_command += [ # noqa: W503
81"--cov-report=term",
82"--cov-report=html",
83"--cov-report=xml",
84]
85pytest_command = pytest_command + [f"--cov-config={args.coverage_config}"]
86if args.ignore_glob:
87pytest_command = pytest_command + [f"--ignore-glob={args.ignore_glob}"]
88pytest_command += [
89"-n",
90args.n,
91"--dist",
92"loadfile",
93"--log-level=info",
94"--log-format=%(asctime)s %(levelname)s %(message)s",
95"--log-date-format=[%Y-%m-%d %H:%M:%S]",
96"--durations=5",
97"-ra",
98"-vv",
99]
100
101if args.timeout:
102pytest_command = pytest_command + [
103"--timeout",
104args.timeout,
105"--timeout_method",
106"thread",
107]
108
109if args.m != "all":
110pytest_command = pytest_command + ["-m", args.m]
111
112if args.model_name:
113pytest_command = pytest_command + ["--model-name", args.model_name]
114
115# pytest --junit-xml=test-results.xml --cov=azure.ai.ml --cov-report=html --cov-report=xml -ra ./tests/*/unittests/
116error_code, _ = run_command(pytest_command, throw_on_retcode=False)
117# https://docs.pytest.org/en/7.1.x/reference/exit-codes.html
118if error_code == 1:
119print_red("Tests were collected and run but some of the tests failed.")
120elif error_code == 2:
121print_red("Test execution was interrupted by the user.")
122elif error_code == 3:
123print_red("Internal error happened while executing tests.")
124elif error_code == 4:
125print_red("pytest command line usage error.")
126elif error_code == 5:
127print_red("No tests were collected.")
128sys.exit(error_code)
129