openml-python icon indicating copy to clipboard operation
openml-python copied to clipboard

[MNT]Replaced Live Test-Server Calls with Mocks in Unit Tests on tests/test_study/test_study_function.py module

Open Sachin0496 opened this issue 9 months ago • 0 comments

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:

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

  1. StudyMockServer Class: A new static helper class that encapsulates all XML generation methods (build_study_xml, build_runs_xml, etc.).
  2. Dedicated Mock Setups: Extracted massive side_effect assignments into descriptive static methods (e.g., StudyMockServer.setup_publish_study_mocks).
  3. run_ids Support: Fixed a bug in the old XML generation by updating build_study_xml to accept an explicit list of run_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 via StudyMockServer.setup_publish_benchmark_suite_mocks.
  • test_publish_study: Mocks centralized via StudyMockServer.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 via StudyMockServer.setup_study_attach_illegal_mocks, properly injecting openml.exceptions.OpenMLServerException to 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_server and skips such as @unittest.skip were 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

Sachin0496 avatar Feb 21 '26 16:02 Sachin0496