climate_ref.testing
#
Testing utilities for running and validating diagnostic test cases.
This module provides:
- Path resolution for package-local test data (catalogs, regression data)
- Sample data fetching utilities
- TestCaseRunner for executing diagnostics with test data
- Drift-checking helpers for test-case regression baselines
(assert_test_case_no_drift and the create_no_drift_test factory
used by every provider's integration test module)
TEST_DATA_DIR = _determine_test_directory()
module-attribute
#
Path to the centralised test data directory (for sample data).
TestCaseRunner
#
Helper class for running diagnostic test cases.
This runner handles: - Running the diagnostic with pre-resolved datasets - Setting up the execution definition
Source code in packages/climate-ref/src/climate_ref/testing.py
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 | |
run(diagnostic, test_case_name='default', output_dir=None, clean=False)
#
Run a specific test case for a diagnostic.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
diagnostic
|
Diagnostic
|
The diagnostic to run |
required |
test_case_name
|
str
|
Name of the test case to run (default: "default") |
'default'
|
output_dir
|
Path | None
|
Optional output directory for results |
None
|
clean
|
bool
|
If True, delete the output directory before running |
False
|
Returns:
| Type | Description |
|---|---|
ExecutionResult
|
The result of running the diagnostic |
Raises:
| Type | Description |
|---|---|
NoTestDataSpecError
|
If the diagnostic has no test_data_spec |
TestCaseNotFoundError
|
If the test case doesn't exist |
DatasetResolutionError
|
If datasets cannot be resolved |
Source code in packages/climate-ref/src/climate_ref/testing.py
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 | |
assert_test_case_no_drift(config, diagnostic, test_case_name, paths, work_dir)
#
Execute a diagnostic test case and assert its committed bundle has not drifted.
Runs the diagnostic against the fetched catalog using the same execute/build stages as
ref test-cases run (see :mod:climate_ref.cli.test_cases._stages),
rebuilds the committed bundle from the fresh execution,
and compares it to the tracked regression/ baseline within tolerance.
Unlike ref test-cases replay this re-executes the diagnostic rather than replaying,
so it also proves the diagnostic still runs and emits a valid bundle.
Ingesting a committed bundle into the database is a separate concern,
covered by the executor result-handling tests
(packages/climate-ref/tests/unit/executor/test_result_handling.py and friends).
It is intentionally not re-checked here.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
Config
|
The active configuration (provides the software root and results dir). |
required |
diagnostic
|
Diagnostic
|
The diagnostic to run. |
required |
test_case_name
|
str
|
Name of the test case to run. |
required |
paths
|
TestCasePaths
|
Resolved paths for the test case (catalog + tracked regression baseline). |
required |
work_dir
|
Path
|
A per-test scratch directory for the output slot and execution outputs. |
required |
Raises:
| Type | Description |
|---|---|
NoTestDataSpecError
|
If the diagnostic has no test_data_spec. |
TestCaseNotFoundError
|
If the test case doesn't exist. |
AssertionError
|
If the rebuilt committed bundle drifts from the tracked baseline. |
Source code in packages/climate-ref/src/climate_ref/testing.py
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 | |
create_no_drift_test(provider)
#
Build the standard per-provider integration test for committed-bundle drift.
Returns a pytest function parameterized with one case per diagnostic test case
(via :func:climate_ref_core.testing.collect_test_case_params),
marked slow and test_cases.
Each case configures the provider,
resolves the package-local test-case paths,
skips when the fetched catalog or committed baseline is missing,
and delegates to :func:assert_test_case_no_drift.
Requires ref test-cases fetch --provider <slug> to have been run first.
Set REF_TEST_CASES_STRICT=true in CI to fail on missing catalogs or baselines
instead of silently skipping them. Explicit diagnostic exclusions still apply.
Usage in a provider's tests/integration/test_diagnostics.py::
from climate_ref_example import provider
from climate_ref.testing import create_no_drift_test
test_run_test_cases = create_no_drift_test(provider)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
provider
|
DiagnosticProvider
|
The diagnostic provider whose test cases should be exercised. |
required |
Source code in packages/climate-ref/src/climate_ref/testing.py
fetch_sample_data(force_cleanup=False, symlink=False)
#
Fetch the sample data for the given version.
The sample data is produced in the Climate-REF/ref-sample-data repository. This repository contains decimated versions of key datasets used by the diagnostics packages. Decimating these data greatly reduces the data volumes needed to run the test-suite.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
force_cleanup
|
bool
|
If True, remove any existing files |
False
|
symlink
|
bool
|
If True, symlink in the data otherwise copy the files The symlink approach is faster, but will fail when running with a non-local executor because the symlinks can't be followed. |
False
|
Source code in packages/climate-ref/src/climate_ref/testing.py
log_resources(case_id)
#
Measure a block of work and log what it cost once it finishes.
The measurement is logged whether or not the block raises.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
case_id
|
str
|
Identifier of the case the measurement belongs to. |
required |
Yields:
| Type | Description |
|---|---|
Iterator[None]
|
Nothing. The block is the span being measured. |