Skip to content

Make pytest functions reproducible (1/2)#1036

Open
jwgarber wants to merge 2 commits into
malariagen:masterfrom
jwgarber:numpy-rng2
Open

Make pytest functions reproducible (1/2)#1036
jwgarber wants to merge 2 commits into
malariagen:masterfrom
jwgarber:numpy-rng2

Conversation

@jwgarber
Copy link
Copy Markdown

@jwgarber jwgarber commented Mar 4, 2026

This converts about half of the pytest functions so their random number generation is reproducible. The name of the pytest function test case is used to seed the RNG to ensure all functions generate independent random numbers. There are a few instances where the Numpy RNG has slightly different behaviour than the Python ones (e.g. random.randint() includes the endpoint by default while rng.integers() does not), so that had to be adapted. There are also a few cases where the numpy types have to be cast to Python types to make the type checker happy (e.g. np.int64 -> int), otherwise it's quite similar.

This was tested by trying to reproduce this flaky test failure. After about 140 trials I was able to find a random seed that caused the crash (325228949307297790525745584766237006555 in this case), and I am able to reliably reproduce this crash with that seed. Once this is finished the next job will be debugging that.

There are a lot of functions to convert, so the other half will be done in another PR since this one is large already.

While testing this I noticed that the global seed wasn't being printed in the workflow logs, even though it is when you run tests individually. Apparently this is because the seed needs to be printed from the top-level conftest.py, otherwise it won't get picked up when running pytest -v tests. Anyway, I rearranged some of the files so it works now.

@jwgarber jwgarber force-pushed the numpy-rng2 branch 2 times, most recently from c2cf2bc to 141c154 Compare March 4, 2026 18:47
@jwgarber
Copy link
Copy Markdown
Author

jwgarber commented Mar 4, 2026

@jonbrenas This is ready now, follow up to #1008

@codecov
Copy link
Copy Markdown

codecov Bot commented Mar 9, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.26%. Comparing base (ab59c87) to head (2262070).
⚠️ Report is 50 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1036      +/-   ##
==========================================
+ Coverage   90.25%   90.26%   +0.01%     
==========================================
  Files          51       51              
  Lines        5631     5659      +28     
==========================================
+ Hits         5082     5108      +26     
- Misses        549      551       +2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@jonbrenas
Copy link
Copy Markdown
Collaborator

Thanks @jwgarber. Why do the new conftest and rng_util need to be outside of anoph if all the tests using the new rig are in `anoph?

@jwgarber jwgarber force-pushed the numpy-rng2 branch 2 times, most recently from 8922101 to 2262070 Compare March 9, 2026 19:56
@jwgarber
Copy link
Copy Markdown
Author

jwgarber commented Mar 9, 2026

Unfortunately the pytest_report_header function needs to be located in the tests/conftest.py in order for the global seed to be printed when running all tests. E.g. pytest -v tests --ignore tests/integration will run the anoph tests, but the report header in tests/anoph/conftest.py will be ignored, so that's why we need the other file. I was able to remove rng_util though and put it back where it used to be.

@jwgarber
Copy link
Copy Markdown
Author

@jonbrenas Any other comments on this?

Copy link
Copy Markdown
Collaborator

@jonbrenas jonbrenas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Jacob Garber added 2 commits May 11, 2026 11:04
This converts about half of the pytest functions so their random number
generation is reproducible. The name of the pytest function is used to
seed the RNG to ensure all functions generate independent random
numbers.
This needs to be in a top-level conftest.py, otherwise it won't always
get printed on startup.
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.

2 participants