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