Skip to content

Make it possible to run Pytest in parallel#1291

Draft
mhucka wants to merge 10 commits intoquantumlib:mainfrom
mhucka:parallel-pytest
Draft

Make it possible to run Pytest in parallel#1291
mhucka wants to merge 10 commits intoquantumlib:mainfrom
mhucka:parallel-pytest

Conversation

@mhucka
Copy link
Copy Markdown
Contributor

@mhucka mhucka commented Apr 27, 2026

This PR makes 2 sets of changes that together allow parallel Pytest execution via the pytest-xdist plugin.

  • Set a fixed random seed for testing, in src/openfermion/conftest.py. This is necessary for pytest-xdist worker processes to be able to perform identical test collections. Otherwise, different workers might generate different values and you end up with "Inconsistent collection" errors during execution.

  • Change some tests that were writing to fixed-path files and folders. Those needed to be changed to use temporary directories so that parallel Pytest processes don't collide trying to write to the same things.

mhucka added 10 commits April 27, 2026 22:33
Setting the seed in conftest.py like this makes it possible to run
pytest in parallel with the `-n` option from pytest-xdist.
Some of the test code did not work with parallel pytest execution
because it attempted to write to and delete the same files in the shared
`src/openfermion/testing/data` directory. Specifically,
`SaveLoadOperatorTest` and `MolecularDataTest` used fixed
filenames:(like `test_file.data` and `dummy_molecule.hdf5`) which led to
collisions and errors during parallel execution.

The changes implemented here are:

* Refactor `SaveLoadOperatorTest` in `operator_utils_test.py` to use a
  unique temporary directory for each test instance through the use of
  `tempfile.mkdtemp()`.

* Refactor `MolecularDataTest` in `molecular_data_test.py` to use a
  temporary directory for tests that perform disk writes
  (`test_dummy_save` and `test_abstract_molecule`).
Although `check/pytest` accepts command line arguments,
`pytest-and-incremental-coverage` does not, so we can't instruct
contributors to add `-n auto` to it to run it in parallel.

Since it's something that is less commonly run by contributors, I took
the more expedient route of simply adding the parallelism flag directly
to the script.
Now that parallel Pytest execution works for OpenFermion, we can tell
users about it.
Turns out the use of the `pytest-randomly` plugin and using
`np.random.default_rng` seems to make it possible to run pytest in
parallel. Evidently, that seeds every test individually based on a
master seed, and this solves the xdist collection issue automatically
without creating inter-test dependencies.
Regenerate dependency files after adding pytest-randomly.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant