Test/ci switch to pytest with coverage#5
Conversation
Welcome to Codecov 🎉Once you merge this PR into your default branch, you're all set! Codecov will compare coverage reports and display results in all future pull requests. Thanks for integrating Codecov - We've got you covered ☂️ |
5a5ac06 to
0e64db5
Compare
There was a problem hiding this comment.
Pull request overview
This PR switches the testing infrastructure from Django's built-in test framework to pytest with coverage reporting. The changes introduce pytest, pytest-django, and pytest-cov dependencies, configure comprehensive coverage tracking, and add initial tests for the health check endpoint.
Key Changes:
- Migrated from Django's
manage.py testto pytest with pytest-django integration - Added pytest-cov for code coverage tracking with Codecov integration in CI
- Configured coverage reporting with exclusions for test files, migrations, and configuration files
Reviewed changes
Copilot reviewed 7 out of 10 changed files in this pull request and generated 6 comments.
Show a summary per file
| File | Description |
|---|---|
| uv.lock | Added dependencies for pytest (9.0.1), pytest-django (4.11.1), pytest-cov (7.0.0), coverage (7.12.0), and supporting packages |
| requirements.txt | Updated with new test dependencies and their hashes |
| pyproject.toml | Configured pytest options, coverage settings, and added dev dependencies for testing |
| conftest.py | Created pytest configuration file with Django setup |
| config/tests/test_views.py | Added pytest-based tests for health check endpoint |
| config/tests/init.py | Created empty init file for test package |
| Makefile | Added test, test-cov, and test-cov-xml targets |
| .github/workflows/ci.yml | Updated CI to use pytest and upload coverage to Codecov |
| ansible/deploy.yml | Fixed deprecated ansible_lsb usage (unrelated to testing changes) |
| README.md | Added CI, Codecov, Python, and Django badges |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
|
||
| def pytest_configure(config): | ||
| """Configure pytest for Django.""" | ||
| django.setup() |
There was a problem hiding this comment.
The pytest_configure hook manually calls django.setup(), but this is unnecessary when using pytest-django. The pytest-django plugin automatically handles Django setup when DJANGO_SETTINGS_MODULE is configured in pyproject.toml (line 24). This manual setup can potentially cause issues or double initialization. Consider removing the pytest_configure function and relying on pytest-django's automatic setup.
| def pytest_configure(config): | |
| """Configure pytest for Django.""" | |
| django.setup() |
| from django.conf import settings | ||
|
|
||
| # Ensure Django settings are configured for tests | ||
| os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings") |
There was a problem hiding this comment.
The Django settings module is configured twice - once manually with os.environ.setdefault (line 10) and once in pyproject.toml via DJANGO_SETTINGS_MODULE (line 24). When using pytest-django, the configuration in pyproject.toml is sufficient. The environment variable setting in conftest.py is redundant and should be removed to avoid confusion about which configuration takes precedence.
| os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings") |
| @pytest.fixture | ||
| def client(): | ||
| """Provide a Django test client.""" | ||
| return Client() |
There was a problem hiding this comment.
The custom client fixture shadows pytest-django's built-in client fixture. Pytest-django already provides a client fixture that returns a Django test client, so this custom fixture is unnecessary. Remove this fixture and use the built-in one provided by pytest-django.
| @pytest.fixture | |
| def client(): | |
| """Provide a Django test client.""" | |
| return Client() |
| @uv run pytest --cov-report=term --cov-report=html | ||
|
|
||
| .PHONY: test-cov-xml | ||
| test-cov-xml: | ||
| @uv run pytest --cov-report=xml |
There was a problem hiding this comment.
The test-cov and test-cov-xml targets override the coverage report settings defined in pyproject.toml (lines 30-32). The pyproject.toml already configures --cov-report=term-missing, --cov-report=html, and --cov-report=xml in the addopts section. These Makefile targets should either:
- Remove the redundant
--cov-reportflags and rely on pyproject.toml defaults, or - Use
--no-covto disable coverage and then re-enable specific reports if customization is needed.
The current implementation may produce duplicate reports.
| @uv run pytest --cov-report=term --cov-report=html | |
| .PHONY: test-cov-xml | |
| test-cov-xml: | |
| @uv run pytest --cov-report=xml | |
| @uv run pytest | |
| .PHONY: test-cov-xml | |
| test-cov-xml: | |
| @uv run pytest |
| "--cov=config", | ||
| "--cov-report=term-missing", | ||
| "--cov-report=html", | ||
| "--cov-report=xml", | ||
| ] | ||
|
|
||
| [tool.coverage.run] | ||
| source = ["."] |
There was a problem hiding this comment.
There is a conflicting coverage configuration. Line 29 specifies --cov=config which limits coverage to the config module, but line 36 specifies source = ["."] which tells coverage to measure the entire project. This can lead to inconsistent results. Consider either:
- Using
--cov=.and keepingsource = ["."], or - Removing the
sourcesetting from[tool.coverage.run]and relying on the--cov=configargument.
For comprehensive coverage of the entire project, option 1 is recommended.
|
|
||
| import os | ||
| import django | ||
| from django.conf import settings |
There was a problem hiding this comment.
Import of 'settings' is not used.
| from django.conf import settings |
0e64db5 to
d80d3ff
Compare
No description provided.