-
Notifications
You must be signed in to change notification settings - Fork 0
Testing
This page describes the purpose and organization of the Test folder for the entropic_measurement library, along with comprehensive testing guidelines and best practices.
The tests/ directory contains comprehensive test suites for the entropic_measurement library. It serves three primary purposes:
- Quality Assurance: Ensures all library functions work correctly and handle edge cases appropriately
- Regression Prevention: Validates that new changes don't break existing functionality
- Documentation: Provides practical examples of how library functions should be used
The test directory is organized as follows:
tests/
├── test_core.py # Tests for core entropy calculation functions
├── test_utils.py # Tests for utility functions and helpers
├── test_integration.py # End-to-end workflow tests
├── test_performance.py # Performance benchmarks and stress tests
├── fixtures/ # Test data files and fixtures
│ ├── sample_data.json
│ └── test_datasets.csv
└── conftest.py # Pytest configuration and shared fixtures
The library uses pytest as the primary testing framework. To run all tests:
# Install test dependencies
pip install pytest pytest-cov
# Run all tests
pytest tests/
# Run tests with coverage report
pytest --cov=entropic_measurement tests/
# Run specific test file
pytest tests/test_core.py
# Run tests matching a pattern
pytest -k "shannon" tests/# Run tests in parallel (requires pytest-xdist)
pytest -n auto tests/
# Generate HTML coverage report
pytest --cov=entropic_measurement --cov-report=html tests/
# Run performance tests only
pytest tests/test_performance.py -v- Purpose: Test individual functions in isolation
-
Location:
test_core.py,test_utils.py - Focus: Input validation, edge cases, mathematical correctness
- Purpose: Verify end-to-end workflows and component interactions
-
Location:
test_integration.py - Focus: Data loading → processing → output workflows
- Purpose: Benchmark execution time and memory usage
-
Location:
test_performance.py - Focus: Large datasets, scalability, resource efficiency
from entropic_measurement import shannon_entropy
import pytest
def test_shannon_entropy_binary():
"""Test Shannon entropy for binary distribution."""
data = [0.5, 0.5]
result = shannon_entropy(data)
assert abs(result - 1.0) < 1e-10
def test_shannon_entropy_edge_cases():
"""Test edge cases and error conditions."""
# Test empty input
with pytest.raises(ValueError):
shannon_entropy([])
# Test negative probabilities
with pytest.raises(ValueError):
shannon_entropy([-0.1, 0.6, 0.5])def test_full_entropy_analysis_workflow():
"""Test complete analysis from data loading to results."""
# Load test data
data = load_test_dataset('sample_data.json')
# Process through full pipeline
config = {
'method': 'shannon',
'normalize': True
}
result = analyze_entropy(data, config)
# Validate results structure and values
assert 'entropy' in result
assert 'metadata' in result
assert result['entropy'] > 0-
Test Naming: Use descriptive names that explain what is being tested
# Good def test_shannon_entropy_handles_zero_probabilities(): # Avoid def test_shannon():
-
Test Organization: Group related tests in classes
class TestShannonEntropy: def test_binary_distribution(self): def test_uniform_distribution(self): def test_edge_cases(self):
-
Use Fixtures: Create reusable test data
@pytest.fixture def sample_distribution(): return [0.3, 0.4, 0.2, 0.1]
- Minimum Coverage: Aim for 90% code coverage
- Critical Paths: 100% coverage for core entropy calculations
- Error Handling: Test all exception paths and edge cases
- Use small, predictable datasets for unit tests
- Include real-world data samples for integration tests
- Test with various data types and distributions
- Validate both typical and edge cases
When contributing new functionality:
- Create corresponding tests in the appropriate test file
- Follow existing patterns for test structure and naming
- Include edge cases and error conditions
- Add performance tests for computationally intensive functions
- Update fixtures if new test data is needed
- Tests cover the happy path (normal usage)
- Tests cover edge cases (empty inputs, extreme values)
- Tests verify error handling (invalid inputs)
- Tests include performance considerations for large data
- Test names are descriptive and follow conventions
- Documentation strings explain test purpose
- New fixtures are properly configured
Tests are automatically run on:
- Pull Requests: All tests must pass before merging
- Main Branch: Continuous monitoring for regressions
- Multiple Python Versions: Compatibility testing across supported versions
Before submitting contributions:
# Run full test suite
pytest tests/ -v
# Check coverage
pytest --cov=entropic_measurement tests/ --cov-report=term-missing
# Run performance benchmarks
pytest tests/test_performance.py --benchmark-only- Check that all dependencies are installed:
pip install -e .[test] - Verify Python version compatibility
- Review test data file paths and permissions
- Performance tests may vary based on system resources
- Use relative comparisons rather than absolute timing
- Consider system load when interpreting results
- Contributing Guidelines - General contribution workflow
- API Reference - Detailed function documentation
- Examples - Practical usage examples
- Architecture - System design and structure
Note: This testing framework ensures the entropic_measurement library maintains high quality and reliability. All contributors are encouraged to write comprehensive tests for their code contributions.