Test Structure¶
This directory contains all tests for dummyxarray, organized by test type and module.
Directory Structure¶
tests/
├── conftest.py # Shared fixtures and pytest configuration
├── unit/ # Unit tests (per module)
│ ├── test_core.py # DummyArray, DummyDataset core functionality
│ ├── test_history.py # HistoryMixin tests
│ ├── test_provenance.py # ProvenanceMixin tests
│ ├── test_cf_compliance.py # CFComplianceMixin tests
│ ├── test_io.py # IOMixin tests
│ ├── test_validation.py # ValidationMixin tests
│ ├── test_data_generation.py # DataGenerationMixin tests
│ ├── test_mfdataset.py # Multi-file dataset support tests
│ └── test_ncdump_parser.py # NetCDF metadata parser tests
├── integration/ # Integration tests (workflows)
│ └── test_workflows.py # End-to-end workflow tests
└── fixtures/ # Test data and fixtures
└── __init__.py
Test Categories¶
Unit Tests (tests/unit/)¶
Test individual modules and mixins in isolation:
- test_core.py: Core classes (
DummyArray,DummyDataset) - Initialization, repr, basic operations
- Dimension management
- Coordinate and variable management
-
Renaming operations
-
test_history.py: History tracking functionality
- Operation recording
- History export/replay
- History visualization (text, dot, mermaid)
-
Reset history
-
test_provenance.py: Provenance tracking
- Change tracking (added/removed/modified)
- Provenance visualization
-
Rename tracking
-
test_cf_compliance.py: CF convention support
- Axis inference (X/Y/Z/T)
- Axis attribute setting
- CF validation
-
Standard name handling
-
test_io.py: I/O operations
- Dictionary/JSON/YAML export
- File save/load
- xarray conversion (to/from)
-
Zarr export
-
test_validation.py: Dataset validation
- Structure validation
- Dimension inference
-
Conflict detection
-
test_data_generation.py: Random data generation
- Coordinate data generation
- Variable data generation
-
Metadata-based generation
-
test_mfdataset.py: Multi-file dataset support
- Opening multiple NetCDF files
- Frequency inference
- Time-based grouping
-
File tracking and queries
-
test_ncdump_parser.py: NetCDF metadata parser
- Parsing ncdump output
- Dimension extraction
- Variable and attribute parsing
Integration Tests (tests/integration/)¶
Test complete workflows and interactions between modules:
- test_workflows.py: End-to-end workflows
- CF compliance workflow
- History and provenance workflow
- Data generation and export workflow
- Rename workflow
- Validation and fix workflow
- Import/modify/export workflow
Running Tests¶
Run all tests¶
Run specific test file¶
Run specific test class¶
Run specific test¶
Run only unit tests¶
Run only integration tests¶
Run with markers¶
# Run only unit tests (if marked)
pytest -m unit
# Run only integration tests (if marked)
pytest -m integration
# Skip slow tests
pytest -m "not slow"
Run with coverage¶
Shared Fixtures (conftest.py)¶
Common fixtures available to all tests:
Dataset Fixtures¶
empty_dataset: Empty DummyDatasetsimple_dataset: Dataset with dimensions onlydataset_with_coords: Dataset with coordinatesdataset_with_data: Dataset with data arrayscf_compliant_dataset: CF-compliant dataset
Array Fixtures¶
simple_array: Simple DummyArrayarray_with_data: DummyArray with data
Parametrized Fixtures¶
with_history: Test with/without history trackingviz_format: Test all visualization formats
Temporary File Fixtures¶
temp_yaml_file: Temporary YAML file pathtemp_zarr_store: Temporary Zarr store path
Mock Data Fixtures¶
sample_history: Sample operation historysample_provenance: Sample provenance data
Writing New Tests¶
Unit Test Template¶
"""Tests for [module] functionality."""
import pytest
from dummyxarray import DummyDataset
class TestFeature:
"""Test [feature] functionality."""
def test_basic_case(self, simple_dataset):
"""Test basic [feature] usage."""
# Arrange
ds = simple_dataset
# Act
result = ds.some_method()
# Assert
assert result is not None
Integration Test Template¶
"""Integration tests for [workflow]."""
import pytest
from dummyxarray import DummyDataset
class TestWorkflow:
"""Test [workflow] end-to-end."""
def test_complete_workflow(self, tmp_path):
"""Test complete [workflow] from start to finish."""
# Create dataset
ds = DummyDataset()
# Perform workflow steps
ds.add_dim("time", 10)
# ... more steps
# Verify final state
assert ds.dims["time"] == 10
Best Practices¶
- Use fixtures: Leverage shared fixtures from
conftest.py - Test one thing: Each test should verify one specific behavior
- Descriptive names: Test names should describe what they test
- Arrange-Act-Assert: Follow AAA pattern for clarity
- Parametrize: Use
@pytest.mark.parametrizefor similar tests - Mock external dependencies: Don't rely on external services
- Clean up: Use fixtures with cleanup or
tmp_pathfor files
Test Coverage¶
Current coverage: 188 tests across all modules
- Unit tests: ~160 tests
- Integration tests: ~28 tests
Target: >90% code coverage