Skip to content

Refactor test_init Home Assistant stubs into fixtures#133

Open
eXPerience83 wants to merge 1 commit into
mainfrom
codex/refactor-tests/test_init.py-to-use-pytest-fixtures
Open

Refactor test_init Home Assistant stubs into fixtures#133
eXPerience83 wants to merge 1 commit into
mainfrom
codex/refactor-tests/test_init.py-to-use-pytest-fixtures

Conversation

@eXPerience83
Copy link
Copy Markdown
Owner

Motivation

  • Reduce import-order coupling by removing module-level sys.modules mutation in tests/test_init.py and instead install Home Assistant stubs using pytest fixtures and monkeypatch.
  • Limit the surface area of test stubbing so the integration is imported only after the minimal required stubs are in place, preserving existing integration behavior and assertions.

Description

  • Replaced import-time stub installation in tests/test_init.py with a fixture stub_init_ha_modules(monkeypatch) that installs only the modules needed by custom_components.pollenlevels.__init__ using monkeypatch.setitem(sys.modules, ...) and the helpers in tests/_ha_stubs.py.
  • Added an autouse fixture integration_modules(stub_init_ha_modules) which calls clear_integration_modules(monkeypatch=...) and then imports custom_components.pollenlevels.__init__ and custom_components.pollenlevels.const after stubs are installed, and sets _BaseDataUpdateCoordinator to the local stub.
  • Moved small fake classes and helper functions into the test file as readable local definitions and avoided a mega-stub by only exposing what __init__ requires.
  • Preserved all existing test names and assertions (including force_update redaction and coordinator behaviors) while ensuring the tests can run independently of import order.

Testing

  • Ran compilation check with python -m compileall -q custom_components tests which succeeded.
  • Ran lint/import-order checks with ruff check . which succeeded.
  • Ran formatting checks with black --check . which succeeded.
  • Ran pytest -q tests/test_init.py which passed (33 passed).
  • Ran pytest -q tests/test_button.py which passed (7 passed).
  • Ran the full test suite with pytest -q which passed (310 passed).

Codex Task

@qodo-code-review
Copy link
Copy Markdown
Contributor

Qodo reviews are paused for this user.

Troubleshooting steps vary by plan Learn more →

On a Teams plan?
Reviews resume once this user has a paid seat and their Git account is linked in Qodo.
Link Git account →

Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center?
These require an Enterprise plan - Contact us
Contact us →

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

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.

Comment thread tests/test_init.py
Comment on lines +240 to +241
integration = importlib.import_module("custom_components.pollenlevels.__init__")
const = importlib.import_module("custom_components.pollenlevels.const")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

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
  1. 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.

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

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

💡 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".

Comment thread tests/test_init.py
Comment on lines +158 to +162
@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)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge 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 👍 / 👎.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant