[202605] fix(tests): centralize sonic_platform mock in conftest.py#4610
Open
rookie-who wants to merge 1 commit into
Open
[202605] fix(tests): centralize sonic_platform mock in conftest.py#4610rookie-who wants to merge 1 commit into
rookie-who wants to merge 1 commit into
Conversation
sonic_platform is a hardware-specific package unavailable in the build/test environment. Tests that use @patch('sonic_platform...') need it pre-injected into sys.modules or the decorator raises ModuleNotFoundError. Previously every test file had to know about this and inject the mock individually — an undocumented pattern scattered across 8+ files. When contributors missed it (most recently sed_test.py in sonic-net#4185), the sonic-buildimage submodule update PR failed across ALL platforms: sonic-net/sonic-buildimage#27773 (aspeed, broadcom, marvell, alpinevs, vpp, vs — 8 tests FAILED) This was already fixed reactively at least twice: - sonic-net#4583 (sed_test.py — same ModuleNotFoundError) - sonic-net#4530 (sfp/pcie tests — mock leakage under xdist) - sonic-net#4366 (ssdutil_test.py — module cache pollution) Add a global autouse fixture _ensure_sonic_platform_mock() in tests/conftest.py that re-injects the MagicMock stubs after _reset_between_files() clears them. This covers all tests that use @patch('sonic_platform...') at decoration time. Test files that import sonic_platform at module level (fwutil_test, psuutil_test, sfputil_test) retain their own sys.modules injection before the import line, since module collection happens before fixtures run. Remove the now-redundant per-file injections and fixtures from: sed_test.py, decode_syseeprom_test.py, chassis_modules_test.py, psushow_test.py, ssdutil_test.py. Signed-off-by: rookie-who <rookie-who@users.noreply.github.com>
Collaborator
|
/azp run |
|
Azure Pipelines successfully started running 1 pipeline(s). |
vaibhavhd
approved these changes
Jun 12, 2026
This was referenced Jun 12, 2026
vaibhavhd
approved these changes
Jun 12, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why I did it
Cherry-pick of #4601 to 202605.
tests/sed_test.pyuses@patch('sonic_platform.platform.Platform')butsonic_platformis a platform-specific package not available in the build environment. This causesModuleNotFoundErrorand fails the sonic-utilities wheel build, blocking the sonic-utilities submodule update (#27773 in sonic-buildimage).How I did it
Add a session-scoped
sonic_platformmock toconftest.pythat installs a fakesonic_platform.platform.Platformmodule before any test imports it. Remove per-file mock setups that are now redundant.Cherry-pick of 37e2eee from master.
How to verify it
pytest tests/sed_test.pypasses withoutsonic_platforminstalled.