LCOV - code coverage report
Current view: top level - tests - conftest.py (source / functions) Hit Total Coverage
Test: coverage.info Lines: 30 32 93.8 %
Date: 2025-09-29 10:28:29 Functions: 5 10 50.0 %

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

Generated by: LCOV version 1.16