Optimize test performance with session-scoped pytest fixtures #877
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.
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 timeseriesPM_ds_initialized_3d_full- MPI Perfect-model global mapsPM_ds3v_control_1d- MPI Perfect-model control timeseriesPM_ds_control_3d_full- MPI Perfect-model control global mapshind_ds_initialized_1d- CESM-DPLE hindcast timeserieshind_ds_initialized_3d_full- CESM-DPLE hindcast Pacific mapshist_ds_uninitialized_1d- CESM-LE uninitialized historical timeseriesreconstruction_ds_1d- CESM-FOSI reconstruction timeseriesreconstruction_ds_3d_full- CESM-FOSI reconstruction Pacific mapsobservations_ds_1d- Historical observations timeserieshindcast_S2S_Germany- S2S ECMWF hindcasts with observationshindcast_NMME_Nino34- NMME hindcasts with IOv2 observations📚 Documentation Added
Performance Benefits
Implementation Details
The optimization targets only base fixtures that:
load_dataset()callsDependent fixtures automatically inherit the performance benefits while maintaining their existing function-level scope, ensuring test isolation where needed.
Example of the optimization:
This change maintains full API compatibility while significantly improving test suite efficiency and maintainability.
Original prompt
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.