[MNT]Replaced Live Test-Server Calls with Mocks in Unit Tests on tests/test_study/test_study_function.py module
Metadata
- Reference Issue: Fixes #1649
- New Tests Added: No
- Documentation Updated: No
- Change Log Entry: Refactored study tests by introducing a centralized StudyMockServer that mocks requests.Session calls, replacing live test-server interactions. This reduced the request load on test_server and production_server, improved maintainability, and reduced average test runtime from ~30s to ~8s.
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 StudyMockServer helper class that dynamically generates the necessary mocked API responses.
Why is this change necessary?
The existing study tests had two major problems:
- Performance & Reliability: Relying on the live OpenML test server meant tests were slow, flaky, and put unnecessary load on the server.
Impact of this PR:
- Massive Speedup: By properly mocking these server interactions, the average test execution time for this suite dropped from ~30s to ~8s.
- Reduced Server Load: Tests have been mocked/patched with
@mock.patch.object(requests.Session), reducing the load on live server
Implementation 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.).- Dedicated Mock Setups: Extracted massive
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 Logic: The actual test execution, API calls being tested, and assertions remain 100% identical.
- Production Server Tests: Tests hitting the real server (e.g.,
test_get_study_old,test_get_suite) remain untouched to continue validating real server behavior. - Marks and skips: The server markers such as
pytest.mark.test_serverand skips such as@unittest.skipwere left untouched , still mocks have been implemented for the same
How 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:
pytest tests/test_study/test_study_functions.py