Refactor test_init Home Assistant stubs into fixtures#133
Conversation
Qodo reviews are paused for this user.Troubleshooting steps vary by plan Learn more → On a Teams plan? Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center? |
There was a problem hiding this comment.
Code Review
This pull request refactors the test initialization in tests/test_init.py by moving global Home Assistant module stubbing and integration imports into pytest fixtures (stub_init_ha_modules and integration_modules) using monkeypatch for better test isolation. The reviewer suggested explicitly clearing the integration modules from sys.modules and importing them in dependency order (const before integration) to prevent relative imports from picking up stale module identities.
| integration = importlib.import_module("custom_components.pollenlevels.__init__") | ||
| const = importlib.import_module("custom_components.pollenlevels.const") |
There was a problem hiding this comment.
According to the general rules, we should explicitly clear modules from sys.modules before reloading them in tests, and reload them in dependency order to prevent relative imports during execution from picking up stale module or class identities. Since the integration's __init__.py imports and depends on const.py, both modules should be cleared from sys.modules first, and then const should be imported before the integration module.
sys.modules.pop("custom_components.pollenlevels.const", None)
sys.modules.pop("custom_components.pollenlevels", None)
const = importlib.import_module("custom_components.pollenlevels.const")
integration = importlib.import_module("custom_components.pollenlevels")References
- Explicitly clear modules from sys.modules before reloading them in tests, and reload them in dependency order, to prevent relative imports during execution from picking up stale module or class identities.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: a2e70f4c0f
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| @pytest.fixture | ||
| def stub_init_ha_modules(monkeypatch: pytest.MonkeyPatch) -> None: | ||
| """Install only the Home Assistant stubs needed by ``__init__`` imports.""" | ||
| clear_integration_modules(monkeypatch=monkeypatch) | ||
| stub_custom_components_packages(root=ROOT, monkeypatch=monkeypatch) |
There was a problem hiding this comment.
Restore collection-time stubs for dependent test modules
Because these stubs are now installed only from a pytest fixture, they do not exist during collection of other test files. In the inspected order pytest -q tests/test_init.py tests/test_options_flow.py, collection of tests/test_options_flow.py imports custom_components.pollenlevels.const, which loads the package __init__ and fails with ModuleNotFoundError: No module named 'homeassistant' before this fixture can run. This makes otherwise valid focused test selections order-dependent again; move the shared import stubs to collection-time setup or make the importing test modules install their own stubs before module-level integration imports.
Useful? React with 👍 / 👎.
Motivation
sys.modulesmutation intests/test_init.pyand instead install Home Assistant stubs using pytest fixtures andmonkeypatch.Description
tests/test_init.pywith a fixturestub_init_ha_modules(monkeypatch)that installs only the modules needed bycustom_components.pollenlevels.__init__usingmonkeypatch.setitem(sys.modules, ...)and the helpers intests/_ha_stubs.py.integration_modules(stub_init_ha_modules)which callsclear_integration_modules(monkeypatch=...)and then importscustom_components.pollenlevels.__init__andcustom_components.pollenlevels.constafter stubs are installed, and sets_BaseDataUpdateCoordinatorto the local stub.__init__requires.force_updateredaction and coordinator behaviors) while ensuring the tests can run independently of import order.Testing
python -m compileall -q custom_components testswhich succeeded.ruff check .which succeeded.black --check .which succeeded.pytest -q tests/test_init.pywhich passed (33 passed).pytest -q tests/test_button.pywhich passed (7 passed).pytest -qwhich passed (310 passed).Codex Task