|
1 | 1 | """Configuration for unit tests.""" |
2 | | -import logging |
| 2 | + |
3 | 3 | from importlib import util |
4 | 4 | from pathlib import Path |
5 | | -from typing import Dict, List, Sequence |
| 5 | +from typing import Dict, Sequence |
6 | 6 |
|
7 | 7 | import pytest |
8 | 8 | from pytest import Config, Function, Parser |
| 9 | + |
9 | 10 | from zhipuai.core.logs import ( |
10 | | - get_config_dict, |
11 | | - get_log_file, |
12 | | - get_timestamp_ms, |
| 11 | + get_config_dict, |
| 12 | + get_log_file, |
| 13 | + get_timestamp_ms, |
13 | 14 | ) |
14 | 15 |
|
15 | 16 |
|
16 | | - |
17 | 17 | def pytest_addoption(parser: Parser) -> None: |
18 | | - """Add custom command line options to pytest.""" |
19 | | - parser.addoption( |
20 | | - "--only-extended", |
21 | | - action="store_true", |
22 | | - help="Only run extended tests. Does not allow skipping any extended tests.", |
23 | | - ) |
24 | | - parser.addoption( |
25 | | - "--only-core", |
26 | | - action="store_true", |
27 | | - help="Only run core tests. Never runs any extended tests.", |
28 | | - ) |
| 18 | + """Add custom command line options to pytest.""" |
| 19 | + parser.addoption( |
| 20 | + '--only-extended', |
| 21 | + action='store_true', |
| 22 | + help='Only run extended tests. Does not allow skipping any extended tests.', |
| 23 | + ) |
| 24 | + parser.addoption( |
| 25 | + '--only-core', |
| 26 | + action='store_true', |
| 27 | + help='Only run core tests. Never runs any extended tests.', |
| 28 | + ) |
29 | 29 |
|
30 | 30 |
|
31 | 31 | def pytest_collection_modifyitems(config: Config, items: Sequence[Function]) -> None: |
32 | | - """Add implementations for handling custom markers. |
33 | | -
|
34 | | - At the moment, this adds support for a custom `requires` marker. |
35 | | -
|
36 | | - The `requires` marker is used to denote tests that require one or more packages |
37 | | - to be installed to run. If the package is not installed, the test is skipped. |
38 | | -
|
39 | | - The `requires` marker syntax is: |
40 | | -
|
41 | | - .. code-block:: python |
42 | | -
|
43 | | - @pytest.mark.requires("package1", "package2") |
44 | | - def test_something(): |
45 | | - ... |
46 | | - """ |
47 | | - # Mapping from the name of a package to whether it is installed or not. |
48 | | - # Used to avoid repeated calls to `util.find_spec` |
49 | | - required_pkgs_info: Dict[str, bool] = {} |
50 | | - |
51 | | - only_extended = config.getoption("--only-extended") or False |
52 | | - only_core = config.getoption("--only-core") or False |
53 | | - |
54 | | - if only_extended and only_core: |
55 | | - raise ValueError("Cannot specify both `--only-extended` and `--only-core`.") |
56 | | - |
57 | | - for item in items: |
58 | | - requires_marker = item.get_closest_marker("requires") |
59 | | - if requires_marker is not None: |
60 | | - if only_core: |
61 | | - item.add_marker(pytest.mark.skip(reason="Skipping not a core test.")) |
62 | | - continue |
63 | | - |
64 | | - # Iterate through the list of required packages |
65 | | - required_pkgs = requires_marker.args |
66 | | - for pkg in required_pkgs: |
67 | | - # If we haven't yet checked whether the pkg is installed |
68 | | - # let's check it and store the result. |
69 | | - if pkg not in required_pkgs_info: |
70 | | - try: |
71 | | - installed = util.find_spec(pkg) is not None |
72 | | - except Exception: |
73 | | - installed = False |
74 | | - required_pkgs_info[pkg] = installed |
75 | | - |
76 | | - if not required_pkgs_info[pkg]: |
77 | | - if only_extended: |
78 | | - pytest.fail( |
79 | | - f"Package `{pkg}` is not installed but is required for " |
80 | | - f"extended tests. Please install the given package and " |
81 | | - f"try again.", |
82 | | - ) |
83 | | - |
84 | | - else: |
85 | | - # If the package is not installed, we immediately break |
86 | | - # and mark the test as skipped. |
87 | | - item.add_marker( |
88 | | - pytest.mark.skip(reason=f"Requires pkg: `{pkg}`") |
89 | | - ) |
90 | | - break |
91 | | - else: |
92 | | - if only_extended: |
93 | | - item.add_marker( |
94 | | - pytest.mark.skip(reason="Skipping not an extended test.") |
95 | | - ) |
| 32 | + """Add implementations for handling custom markers. |
| 33 | +
|
| 34 | + At the moment, this adds support for a custom `requires` marker. |
| 35 | +
|
| 36 | + The `requires` marker is used to denote tests that require one or more packages |
| 37 | + to be installed to run. If the package is not installed, the test is skipped. |
| 38 | +
|
| 39 | + The `requires` marker syntax is: |
| 40 | +
|
| 41 | + .. code-block:: python |
| 42 | +
|
| 43 | + @pytest.mark.requires('package1', 'package2') |
| 44 | + def test_something(): ... |
| 45 | + """ |
| 46 | + # Mapping from the name of a package to whether it is installed or not. |
| 47 | + # Used to avoid repeated calls to `util.find_spec` |
| 48 | + required_pkgs_info: Dict[str, bool] = {} |
| 49 | + |
| 50 | + only_extended = config.getoption('--only-extended') or False |
| 51 | + only_core = config.getoption('--only-core') or False |
| 52 | + |
| 53 | + if only_extended and only_core: |
| 54 | + raise ValueError('Cannot specify both `--only-extended` and `--only-core`.') |
| 55 | + |
| 56 | + for item in items: |
| 57 | + requires_marker = item.get_closest_marker('requires') |
| 58 | + if requires_marker is not None: |
| 59 | + if only_core: |
| 60 | + item.add_marker(pytest.mark.skip(reason='Skipping not a core test.')) |
| 61 | + continue |
| 62 | + |
| 63 | + # Iterate through the list of required packages |
| 64 | + required_pkgs = requires_marker.args |
| 65 | + for pkg in required_pkgs: |
| 66 | + # If we haven't yet checked whether the pkg is installed |
| 67 | + # let's check it and store the result. |
| 68 | + if pkg not in required_pkgs_info: |
| 69 | + try: |
| 70 | + installed = util.find_spec(pkg) is not None |
| 71 | + except Exception: |
| 72 | + installed = False |
| 73 | + required_pkgs_info[pkg] = installed |
| 74 | + |
| 75 | + if not required_pkgs_info[pkg]: |
| 76 | + if only_extended: |
| 77 | + pytest.fail( |
| 78 | + f'Package `{pkg}` is not installed but is required for ' |
| 79 | + f'extended tests. Please install the given package and ' |
| 80 | + f'try again.', |
| 81 | + ) |
| 82 | + |
| 83 | + else: |
| 84 | + # If the package is not installed, we immediately break |
| 85 | + # and mark the test as skipped. |
| 86 | + item.add_marker(pytest.mark.skip(reason=f'Requires pkg: `{pkg}`')) |
| 87 | + break |
| 88 | + else: |
| 89 | + if only_extended: |
| 90 | + item.add_marker(pytest.mark.skip(reason='Skipping not an extended test.')) |
96 | 91 |
|
97 | 92 |
|
98 | 93 | @pytest.fixture |
99 | 94 | def logging_conf() -> dict: |
100 | | - return get_config_dict( |
101 | | - "info", |
102 | | - get_log_file(log_path="logs", sub_dir=f"local_{get_timestamp_ms()}"), |
103 | | - 1024*1024, |
104 | | - 1024*1024*1024, |
105 | | - ) |
| 95 | + return get_config_dict( |
| 96 | + 'info', |
| 97 | + get_log_file(log_path='logs', sub_dir=f'local_{get_timestamp_ms()}'), |
| 98 | + 1024 * 1024, |
| 99 | + 1024 * 1024 * 1024, |
| 100 | + ) |
| 101 | + |
106 | 102 |
|
107 | 103 | @pytest.fixture |
108 | 104 | def test_file_path(request) -> Path: |
109 | | - from pathlib import Path |
110 | | - import os |
111 | | - # 当前执行目录 |
112 | | - # 获取当前测试文件的路径 |
113 | | - test_file_path = Path(str(request.fspath)).parent |
114 | | - print("test_file_path:",test_file_path) |
115 | | - return test_file_path |
| 105 | + from pathlib import Path |
| 106 | + |
| 107 | + # 当前执行目录 |
| 108 | + # 获取当前测试文件的路径 |
| 109 | + test_file_path = Path(str(request.fspath)).parent |
| 110 | + print('test_file_path:', test_file_path) |
| 111 | + return test_file_path |
0 commit comments