This PR significantly expands the test coverage for the Red-DiscordBot cogs repository, moving beyond basic import tests to comprehensive unit testing of cog functionality.
-
Pytest Configuration
pytest.ini: Test discovery, async support, coverage settings, and test markers.coveragerc: Coverage reporting configuration with exclusionsrequirements-test.txt: Test dependencies (pytest, pytest-asyncio, pytest-cov, pytest-mock)
-
Mock Utilities (
tests/conftest.py)- Mock Discord Objects: Bot, Guild, Member, Role, TextChannel, Message, Context, Interaction
- Mock Red-DiscordBot Config: Complete implementation of the Config API for testing
- Pytest Fixtures: Auto-available fixtures for common test objects
-
CI Integration
- Added pytest job to
.github/workflows/lint.yml - Runs tests on every push
- Includes coverage reporting
- Added pytest job to
-
Documentation
- Comprehensive
tests/README.mdwith examples and best practices - Instructions for running tests, writing new tests, and troubleshooting
- Comprehensive
- IMDB Parsing: Link validation and ID extraction
- Configuration: Channel management, emoji settings
- Movie Management: Add, remove, watch/unwatch movies
- Vote Counting: Score calculation from reactions
- Leaderboard: Sorting, filtering, limiting results
- Edge Cases: Empty lists, all watched, non-existent movies
- Integration: Complete movie lifecycle workflows
- Configuration: Setup and persistence
- Type Consistency: Tests for bug #119 (int vs string ID handling)
- Role Assignment: Lookup and assignment logic
- Multiple Reactions: Managing multiple reactions per message
- Edge Cases: Missing messages, duplicate setups
- Integration: Complete reaction-role workflows
- Configuration: Scale, rotation, position offsets, flip settings
- Image Dimensions: Dimension and position calculations
- Validation: Scale and rotation limits, clamping
- Image Processing: Flipping, aspect ratio, byte conversion
- Edge Cases: Zero dimensions, negative values, extreme rotations
- Configuration Persistence: Save/load settings
- Integration: Complete hat configuration workflows
- Total Tests: 76
- All Passing: ✅ 100%
- Test Files: 3 (
test_movie_vote.py,test_react_roles.py,test_hat.py) - Lines of Test Code: ~1,900 lines
- Mock Utilities: ~650 lines
All tests run without requiring:
- A running Discord bot
- Red-DiscordBot installation (for unit tests)
- Actual Discord API calls
- Database or file system persistence
This is achieved through comprehensive mocking of:
- Discord.py objects
- Red-DiscordBot Config system
- Bot and guild state
Tests are organized into logical classes:
- Configuration Tests: Settings and defaults
- Logic Tests: Core functionality and algorithms
- Edge Case Tests: Boundary conditions and errors
- Integration Tests: Complete workflows
All async tests are properly handled with:
@pytest.mark.asynciodecoratorpytest-asyncioplugin- Async fixtures and utilities
- Catch Bugs Early: Tests validate logic before deployment
- Prevent Regressions: Ensure fixes don't break existing functionality
- Document Behavior: Tests serve as examples of correct usage
- Refactoring Confidence: Change code without fear
- CI Validation: Automatic verification on every commit
While the tests provide excellent coverage of business logic, they focus on:
- ✅ Configuration management
- ✅ Data validation
- ✅ Logic and algorithms
- ✅ Edge case handling
- ✅ Integration workflows
Not covered (by design):
- ❌ Discord API interactions (would require integration tests)
- ❌ Actual image file manipulation (unit tests validate logic only)
- ❌ Database persistence (mocked for unit testing)
-
Party Cog Tests
- Party creation and deletion
- User signup/leave logic
- Role limit validation
- Concurrent signup handling
-
Secret Santa Cog Tests
- Participant matching algorithm
- Event management
- Anonymous messaging
- Gift tracking
-
Additional Cogs
- Empty Voices: Channel creation and cleanup
- Quote DB: Quote storage and retrieval
- Access: Permission management
-
Integration Tests (if feasible)
- Tests with actual Discord.py objects
- Message and reaction handling
- Permission checking
-
Performance Tests
- Concurrent operations
- Large datasets
- Rate limiting
-
Mutation Testing
- Validate test quality
- Ensure tests actually catch bugs
# Install dependencies
pip install -r requirements-test.txt
# Run all tests
pytest tests/ -v
# Run with coverage
pytest tests/ -v --cov=. --cov-report=html
# Run specific test file
pytest tests/test_movie_vote.py -v
# Run specific test
pytest tests/test_movie_vote.py::TestIMDBParsing::test_imdb_link_regex_valid_url -vTests run automatically on every push via GitHub Actions:
- Lint job: Runs flake8
- Test job: Validates Red-DiscordBot imports
- Pytest job: Runs full pytest suite with coverage
- Only basic import tests
- No validation of business logic
- Bugs discovered in production
- Fear of breaking changes
- Comprehensive unit tests
- Logic validation before deployment
- Bugs caught in CI
- Confidence in refactoring
pytest.ini- Pytest configuration.coveragerc- Coverage configurationrequirements-test.txt- Test dependenciestests/__init__.py- Test package markertests/conftest.py- Mock utilities and fixtures (650 lines)tests/test_movie_vote.py- Movie Vote tests (27 tests)tests/test_react_roles.py- React Roles tests (18 tests)tests/test_hat.py- Hat tests (31 tests)tests/README.md- Comprehensive testing documentation
.github/workflows/lint.yml- Added pytest job with coverage
- Addresses issue #119: Type inconsistency in React Roles (tests validate the bug)
- Supports issue #118: Async patterns (validates async logic)
- Supports issue #120: Null pointer checks (edge case tests)
This PR establishes a solid foundation for testing Red-DiscordBot cogs. The infrastructure is in place for easily adding more tests, and the existing tests provide excellent coverage of critical functionality.
The testing approach balances pragmatism (mocking complex dependencies) with thoroughness (comprehensive test cases), making it easy to validate cog behavior without requiring a full Discord bot setup.
Test Summary: 76 tests, 100% passing ✅