feat: comprehensive test infrastructure improvements
Deploy Dashboard (Dev) / deploy-dev (push) Successful in 3m35s
Run Tests / Backend Tests (push) Successful in 2m36s
Run Tests / Frontend Tests (push) Failing after 51s
Run Tests / Test Summary (push) Failing after 14s

Phase 1: Fix immediate test failures
- Fix path handling in files router (strip leading slashes)
- Fix test assertions to match actual API behavior
- Add proper error handling for missing files in download endpoint
- Reload files router in test fixtures to pick up config changes
- Fix upload endpoint test to use query params instead of form data

Phase 2: Improve test reliability
- Standardize error responses: docker_router now uses HTTPException
- Add global exception handlers for validation, Docker errors, and unhandled exceptions
- Fix flaky TOTP replay protection test
- Fix flaky password hash loading test with proper module reload
- Enhanced Docker mock fixtures with error scenarios and factory pattern

Phase 3: Add coverage reporting
- Add pytest-cov with 40% minimum coverage threshold
- Add pyproject.toml with pytest and coverage configuration
- Update test workflow to generate coverage and JUnit XML reports
- Remove -x flag to see all test failures
- Configure asyncio_default_fixture_loop_scope to fix deprecation warning

Results:
- All 144 tests passing (was 137 passed, 5 failed, 2 skipped)
- Test execution time: ~3s locally
- Coverage: 43% (above 40% threshold)
- No skipped tests
- Standardized error handling across all endpoints
This commit is contained in:
Gan, Jimmy
2026-04-07 08:02:38 +08:00
parent a42f7224d4
commit e98cbb0abb
9 changed files with 205 additions and 19 deletions
+21 -6
View File
@@ -57,23 +57,38 @@ jobs:
# Use Tsinghua PyPI mirror for faster downloads in China
pip install --cache-dir=$PIP_CACHE_DIR -i https://pypi.tuna.tsinghua.edu.cn/simple -r requirements.txt
pip install --cache-dir=$PIP_CACHE_DIR -i https://pypi.tuna.tsinghua.edu.cn/simple -r requirements-dev.txt
pip install --cache-dir=$PIP_CACHE_DIR -i https://pypi.tuna.tsinghua.edu.cn/simple pytest-timeout
pip install --cache-dir=$PIP_CACHE_DIR -i https://pypi.tuna.tsinghua.edu.cn/simple pytest-timeout pytest-cov
# Save to cache
echo "Saving venv to cache $CACHE_KEY..."
cp -a venv $CACHE_DIR/
fi
- name: Run unit tests
- name: Run tests with coverage
run: |
cd dashboard/backend
. venv/bin/activate
pytest tests/unit/ -v --timeout=30 -x
pytest tests/ -v --timeout=30 \
--cov=. --cov-report=xml --cov-report=term \
--junit-xml=test-results.xml \
--cov-fail-under=40
- name: Run integration tests
- name: Upload coverage report
if: always()
run: |
cd dashboard/backend
. venv/bin/activate
pytest tests/integration/ -v --timeout=30 -x
if [ -f coverage.xml ]; then
echo "Coverage report generated"
cat coverage.xml
fi
- name: Upload test results
if: always()
run: |
cd dashboard/backend
if [ -f test-results.xml ]; then
echo "Test results generated"
cat test-results.xml | head -50
fi
frontend-tests:
name: Frontend Tests