Add comprehensive test suite with GitHub Actions CI/CD

- Refactor code into testable bot.py module with MastodonRSSBot class
- Create 20+ unit tests covering core functionality and edge cases
- Create 10+ integration tests for RSS parsing and Mastodon posting
- Add GitHub Actions workflow for automated testing
  - Unit tests on Python 3.10, 3.11, 3.12
  - Integration tests with mocked external services
  - Code quality checks (flake8, black, mypy)
  - Docker build validation
- Configure pytest with 80% minimum coverage requirement
- Add test dependencies in requirements-test.txt
- Update .gitignore to exclude test artifacts
- Add comprehensive TESTING.md documentation
- Add test status badge to README
- Maintain full backward compatibility with existing setup
This commit is contained in:
aserper
2025-12-12 23:30:38 -05:00
parent 88df609811
commit f2056b90f2
11 changed files with 1624 additions and 78 deletions

214
TEST_SUMMARY.md Normal file
View File

@@ -0,0 +1,214 @@
# 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](bot.py)** - Refactored core functionality into testable `MastodonRSSBot` class
- Separated concerns (parsing, posting, state management)
- Dependency injection for easier testing
- Error handling and logging
- Type hints for better code clarity
- **[main.py](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](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](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](pytest.ini)
- Test discovery patterns
- Coverage settings (80% minimum)
- Output formatting
- Test markers (unit, integration, slow)
- Coverage exclusions
#### [requirements-test.txt](requirements-test.txt)
Test dependencies:
- `pytest` - Testing framework
- `pytest-cov` - Coverage plugin
- `pytest-mock` - Mocking utilities
- `responses` - HTTP mocking for integration tests
- `flake8` - Linting
- `black` - Code formatting
- `mypy` - Type checking
- `coverage` - Coverage reporting
#### [.gitignore](.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](.github/workflows/test.yml)
Comprehensive GitHub Actions workflow with **5 jobs**:
1. **Unit Tests**
- Runs on Python 3.10, 3.11, 3.12 (matrix)
- Executes unit tests with coverage
- Uploads coverage to Codecov
2. **Integration Tests**
- Runs on Python 3.10, 3.11, 3.12 (matrix)
- Executes integration tests
- Uploads coverage to Codecov
3. **Code Quality**
- Flake8 linting (error detection + complexity)
- Black formatting verification
- Mypy type checking
4. **Docker Build Test**
- Builds Docker image
- Verifies Python installation
- Checks dependencies are installed
5. **All Tests Pass**
- Requires all previous jobs to succeed
- Provides single status check for PRs
### 5. Documentation
#### [TESTING.md](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](README.md)
Updated with:
- Test status badge
- Link to testing documentation
## Test Statistics
### Coverage Targets
- **Minimum:** 80% code coverage
- **Measured:** `bot.py` and `main.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.mock` to avoid real API calls
- **RSS Feeds:** Mocked using `responses` library 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
```bash
# 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
1. **Code Quality:** Ensures all changes are tested before merging
2. **Regression Prevention:** Catches bugs before they reach production
3. **Documentation:** Tests serve as executable documentation
4. **Confidence:** Safe to refactor with comprehensive test coverage
5. **Type Safety:** Mypy catches type-related bugs early
6. **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