|
| 1 | +#!/usr/bin/env python3.11 |
| 2 | +# coding:utf-8 |
| 3 | +# Copyright (C) 2020-2025 All rights reserved. |
| 4 | +# FILENAME: ~~/tests/conftest.py |
| 5 | +# VERSION: 1.0.6 |
| 6 | +# CREATED: 2025-09-09 13:17 |
| 7 | +# AUTHOR: Sitt Guruvanich <[email protected]> |
| 8 | +# DESCRIPTION: |
| 9 | +# |
| 10 | +# HISTORY: |
| 11 | +# ************************************************************* |
| 12 | +"""Module defining fixtures auto-imported by `pytest` command when running tests""" |
| 13 | + |
| 14 | +### Third-party packages ### |
| 15 | +from pytest import Config, Item, Parser, UsageError, mark |
| 16 | + |
| 17 | + |
| 18 | +def pytest_addoption(parser: Parser) -> None: |
| 19 | + """Add option to Pytest CLI parser |
| 20 | +
|
| 21 | + --- |
| 22 | + :param parser: Pytest Parser instance passed when running via pytest command line interface |
| 23 | + :type parser: pytest.Parser |
| 24 | + """ |
| 25 | + parser.addoption( |
| 26 | + "--flexible", |
| 27 | + action="store_true", |
| 28 | + default=False, |
| 29 | + help="When flagged, only run Flexible mode tests.", |
| 30 | + ) |
| 31 | + parser.addoption( |
| 32 | + "--normal", |
| 33 | + action="store_true", |
| 34 | + default=False, |
| 35 | + help="when flagged, only run Normal mode tests.", |
| 36 | + ) |
| 37 | + |
| 38 | + |
| 39 | +def pytest_collection_modifyitems(config: Config, items: list[Item]) -> None: |
| 40 | + for item in items: |
| 41 | + posix_path, _, _ = item.reportinfo() |
| 42 | + if config.getoption("--flexible") and "tests/flexible" not in str(posix_path): |
| 43 | + item.add_marker(mark.skip(reason="Tests manually skipped in Flexible Mode")) |
| 44 | + elif config.getoption("--normal") and "tests/flexible" in str(posix_path): |
| 45 | + item.add_marker(mark.skip(reason="Tests manually skipped in Normal mode")) |
| 46 | + |
| 47 | + |
| 48 | +def pytest_configure(config: Config) -> None: |
| 49 | + flexible_mode: bool = config.getoption("flexible") # type: ignore[assignment] |
| 50 | + normal_mode: bool = config.getoption("normal") # type: ignore[assignment] |
| 51 | + if flexible_mode is True and normal_mode is True: |
| 52 | + raise UsageError("--flexible and --normal are mutually exclusive flags.") |
| 53 | + |
| 54 | + |
| 55 | +__all__: tuple[str, ...] = ("pytest_addoption", "pytest_collection_modifyitems", "pytest_configure") |
0 commit comments