[MNT]Replaced Live Test-Server Calls with Mocks in Unit Tests on tests/test_study/test_study_function.py module#1684
Open
Sachin0496 wants to merge 1 commit intoopenml:mainfrom
Open
Conversation
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.
Metadata
Details
What does this PR implement?
This PR completely overhauls the mocking strategy in
tests/test_study/test_study_functions.py.Previously, the tests relied heavily on live hits to the OpenML test server and a massive amount of cluttered, inline XML string generation. This PR extracts all of that logic into a dedicated, standalone
StudyMockServerhelper class that dynamically generates the necessary mocked API responses.Why is this change necessary?
The existing study tests had two major problems:
Impact of this PR:
@mock.patch.object(requests.Session), reducing the load on live serverImplementation Overview
Mock Strategy
All study test-server interactions (GET, POST, DELETE) are intercepted using
@mock.patch.object(requests.Session). However, the setup is now entirely delegated:StudyMockServerClass: A new static helper class that encapsulates all XML generation methods (build_study_xml,build_runs_xml, etc.).side_effectassignments into descriptive static methods (e.g.,StudyMockServer.setup_publish_study_mocks).run_idsSupport: Fixed a bug in the old XML generation by updatingbuild_study_xmlto accept an explicit list ofrun_ids. This ensures the mock server returns the exact IDs the test expects after an attach/detach operation without failing assertions.Refactored Test Cases
The following test methods were dramatically simplified by offloading their mock setups to the
StudyMockServer:test_publish_benchmark_suite: Mocks centralized viaStudyMockServer.setup_publish_benchmark_suite_mocks.test_publish_study: Mocks centralized viaStudyMockServer.setup_publish_study_mocks. Fixed the bug causing assertion failures on detached run IDs.test_publish_empty_study_explicit&implicit: Cleaned up copy-paste errors and centralized the empty XML mock generation.test_study_attach_illegal: Mocks centralized viaStudyMockServer.setup_study_attach_illegal_mocks, properly injectingopenml.exceptions.OpenMLServerExceptionto simulate server-side validation errors.What Was NOT Changed
test_get_study_old,test_get_suite) remain untouched to continue validating real server behavior.pytest.mark.test_serverand skips such as@unittest.skipwere left untouched , still mocks have been implemented for the sameHow to Reproduce
Run the study test suite locally to verify the speedup and clean output. No OpenML test server access is required for these specific tests anymore: