Make tests deterministic with offline mocks#970
Open
LeMyst wants to merge 2 commits into
Open
Conversation
4861fca to
fe70123
Compare
Contributor
There was a problem hiding this comment.
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.patchand 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_mocksfixture is function-scoped (default). That means it will not apply to module import-time code orunittest.TestCase.setUpClassexecutions, 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 usingpytest.MonkeyPatch()), or ensuring tests don’t do network work insetUpClass/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)
6dffbe7 to
a29beed
Compare
Owner
Author
|
@copilot rebase |
Copilot stopped work on behalf of
LeMyst due to an error
May 9, 2026 03:09
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.
11d2889 to
051ee3b
Compare
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.
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:
Overall this change refactors tests to be network-independent and more reliable in CI/local runs.