Skip to content

Conversation

Copy link

Copilot AI commented Oct 5, 2025

Problem

The test suite was experiencing redundant setup and teardown processes during testing due to dataset loading fixtures being executed repeatedly for each test. This led to inefficient test execution times, especially for tests that rely on the same base datasets.

Solution

Implemented pytest fixtures with scope='session' for base dataset loading operations to optimize test performance. This change ensures that expensive dataset loading operations are performed only once per test session rather than for each individual test.

Changes Made

🔧 12 Base Fixtures Optimized

Converted the following dataset loading fixtures to session scope:

  • PM_ds3v_initialized_1d - MPI Perfect-model initialized timeseries
  • PM_ds_initialized_3d_full - MPI Perfect-model global maps
  • PM_ds3v_control_1d - MPI Perfect-model control timeseries
  • PM_ds_control_3d_full - MPI Perfect-model control global maps
  • hind_ds_initialized_1d - CESM-DPLE hindcast timeseries
  • hind_ds_initialized_3d_full - CESM-DPLE hindcast Pacific maps
  • hist_ds_uninitialized_1d - CESM-LE uninitialized historical timeseries
  • reconstruction_ds_1d - CESM-FOSI reconstruction timeseries
  • reconstruction_ds_3d_full - CESM-FOSI reconstruction Pacific maps
  • observations_ds_1d - Historical observations timeseries
  • hindcast_S2S_Germany - S2S ECMWF hindcasts with observations
  • hindcast_NMME_Nino34 - NMME hindcasts with IOv2 observations

📚 Documentation Added

  • Added comprehensive comments explaining the session scope optimization strategy
  • Updated fixture docstrings to indicate session scope for performance
  • Documented the inheritance of performance benefits by dependent fixtures

Performance Benefits

  • Eliminates redundant data loading: Datasets load once per test session instead of per test
  • Automatic optimization inheritance: 10+ dependent fixtures benefit without modification
  • Zero breaking changes: Existing test interface remains unchanged
  • Significant speedup: Particularly beneficial for test suites with heavy dataset usage

Implementation Details

The optimization targets only base fixtures that:

  1. Load datasets via load_dataset() calls
  2. Have no fixture dependencies themselves
  3. Are used by multiple other fixtures or tests

Dependent fixtures automatically inherit the performance benefits while maintaining their existing function-level scope, ensuring test isolation where needed.

Example of the optimization:

# Before: Function scope (default)
@pytest.fixture()
def PM_ds3v_initialized_1d():
    return load_dataset("MPI-PM-DP-1D").isel(area=1, period=-1, drop=True)

# After: Session scope
@pytest.fixture(scope='session')
def PM_ds3v_initialized_1d():
    """Session-scoped for performance optimization."""
    return load_dataset("MPI-PM-DP-1D").isel(area=1, period=-1, drop=True)

This change maintains full API compatibility while significantly improving test suite efficiency and maintainability.

Original prompt

Currently, the repository does not utilize pytest fixtures with a session scope, which can lead to redundant setup and teardown processes during testing. To address this, pytest fixtures with a session scope should be implemented to optimize the testing process.

This involves:

  1. Identifying setup code that is repeatedly executed in multiple tests.
  2. Refactoring the setup code into pytest fixtures.
  3. Applying a scope='session' to these fixtures to ensure they are initialized only once per test session.
  4. Updating the tests to use these fixtures, ensuring compatibility and no breaking changes.

This change should improve the efficiency and maintainability of the test suite.

*This pull request was created as a result of the following prompt from Copilot chat.* > Currently, the repository does not utilize pytest fixtures with a session scope, which can lead to redundant setup and teardown processes during testing. To address this, pytest fixtures with a session scope should be implemented to optimize the testing process. > > This involves: > 1. Identifying setup code that is repeatedly executed in multiple tests. > 2. Refactoring the setup code into pytest fixtures. > 3. Applying a `scope='session'` to these fixtures to ensure they are initialized only once per test session. > 4. Updating the tests to use these fixtures, ensuring compatibility and no breaking changes. > > This change should improve the efficiency and maintainability of the test suite.

💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Copilot AI changed the title [WIP] Add pytest fixtures with session scope Optimize test performance with session-scoped pytest fixtures Oct 5, 2025
Copilot AI requested a review from aaronspring October 5, 2025 12:21
Copilot finished work on behalf of aaronspring October 5, 2025 12:21
Copy link
Collaborator

@aaronspring aaronspring left a comment

Choose a reason for hiding this comment

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

@aaronspring aaronspring marked this pull request as ready for review October 6, 2025 14:29
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.

2 participants