This document describes the testing infrastructure for asimov-gwdata, including how to run tests both with and without network access.
The asimov-gwdata testing framework has been improved to support offline testing without requiring access to external resources like:
- GWOSC (Gravitational Wave Open Science Center) servers
- gwdatafind servers
- Zenodo for test data downloads
-
test_fixtures.py - Provides mock fixtures for testing without network access
MockGWDataFind- Mock gwdatafind serverMockGWOSC- Mock GWOSC data access- Utility functions for test data management
-
test_frames.py - Tests for frame file access
- Uses mocks to test frame lookup without network
- Legacy tests kept for integration testing (skipped by default)
-
test_calibration.py - Tests for calibration data handling
- Uses mocks for file system operations
-
test_pesummary.py - Tests for PESummary metafile handling
- Gracefully handles missing test data files
-
test_with_mocks.py - Examples demonstrating mock usage
Run all tests:
python3 -m unittest discover -s tests -p "test_*.py" -vRun a specific test module:
python3 -m unittest tests.test_frames -vRun a specific test class:
python3 -m unittest tests.test_frames.TestLIGOFramesWithMocks -vRun a specific test:
python3 -m unittest tests.test_frames.TestLIGOFramesWithMocks.test_lookup_gw150914_with_mock -vAll tests using mocks will work without network access:
# These tests will pass without network
python3 -m unittest tests.test_with_mocks -v
python3 -m unittest tests.test_frames.TestLIGOFramesWithMocks -vSome tests require network access or authentication:
# Requires IGWN authentication
python3 -m unittest tests.test_calibration.TestFrameCalibration -v
# Requires network to download test data
python3 -m unittest tests.test_pesummary -vfrom tests.test_fixtures import MockGWDataFind
from unittest.mock import patch
def mock_find_urls(site, frametype, gpsstart, gpsend, **kwargs):
"""Custom mock implementation."""
return ["file:///path/to/frame.gwf"]
# Use in a test
with patch('datafind.frames.find_urls', side_effect=mock_find_urls):
# Your test code here
urls, files = get_data_frames_private(...)from tests.test_fixtures import MockGWOSC
mock_gwosc = MockGWOSC({
'H1': ['https://gwosc.org/data/H1-frame.gwf'],
'L1': ['https://gwosc.org/data/L1-frame.gwf']
})
with mock_gwosc.patch_get_urls():
# Your test code here
urls = get_data_frames_gwosc(...)from tests.test_fixtures import temporary_test_directory
with temporary_test_directory() as tmpdir:
# Create test files
test_file = os.path.join(tmpdir, 'test.txt')
with open(test_file, 'w') as f:
f.write('test data')
# Run tests
# ...
# Directory is automatically cleaned upThe test suite is designed to work in CI/CD environments without network access:
# .gitlab-ci.yml or similar
test:
script:
- pip install -e .
- python3 -m unittest discover -s tests -p "test_*.py" -vAll tests will either:
- Pass using mocks (for network-dependent functionality)
- Skip gracefully (if optional dependencies are missing)
If network access is available, additional integration tests will run:
test-with-network:
script:
- pip install -e .
# Download test data first
- wget -O tests/GW150914.hdf5 "https://zenodo.org/records/6513631/files/IGWN-GWTC2p1-v2-GW150914_095045_PEDataRelease_mixed_cosmo.h5?download=1"
- python3 -m unittest discover -s tests -p "test_*.py" -vtests/test_data/V1.gwf- A sample Virgo frame file
tests/GW150914.hdf5- PESummary metafile for GW150914 (~12MB)- Downloaded automatically if network is available
- Tests skip gracefully if not present
For large test files, consider:
- Git LFS - Store large files using Git Large File Storage
- Pre-download - Download files in CI/CD before running tests
- Artifacts - Cache downloaded files as CI/CD artifacts
To add a new mock for testing:
- Add the mock class to
tests/test_fixtures.py:
class MockNewService:
"""Mock for a new external service."""
def __init__(self, mock_data):
self.mock_data = mock_data
def mock_method(self, *args, **kwargs):
"""Mock implementation."""
return self.mock_data
@contextmanager
def patch_method(self):
"""Context manager for patching."""
with patch('module.method', side_effect=self.mock_method):
yield self-
Create tests demonstrating its usage in
tests/test_with_mocks.py -
Update this documentation
To add new test data files:
- Place small files (<1MB) in
tests/test_data/ - For large files:
- Use Git LFS, or
- Download in
setUpModule()with graceful failure, or - Provide download script
- Prefer Mocks - Use mocks for external services when possible
- Graceful Degradation - Skip tests if dependencies are unavailable
- Clear Messages - Provide helpful skip messages explaining what's needed
- Document Requirements - Clearly document what each test needs
- Minimal Test Data - Keep test data files as small as possible
Some tests may skip if optional dependencies are missing:
skipped "Cannot access frame file: No module named 'LDAStools'"
This is expected. Install the missing dependency if you need to run that test:
pip install LDAStoolsSome tests require IGWN authentication:
skipped 'No scitoken was found'
Set up authentication following the IGWN guidelines if you need to run these tests.
If tests fail with network errors in an offline environment, ensure you're running the mock-based tests:
python3 -m unittest tests.test_frames.TestLIGOFramesWithMocks -vInstead of the legacy integration tests:
# Don't run this offline - it's marked as skipped anyway
python3 -m unittest tests.test_frames.TestLIGOFrames -v