LCOV - code coverage report
Current view: top level - tests - conftest.py (source / functions) Hit Total Coverage
Test: coverage.info Lines: 38 40 95.0 %
Date: 2025-12-08 07:47:12 Functions: 6 12 50.0 %

          Line data    Source code
       1             : # SPDX-FileCopyrightText: 2024 PairInteraction Developers
       2             : # SPDX-License-Identifier: LGPL-3.0-or-later
       3             : 
       4           1 : from __future__ import annotations
       5             : 
       6           1 : import multiprocessing
       7           1 : import os
       8           1 : from typing import TYPE_CHECKING
       9             : 
      10           1 : import pytest
      11           1 : from pint import UnitRegistry
      12             : 
      13             : if TYPE_CHECKING:
      14             :     from _pytest.config import Config
      15             :     from _pytest.config.argparsing import Parser
      16             :     from pairinteraction_gui.app import Application
      17             : 
      18             :     from .utils import PairinteractionModule
      19             : 
      20             : 
      21           1 : def pytest_addoption(parser: Parser) -> None:
      22           1 :     parser.addoption("--generate-reference", action="store_true", default=False, help="Generate reference data")
      23           1 :     parser.addoption(
      24             :         "--database-dir",
      25             :         action="store",
      26             :         default=None,
      27             :         help="Path to the database directory",
      28             :     )
      29           1 :     parser.addoption("--download-missing", action="store_true", default=False, help="Download missing database files")
      30             : 
      31             : 
      32           1 : @pytest.fixture(scope="session")
      33           1 : def generate_reference(pytestconfig: Config) -> bool:
      34           1 :     return pytestconfig.getoption("--generate-reference")  # type: ignore [no-any-return]
      35             : 
      36             : 
      37           1 : @pytest.fixture(scope="session")
      38           1 : def ureg() -> UnitRegistry:
      39             :     """Create and return a UnitRegistry with atomic units."""
      40           1 :     return UnitRegistry(system="atomic")
      41             : 
      42             : 
      43           1 : def pytest_sessionstart(session: pytest.Session) -> None:
      44             :     """Initialize everything before the tests are run."""
      45           1 :     download_missing: bool = session.config.getoption("--download-missing")
      46           1 :     database_dir: str | None = session.config.getoption("--database-dir")
      47             : 
      48             :     # Disable the test mode of PairInteraction that would call _setup_test_mode
      49             :     # automatically. This would be necessary for testing the jupyter notebooks
      50             :     # but we want to call _setup_test_mode manually.
      51           1 :     test_mode = os.environ.get("PAIRINTERACTION_TEST_MODE", "1")
      52           1 :     os.environ["PAIRINTERACTION_TEST_MODE"] = "0"
      53             : 
      54             :     # Call _setup_test_mode manually with the given options
      55           1 :     from pairinteraction import _setup_test_mode
      56             : 
      57           1 :     _setup_test_mode(download_missing, database_dir)
      58             : 
      59             :     # Set the test mode environment variables, so they can be used by subprocesses
      60           1 :     os.environ["PAIRINTERACTION_TEST_MODE"] = test_mode
      61           1 :     if database_dir is not None:
      62           0 :         os.environ["PAIRINTERACTION_TEST_DOWNLOAD_MISSING"] = str(int(download_missing))
      63           0 :         os.environ["PAIRINTERACTION_TEST_DATABASE_DIR"] = database_dir
      64             : 
      65             :     # For pairinteraction_gui set the multiprocessing start method to "spawn" (see also pairinteraction_gui/__init__.py)
      66           1 :     multiprocessing.set_start_method("spawn")
      67             : 
      68             : 
      69           1 : @pytest.fixture(scope="session")
      70           1 : def qapp_cls() -> type[Application]:
      71             :     """Let the qapp and qtbot fixtures use our custom Application class."""
      72           1 :     from pairinteraction_gui.app import Application
      73             : 
      74           1 :     return Application
      75             : 
      76             : 
      77           1 : @pytest.fixture(params=["real", "complex"])
      78           1 : def pi_module(request: pytest.FixtureRequest) -> PairinteractionModule:
      79             :     """Import and return the pairinteraction module, either real or complex version."""
      80           1 :     use_real = request.param == "real"
      81           1 :     if use_real:
      82           1 :         import pairinteraction.real as pi_real
      83             : 
      84           1 :         return pi_real  # type: ignore [return-value]
      85             : 
      86           1 :     import pairinteraction as pi_complex
      87             : 
      88           1 :     return pi_complex

Generated by: LCOV version 1.16