Any tips for maximizing pytest memoization? What I...
# general
e
Any tips for maximizing pytest memoization? What I have seen in our monorepo with 2.29 and no special settings is that if any files change in a leaf-level module all tests re-run, but the tests for other modules are still kept in cache. That’s already a big improvement, but wondering if there is any further fine-tuning possible to get Pants to recognize that only a subset of a module tests need to be re-run. I understand that means Pants needs to understand which tests use which source files and that this is challenging; re-running all tests in the module and all modules downstream of it is the conservative choice.
h
Currently this is all done at the file level, so there is no mechanism to say "even though this test file depends on this source file, only a subset of its tests actually need to run when that source file changes".
The "workaround" could be to manually split test files up into multiple files, conforming to dep boundaries in some sensible way.
Implementing this as an automatic feature could involve pretending that each test lives in its own fake module, but figuring out which imports go in each of those fake modules would take some doing.
🙏 1
e
Just to be clear, I should not expect a test to re-run if none of the source files it depends on have changed? That’s not what we’re seeing: if any file under src/python changes, all tests re-run, whether or not they have a direct or indirect dependency on the file that was touched. Made me wonder if we were missing something.
h
Correct. If the changed files are not in the (transitive) dependencies of the test then it should not rerun. You can investigate the dependency paths with
pants dependencies
and
pants dependents
👀 1
e
Thanks @happy-kitchen-89482 that made a big difference: I found that we had a sub-module (with very expensive tests) that had an overly wide dependency rule in its
BUILD
for unit tests. Cache hits are much higher now.
💥 1