How to avoid packaging transitive dependencies in py_test when using mocks? #3078
Unanswered
Yanpei-Wang
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hi everyone,
I’m running into an issue with Bazel + rules_python and I hope someone here can point me in the right direction.
I’m testing a Flask API defined in historical_api.py. In my test, I’ve mocked out all the methods it calls (e.g., sync_microsoft_members_to_redis()), so the test doesn’t actually need the real implementation or its heavy dependencies like the Microsoft Graph SDK.
However, when I inspect bazel-bin/, I see that all of these transitive dependencies are still being packaged. This bloats the test’s runtime environment and consumes a lot of CPU and memory in CI.
Is there a way to make Bazel only package the dependencies actually used in the test, or exclude transitive deps if they are mocked?
Here’s a simplified version of my BUILD file:
The API itself looks like this:
In my test, I use unittest.mock to patch sync_microsoft_members_to_redis(), so I don’t expect Bazel to pull in the actual microsoft_ldap_fetcher.
Is there a recommended way in Bazel (or rules_python) to stub or exclude these transitive deps from the test packaging?
Any advice or examples would be hugely appreciated!
Thanks 🙏
This is Link:
https://github.com/Circle-Cat/purrf/blob/main/tests/frontend_service_test/BUILD
What I’ve tried so far:
I experimented with creating a separate stub module where I define “dummy” functions (with the same names as the real ones). I then point my test to import these stubs instead of the real implementation.
For example:
in microsoft_ldap_fetcher_stub.py
in historical_api.py
BUILD
This approach does reduce the dependencies included in the test, but it feels quite hacky since I have to ensure the import paths align perfectly between the test and production code.
Beta Was this translation helpful? Give feedback.
All reactions