Skip to content

Commit a09875b

Browse files
authored
Albrja/mic 6801/pytest xdist (#273)
Albrja/mic 6801/pytest xdist Add pytest xdist feature to parallelize test suites - *Category*: Test - *JIRA issue*: https://jira.ihme.washington.edu/browse/MIC-6801 - *Research reference*: <!--Link to research documentation for code --> Changes and notes -add pytest-xdist to install requirements -add conftest hook to automatically discover number of CPUs allocated on srun on slurm -allow for parallelization on local builds but default to running tests linearly Verification and Testing -built new environment locally and on the cluster. Saw expected default behavior and tested -n flag *** REMINDER *** model results directory is up to date - [X ] all tests pass (`pytest --runslow` with both *vivarium_gates_mncnh_artifact* and *vivarium_gates_mncnh_simulation*)
1 parent 3b4edf9 commit a09875b

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ requires = ["packaging", "setuptools"]
33

44
[tool.pytest.ini_options]
55
testpaths = ["tests"]
6+
addopts = "-nauto"
67

78
[tool.black]
89
line_length = 94

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
"papermill",
6464
"jupyterlab",
6565
"vivarium_testing_utils",
66+
"pytest-xdist",
6667
]
6768
validation_requirements = ["vivarium_testing_utils[validation]"]
6869
lint_requirements = [

tests/conftest.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,28 @@ def pytest_collection_modifyitems(config, items):
8787
item.add_marker(skip_slow)
8888

8989

90+
def pytest_xdist_auto_num_workers(config):
91+
"""Automatically determine the number of workers for pytest-xdist.
92+
93+
- On SLURM: Use CPUs allocated to the job (via SLURM environment variables)
94+
- Not on SLURM: Return 1 (no parallelization by default)
95+
- Users can override by explicitly passing -n flag to pytest
96+
"""
97+
cpus = 1
98+
if IS_ON_SLURM:
99+
# On SLURM clusters, use the number of CPUs allocated to the job
100+
# Check SLURM environment variables in order of preference
101+
slurm_cpus = os.environ.get("SLURM_CPUS_PER_TASK") or os.environ.get(
102+
"SLURM_CPUS_ON_NODE"
103+
)
104+
if slurm_cpus:
105+
cpus = int(slurm_cpus)
106+
# Fallback to total CPUs if SLURM vars not found (shouldn't happen)
107+
cpus = os.cpu_count()
108+
109+
return cpus
110+
111+
90112
@pytest.fixture(scope="session")
91113
def model_spec_path() -> Path:
92114
repo_path = paths.BASE_DIR

0 commit comments

Comments
 (0)