This guide explains how to run and maintain tests for the Metadata-to-Morphsource-compare project.
# Install test dependencies
pip install -r requirements-test.txt
# Run all tests
pytest tests/
# Run tests with coverage
pytest tests/ --cov=. --cov-report=term
Install test dependencies:
pip install -r requirements-test.txt
This installs:
Run all tests:
pytest tests/
Run with verbose output:
pytest tests/ -v
Run specific test file:
pytest tests/test_compare.py
Run specific test class:
pytest tests/test_compare.py::TestMorphosourceMatcher
Run specific test method:
pytest tests/test_compare.py::TestMorphosourceMatcher::test_initialization
Run tests matching a pattern:
pytest tests/ -k "catalog"
Tests automatically run on:
main
or develop
branchesmain
or develop
branchesThe CI workflow (.github/workflows/tests.yml
) runs tests on:
View test results in the Actions tab of the repository.
tests/
├── __init__.py # Test package initialization
├── test_compare.py # Tests for compare.py
├── test_verify_pixel_spacing.py # Tests for verify_pixel_spacing.py
└── test_run_comparison.py # Tests for run_comparison.py
test_compare.py
MorphosourceMatcher
classtest_verify_pixel_spacing.py
MorphosourceVoxelVerifier
classtest_run_comparison.py
Terminal output:
pytest tests/ --cov=. --cov-report=term
HTML report:
pytest tests/ --cov=. --cov-report=html
Then open htmlcov/index.html
in your browser.
XML report (for CI tools):
pytest tests/ --cov=. --cov-report=xml
The test suite currently covers:
test_*.py
Test*
test_*
import unittest
from unittest.mock import Mock, patch
class TestMyFeature(unittest.TestCase):
def setUp(self):
"""Set up test fixtures"""
# Initialize test data
pass
def tearDown(self):
"""Clean up after tests"""
# Remove temporary files
pass
def test_basic_functionality(self):
"""Test basic feature behavior"""
# Arrange
input_data = "test"
# Act
result = my_function(input_data)
# Assert
self.assertEqual(result, expected)
Use unittest.mock
for external dependencies:
from unittest.mock import Mock, patch
@patch('module.requests.get')
def test_api_call(self, mock_get):
"""Test API interaction"""
# Setup mock
mock_response = Mock()
mock_response.status_code = 200
mock_response.json.return_value = {"key": "value"}
mock_get.return_value = mock_response
# Test code that calls requests.get
result = my_api_function()
# Verify
self.assertIsNotNone(result)
mock_get.assert_called_once()
The pytest.ini
file contains pytest configuration:
Lists all testing dependencies and their versions.
Issue: Import errors when running tests
ModuleNotFoundError: No module named 'pandas'
Solution: Install test dependencies:
pip install -r requirements-test.txt
Issue: Tests fail with file not found errors
Solution: Make sure you’re running tests from the repository root:
cd /path/to/Metadata-to-Morphsource-compare
pytest tests/
Issue: Coverage report not generated
Solution: Install pytest-cov:
pip install pytest-cov
Issue: Tests pass locally but fail in CI
Possible causes:
Check the CI logs in the Actions tab for details.
Run tests with more detailed output:
# Show print statements
pytest tests/ -s
# Show detailed traceback
pytest tests/ --tb=long
# Stop at first failure
pytest tests/ -x
# Enter debugger on failure
pytest tests/ --pdb
When adding new functionality:
If you encounter issues with tests: