|
| 1 | +"""Configuration for unit tests.""" |
| 2 | +from importlib import util |
| 3 | +from typing import Dict, Sequence |
| 4 | + |
| 5 | +import pytest |
| 6 | +from pytest import Config, Function, Parser |
| 7 | + |
| 8 | + |
| 9 | +def pytest_addoption(parser: Parser) -> None: |
| 10 | + """Add custom command line options to pytest.""" |
| 11 | + parser.addoption( |
| 12 | + "--only-extended", |
| 13 | + action="store_true", |
| 14 | + help="Only run extended tests. Does not allow skipping any extended tests.", |
| 15 | + ) |
| 16 | + parser.addoption( |
| 17 | + "--only-core", |
| 18 | + action="store_true", |
| 19 | + help="Only run core tests. Never runs any extended tests.", |
| 20 | + ) |
| 21 | + |
| 22 | + |
| 23 | +def pytest_collection_modifyitems(config: Config, items: Sequence[Function]) -> None: |
| 24 | + """Add implementations for handling custom markers. |
| 25 | +
|
| 26 | + At the moment, this adds support for a custom `requires` marker. |
| 27 | +
|
| 28 | + The `requires` marker is used to denote tests that require one or more packages |
| 29 | + to be installed to run. If the package is not installed, the test is skipped. |
| 30 | +
|
| 31 | + The `requires` marker syntax is: |
| 32 | +
|
| 33 | + .. code-block:: python |
| 34 | +
|
| 35 | + @pytest.mark.requires("package1", "package2") |
| 36 | + def test_something(): |
| 37 | + ... |
| 38 | + """ |
| 39 | + # Mapping from the name of a package to whether it is installed or not. |
| 40 | + # Used to avoid repeated calls to `util.find_spec` |
| 41 | + required_pkgs_info: Dict[str, bool] = {} |
| 42 | + |
| 43 | + only_extended = config.getoption("--only-extended") or False |
| 44 | + only_core = config.getoption("--only-core") or False |
| 45 | + |
| 46 | + if only_extended and only_core: |
| 47 | + raise ValueError("Cannot specify both `--only-extended` and `--only-core`.") |
| 48 | + |
| 49 | + for item in items: |
| 50 | + requires_marker = item.get_closest_marker("requires") |
| 51 | + if requires_marker is not None: |
| 52 | + if only_core: |
| 53 | + item.add_marker(pytest.mark.skip(reason="Skipping not a core test.")) |
| 54 | + continue |
| 55 | + |
| 56 | + # Iterate through the list of required packages |
| 57 | + required_pkgs = requires_marker.args |
| 58 | + for pkg in required_pkgs: |
| 59 | + # If we haven't yet checked whether the pkg is installed |
| 60 | + # let's check it and store the result. |
| 61 | + if pkg not in required_pkgs_info: |
| 62 | + try: |
| 63 | + installed = util.find_spec(pkg) is not None |
| 64 | + except Exception: |
| 65 | + installed = False |
| 66 | + required_pkgs_info[pkg] = installed |
| 67 | + |
| 68 | + if not required_pkgs_info[pkg]: |
| 69 | + if only_extended: |
| 70 | + pytest.fail( |
| 71 | + f"Package `{pkg}` is not installed but is required for " |
| 72 | + f"extended tests. Please install the given package and " |
| 73 | + f"try again.", |
| 74 | + ) |
| 75 | + |
| 76 | + else: |
| 77 | + # If the package is not installed, we immediately break |
| 78 | + # and mark the test as skipped. |
| 79 | + item.add_marker( |
| 80 | + pytest.mark.skip(reason=f"Requires pkg: `{pkg}`") |
| 81 | + ) |
| 82 | + break |
| 83 | + else: |
| 84 | + if only_extended: |
| 85 | + item.add_marker( |
| 86 | + pytest.mark.skip(reason="Skipping not an extended test.") |
| 87 | + ) |
0 commit comments