Skip to content

Make tests deterministic with offline mocks#970

Open
LeMyst wants to merge 2 commits into
masterfrom
implement-mock-unittests
Open

Make tests deterministic with offline mocks#970
LeMyst wants to merge 2 commits into
masterfrom
implement-mock-unittests

Conversation

@LeMyst
Copy link
Copy Markdown
Owner

@LeMyst LeMyst commented May 6, 2026

Add a pytest conftest fixture and helper fakes to mock MediaWiki/Wikibase network calls for offline testing (test/conftest.py). Update multiple tests to use these mocks and unittest.mock.patch:

  • test_entity_item.py: add fake entity builders, fake _get/edit implementations, a fake FastRun container, and setUp/tearDown to patch network interactions; adjust write test and remove external HTTP dependency.
  • test_wbi_backoff.py: patch requests.get to simulate HTTP responses and patch login to raise errors for deterministic backoff behavior.
  • test_wbi_core.py: initialize shared common_item in setUpClass.
  • test_wbi_helpers.py: provide a fake mediawiki_api_call helper and use monkeypatch in tests (connection, sparql, format2wbi, user agent, allow_anonymous) to avoid real network calls.
  • test_wbi_login.py: add a FakeResponse and patch Session.post, Handshaker, and login methods to simulate various login failure modes deterministically.

Overall this change refactors tests to be network-independent and more reliable in CI/local runs.

Copilot AI review requested due to automatic review settings May 6, 2026 21:39
Comment thread test/test_wbi_helpers.py Dismissed
Comment thread test/test_wbi_login.py Fixed
@LeMyst LeMyst force-pushed the implement-mock-unittests branch from 4861fca to fe70123 Compare May 6, 2026 21:44
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR aims to make the test suite deterministic and runnable offline by introducing centralized pytest-level network fakes (via test/conftest.py) and refactoring several tests to patch network interactions instead of calling external services.

Changes:

  • Added an autouse pytest fixture providing offline fakes for entity fetching, MediaWiki API helper calls, and SPARQL/FastRun interactions.
  • Refactored multiple tests to use monkeypatch/unittest.mock.patch and local fake responses instead of real HTTP calls.
  • Adjusted some test initialization logic to reduce import-time side effects.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
test/conftest.py Adds autouse offline network mocks + fake entity/SPARQL payloads for deterministic tests.
test/test_entity_item.py Adds per-test patching of entity _get, edit/write behavior, and FastRun container to remove external HTTP dependency.
test/test_wbi_backoff.py Patches requests.get and login to simulate deterministic retry/backoff behavior.
test/test_wbi_core.py Moves common_item initialization to setUpClass.
test/test_wbi_helpers.py Introduces a fake MediaWiki API helper and uses monkeypatching to avoid real network calls in helper tests.
test/test_wbi_login.py Adds fake responses and patches session/login methods to deterministically simulate login failure modes.
Comments suppressed due to low confidence (1)

test/conftest.py:469

  • The autouse offline_network_mocks fixture is function-scoped (default). That means it will not apply to module import-time code or unittest.TestCase.setUpClass executions, so some tests can still perform real network calls even with the fixture present. Consider documenting this limitation and/or switching to a longer-lived patching approach (e.g., a session-scoped fixture using pytest.MonkeyPatch()), or ensuring tests don’t do network work in setUpClass/import time.
def offline_network_mocks(monkeypatch, request):
    module_name = request.module.__name__ if request.module else ''
    if module_name in SKIP_OFFLINE_PATCH_MODULES:
        return

    monkeypatch.setattr('wikibaseintegrator.entities.baseentity.BaseEntity._get', fake_baseentity_get)
    monkeypatch.setattr('wikibaseintegrator.wbi_helpers.mediawiki_api_call_helper', fake_mediawiki_api_call_helper)
    monkeypatch.setattr('wikibaseintegrator.wbi_helpers.execute_sparql_query', fake_execute_sparql_query)
    monkeypatch.setattr('wikibaseintegrator.wbi_fastrun.execute_sparql_query', fake_execute_sparql_query)
    monkeypatch.setattr('wikibaseintegrator.wbi_fastrun.FastRunContainer.get_prop_datatype', fake_get_prop_datatype)

Comment thread test/test_wbi_helpers.py Outdated
Comment thread test/test_wbi_helpers.py Outdated
Comment thread test/test_wbi_core.py Outdated
@LeMyst LeMyst force-pushed the implement-mock-unittests branch 2 times, most recently from 6dffbe7 to a29beed Compare May 6, 2026 21:53
@LeMyst
Copy link
Copy Markdown
Owner Author

LeMyst commented May 9, 2026

@copilot rebase

Copilot stopped work on behalf of LeMyst due to an error May 9, 2026 03:09
LeMyst added 2 commits May 9, 2026 05:11
Add a pytest conftest fixture and helper fakes to mock MediaWiki/Wikibase network calls for offline testing (test/conftest.py). Update multiple tests to use these mocks and unittest.mock.patch:

- test_entity_item.py: add fake entity builders, fake _get/edit implementations, a fake FastRun container, and setUp/tearDown to patch network interactions; adjust write test and remove external HTTP dependency.
- test_wbi_backoff.py: patch requests.get to simulate HTTP responses and patch login to raise errors for deterministic backoff behavior.
- test_wbi_core.py: initialize shared common_item in setUpClass.
- test_wbi_helpers.py: provide a fake mediawiki_api_call helper and use monkeypatch in tests (connection, sparql, format2wbi, user agent, allow_anonymous) to avoid real network calls.
- test_wbi_login.py: add a FakeResponse and patch Session.post, Handshaker, and login methods to simulate various login failure modes deterministically.

Overall this change refactors tests to be network-independent and more reliable in CI/local runs.
@LeMyst LeMyst force-pushed the implement-mock-unittests branch from 11d2889 to 051ee3b Compare May 9, 2026 03:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants