mirror of
https://github.com/aserper/masto-rss.git
synced 2025-12-15 04:25:24 +00:00
6.0 KiB
6.0 KiB
Test Implementation Summary
Overview
Comprehensive test suite designed for the Masto-RSS bot with unit tests, integration tests, and automated CI/CD testing via GitHub Actions.
Files Created
1. Core Refactoring
-
bot.py - Refactored core functionality into testable
MastodonRSSBotclass- Separated concerns (parsing, posting, state management)
- Dependency injection for easier testing
- Error handling and logging
- Type hints for better code clarity
-
main.py - Simplified entry point
- Loads configuration from environment
- Instantiates and runs the bot
- Clean separation from core logic
2. Test Files
test_bot.py - Unit Tests
Contains 20+ unit tests covering:
- Bot initialization with configuration
- Loading/saving processed entries (with and without existing files)
- Directory creation for state files
- Status formatting from feed entries
- Mastodon posting (success and failure cases)
- Feed parsing (success, errors, malformed data)
- Processing new entries (all new, partially processed, no entries)
- Handling entries without URLs
- Failed posts don't get marked as processed
- Main entry point configuration loading
Coverage: Core business logic, edge cases, error handling
test_integration.py - Integration Tests
Contains 10+ integration tests covering:
- End-to-end RSS to Mastodon flow
- Real RSS 2.0 feed parsing
- Real Atom feed parsing
- State persistence across bot runs
- Incremental feed updates (new entries added over time)
- Network error handling (500 errors, timeouts)
- Malformed XML handling
- Different visibility levels (public, unlisted, private, direct)
- Rate limiting error handling
- Retry mechanisms
Coverage: Component integration, external service mocking, data flow
3. Configuration Files
pytest.ini
- Test discovery patterns
- Coverage settings (80% minimum)
- Output formatting
- Test markers (unit, integration, slow)
- Coverage exclusions
pyproject.toml
Test dependencies (managed by uv):
pytest- Testing frameworkpytest-cov- Coverage pluginpytest-mock- Mocking utilitiesresponses- HTTP mocking for integration testsflake8- Lintingblack- Code formattingmypy- Type checkingcoverage- Coverage reporting
.gitignore
Updated to exclude:
- Test artifacts (
.pytest_cache/,htmlcov/,coverage.xml) - Python cache files
- Virtual environments
- IDE configurations
4. CI/CD Pipeline
.github/workflows/test.yml
Comprehensive GitHub Actions workflow with 5 jobs:
-
Unit Tests
- Runs on Python 3.10, 3.11, 3.12 (matrix)
- Executes unit tests with coverage
- Uploads coverage to Codecov
-
Integration Tests
- Runs on Python 3.10, 3.11, 3.12 (matrix)
- Executes integration tests
- Uploads coverage to Codecov
-
Code Quality
- Ruff linting and formatting verification
- Mypy type checking
-
Docker Build Test
- Builds Docker image
- Verifies Python installation
- Checks dependencies are installed
-
All Tests Pass
- Requires all previous jobs to succeed
- Provides single status check for PRs
5. Documentation
TESTING.md
Comprehensive testing guide covering:
- Test architecture explanation
- Running tests locally (all, specific, by marker)
- Coverage report generation
- GitHub Actions CI/CD workflow details
- Test coverage requirements (80% minimum)
- Code quality standards
- Writing new tests (templates provided)
- Troubleshooting common issues
- Best practices
README.md
Updated with:
- Test status badge
- Link to testing documentation
Test Statistics
Coverage Targets
- Minimum: 80% code coverage
- Measured:
bot.pyandmain.py - Excluded: Test files, virtual environments
Test Count
- Unit Tests: 20+ tests
- Integration Tests: 10+ tests
- Total: 30+ tests
Python Versions Tested
- Python 3.10
- Python 3.11
- Python 3.12
Key Testing Features
Mocking Strategy
- Mastodon API: Mocked using
unittest.mockto avoid real API calls - RSS Feeds: Mocked using
responseslibrary with realistic XML - File System: Uses temporary files that are cleaned up automatically
Test Data
- Realistic RSS 2.0 and Atom feed examples
- Multiple entry scenarios (new, processed, malformed)
- Error conditions (network failures, API errors, rate limits)
Continuous Integration
- Runs on every push to
main - Runs on all pull requests
- Parallel test execution across Python versions
- Automatic coverage reporting
- Docker image validation
Running Tests
Locally
# Install dependencies
pip install -r requirements-test.txt
# Run all tests
pytest
# Run with coverage
pytest --cov=bot --cov=main --cov-report=html
# Run specific tests
pytest test_bot.py # Unit tests only
pytest test_integration.py # Integration tests only
In CI/CD
Tests run automatically via GitHub Actions:
- Trigger: Push to main or pull request
- Duration: ~5-10 minutes
- Matrix: 3 Python versions × 2 test types = 6 parallel jobs
- Plus: Code quality and Docker build validation
Benefits
- Code Quality: Ensures all changes are tested before merging
- Regression Prevention: Catches bugs before they reach production
- Documentation: Tests serve as executable documentation
- Confidence: Safe to refactor with comprehensive test coverage
- Type Safety: Mypy catches type-related bugs early
- Code Style: Black and flake8 enforce consistency
Future Enhancements
Potential additions:
- Performance/load testing
- End-to-end tests with real Mastodon test instance
- Security scanning (Bandit, Safety)
- Mutation testing (mutmut)
- Property-based testing (Hypothesis)
- Contract testing for API interactions
Maintenance
- Add tests for all new features
- Update tests when changing behavior
- Keep coverage above 80%
- Run tests before committing
- Review test failures in CI before merging