|
| 1 | +# PyWorkout Test Suite |
| 2 | + |
| 3 | +This directory contains the comprehensive test suite for PyWorkout. The tests are written using pytest and provide coverage for the main CLI functionality and GUI components. |
| 4 | + |
| 5 | +## Test Structure |
| 6 | + |
| 7 | +``` |
| 8 | +tests/ |
| 9 | +├── __init__.py # Package initialization |
| 10 | +├── test_main.py # Tests for main.py (CLI functionality) |
| 11 | +└── test_gui.py # Tests for gui.py (GUI components) |
| 12 | +``` |
| 13 | + |
| 14 | +## Test Coverage |
| 15 | + |
| 16 | +The test suite includes the following test categories: |
| 17 | + |
| 18 | +### Main Module Tests (`test_main.py`) |
| 19 | + |
| 20 | +1. **TestWorkoutData** - Tests workout data structures |
| 21 | + - Validates workout groups are defined correctly |
| 22 | + |
| 23 | +2. **TestMuscleGroupSelection** - Tests muscle group selection |
| 24 | + - Selection by number (1-7) |
| 25 | + - Selection by name (abs, quads, glutes, etc.) |
| 26 | + - Invalid selection handling |
| 27 | + - Quit during selection |
| 28 | + |
| 29 | +3. **TestWorkoutCommands** - Tests CLI commands |
| 30 | + - `list` - List exercises |
| 31 | + - `start` - Start workout |
| 32 | + - `help` - Display help |
| 33 | + - `license` - Display license |
| 34 | + - `quit` - Exit program |
| 35 | + - Invalid command handling |
| 36 | + |
| 37 | +4. **TestWorkoutFlow** - Tests workout flow |
| 38 | + - Start → Next → End flow |
| 39 | + - Skip functionality |
| 40 | + - Stats command |
| 41 | + |
| 42 | +5. **TestVideoFunctionality** - Tests video playback |
| 43 | + - Video command on different platforms |
| 44 | + |
| 45 | +6. **TestWelcomeScreen** - Tests welcome screen |
| 46 | + - Welcome message display |
| 47 | + - Day recommendation |
| 48 | + |
| 49 | +7. **TestIntegration** - Integration tests |
| 50 | + - Complete workout scenarios |
| 51 | + - Multiple muscle groups |
| 52 | + |
| 53 | +### GUI Module Tests (`test_gui.py`) |
| 54 | + |
| 55 | +1. **TestGUIImports** - Tests GUI imports |
| 56 | +2. **TestPercentageFunction** - Tests percentage calculations |
| 57 | +3. **TestGUIComponents** - Tests GUI components |
| 58 | + |
| 59 | +## Running Tests |
| 60 | + |
| 61 | +### Prerequisites |
| 62 | + |
| 63 | +Install test dependencies: |
| 64 | + |
| 65 | +```bash |
| 66 | +pip install -r requirements.txt |
| 67 | +``` |
| 68 | + |
| 69 | +This installs: |
| 70 | +- pytest |
| 71 | +- pytest-cov (coverage reporting) |
| 72 | +- pytest-mock (mocking support) |
| 73 | + |
| 74 | +### Running All Tests |
| 75 | + |
| 76 | +Run all tests with coverage: |
| 77 | + |
| 78 | +```bash |
| 79 | +pytest tests/ -v |
| 80 | +``` |
| 81 | + |
| 82 | +### Running Specific Test Files |
| 83 | + |
| 84 | +Run main module tests: |
| 85 | + |
| 86 | +```bash |
| 87 | +pytest tests/test_main.py -v |
| 88 | +``` |
| 89 | + |
| 90 | +Run GUI tests: |
| 91 | + |
| 92 | +```bash |
| 93 | +pytest tests/test_gui.py -v |
| 94 | +``` |
| 95 | + |
| 96 | +### Running Specific Test Classes |
| 97 | + |
| 98 | +```bash |
| 99 | +pytest tests/test_main.py::TestMuscleGroupSelection -v |
| 100 | +``` |
| 101 | + |
| 102 | +### Running Specific Tests |
| 103 | + |
| 104 | +```bash |
| 105 | +pytest tests/test_main.py::TestMuscleGroupSelection::test_abs_selection_by_number -v |
| 106 | +``` |
| 107 | + |
| 108 | +## Coverage Reports |
| 109 | + |
| 110 | +### Generate Coverage Report |
| 111 | + |
| 112 | +```bash |
| 113 | +pytest tests/ --cov=. --cov-report=term-missing |
| 114 | +``` |
| 115 | + |
| 116 | +### Generate HTML Coverage Report |
| 117 | + |
| 118 | +```bash |
| 119 | +pytest tests/ --cov=. --cov-report=html |
| 120 | +``` |
| 121 | + |
| 122 | +Then open `htmlcov/index.html` in your browser. |
| 123 | + |
| 124 | +### Generate XML Coverage Report |
| 125 | + |
| 126 | +```bash |
| 127 | +pytest tests/ --cov=. --cov-report=xml |
| 128 | +``` |
| 129 | + |
| 130 | +## Test Configuration |
| 131 | + |
| 132 | +Test configuration is stored in `setup.cfg`: |
| 133 | + |
| 134 | +```ini |
| 135 | +[tool:pytest] |
| 136 | +testpaths = tests |
| 137 | +python_files = test_*.py |
| 138 | +python_classes = Test* |
| 139 | +python_functions = test_* |
| 140 | +addopts = |
| 141 | + -v |
| 142 | + --strict-markers |
| 143 | + --tb=short |
| 144 | + --cov=. |
| 145 | + --cov-report=term-missing |
| 146 | + --cov-report=html |
| 147 | + --cov-report=xml |
| 148 | + --cov-branch |
| 149 | +``` |
| 150 | + |
| 151 | +## Continuous Integration |
| 152 | + |
| 153 | +Tests are automatically run via GitHub Actions on: |
| 154 | +- Push to `main` and `develop` branches |
| 155 | +- Pull requests to `main` and `develop` branches |
| 156 | + |
| 157 | +The workflow tests against multiple Python versions: |
| 158 | +- Python 3.9 |
| 159 | +- Python 3.10 |
| 160 | +- Python 3.11 |
| 161 | +- Python 3.12 |
| 162 | + |
| 163 | +See `.github/workflows/tests.yml` for the full workflow configuration. |
| 164 | + |
| 165 | +## Writing New Tests |
| 166 | + |
| 167 | +When adding new tests, follow these guidelines: |
| 168 | + |
| 169 | +1. **Naming Convention** |
| 170 | + - Test files: `test_*.py` |
| 171 | + - Test classes: `Test*` |
| 172 | + - Test functions: `test_*` |
| 173 | + |
| 174 | +2. **Test Organization** |
| 175 | + - Group related tests in classes |
| 176 | + - Use descriptive test names |
| 177 | + - Add docstrings explaining what is being tested |
| 178 | + |
| 179 | +3. **Mocking** |
| 180 | + - Use `@patch` for mocking input/output |
| 181 | + - Mock external dependencies (filesystem, network, etc.) |
| 182 | + |
| 183 | +4. **Example Test** |
| 184 | + |
| 185 | +```python |
| 186 | +@patch('builtins.print') |
| 187 | +@patch('builtins.input') |
| 188 | +def test_abs_selection(mock_input, mock_print): |
| 189 | + """Test selecting abs muscle group.""" |
| 190 | + mock_input.side_effect = ['abs', 'quit'] |
| 191 | + |
| 192 | + with pytest.raises(SystemExit): |
| 193 | + main.workout() |
| 194 | + |
| 195 | + printed_output = [str(call) for call in mock_print.call_args_list] |
| 196 | + assert any('Ab muscle group selected' in str(call) for call in printed_output) |
| 197 | +``` |
| 198 | + |
| 199 | +## Troubleshooting |
| 200 | + |
| 201 | +### GUI Tests Skipped |
| 202 | + |
| 203 | +GUI tests may be skipped in headless environments (CI/CD). This is expected behavior as tkinter requires a display. |
| 204 | + |
| 205 | +### Import Errors |
| 206 | + |
| 207 | +If you encounter import errors, ensure you're running tests from the project root: |
| 208 | + |
| 209 | +```bash |
| 210 | +cd /path/to/PyWorkout |
| 211 | +pytest tests/ |
| 212 | +``` |
| 213 | + |
| 214 | +### Coverage Not Showing |
| 215 | + |
| 216 | +Ensure pytest-cov is installed: |
| 217 | + |
| 218 | +```bash |
| 219 | +pip install pytest-cov |
| 220 | +``` |
| 221 | + |
| 222 | +## Test Results |
| 223 | + |
| 224 | +Current test coverage: ~54% overall |
| 225 | +- Main module: ~51% coverage |
| 226 | +- Test suite: 25 tests passing |
| 227 | +- GUI tests: 5 tests (may skip in headless environments) |
| 228 | + |
| 229 | +## Contributing |
| 230 | + |
| 231 | +When contributing: |
| 232 | +1. Write tests for new features |
| 233 | +2. Ensure all tests pass before submitting PR |
| 234 | +3. Aim for >80% code coverage for new code |
| 235 | +4. Follow existing test patterns and conventions |
0 commit comments