HarvestForTwo / conftest.py
princessamygdala's picture
Claude Opus 5
Analysis pipeline: the data layer
d367697
Raw History Blame Contribute Delete
2 kB
# Registers NiceGUI's pytest fixtures (e.g. `user`) needed by test_block_labels.py.
# We import `user_plugin` directly (not the full `nicegui.testing.plugin`) because
# the full plugin also pulls in `screen_plugin`, which requires `selenium` — a
# browser-testing dependency this project doesn't install and doesn't need here.
import os
import pytest
# Only when nicegui is importable. The analysis package runs in the
# py3_afni_mid conda env (that is where the correct pyEM lives) and nicegui is
# not installed there, so declaring the plugin unconditionally made every
# analysis test fail at collection with an ImportError about the web app.
try:
import nicegui # noqa: F401
except ImportError:
pytest_plugins = []
else:
pytest_plugins = ["nicegui.testing.user_plugin"]
@pytest.fixture(autouse=True, scope="session")
def _no_gitlab_writes_from_tests():
"""Stop the test suite from writing to the real participant-data repo.
Several tests drive the app end to end (e.g. test_finish_reorder opens
`?pid=SMOKE_final_screen&dev=1`), which runs the genuine finish path:
export -> push_to_gitlab -> a real commit on GITLAB_PROJECT. Every full
`pytest` run was therefore adding SMOKE_* sessions and randomization_log
rows to the production dataset, indistinguishable at a glance from
participant data.
gitlab_save.push_to_gitlab() and append_to_log() both no-op when the token
or project is unset, so clearing them for the test session is enough --
no mocking, and the code under test still runs its real code path.
"""
saved = {k: os.environ.pop(k, None) for k in ("GITLAB_TOKEN", "GITLAB_PROJECT")}
# load_dotenv() elsewhere would put them straight back, so mark them empty
# rather than merely absent.
os.environ["GITLAB_TOKEN"] = ""
os.environ["GITLAB_PROJECT"] = ""
yield
for k, v in saved.items():
if v is None:
os.environ.pop(k, None)
else:
os.environ[k] = v