Skip to content

Test/ci switch to pytest with coverage#5

Merged
histrio merged 3 commits into
mainfrom
test/ci-switch-to-pytest-with-coverage
Dec 3, 2025
Merged

Test/ci switch to pytest with coverage#5
histrio merged 3 commits into
mainfrom
test/ci-switch-to-pytest-with-coverage

Conversation

@histrio
Copy link
Copy Markdown
Contributor

@histrio histrio commented Dec 3, 2025

No description provided.

@codecov
Copy link
Copy Markdown

codecov Bot commented Dec 3, 2025

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 ☂️

@histrio histrio requested review from astynax and Copilot and removed request for astynax December 3, 2025 11:01
@histrio histrio force-pushed the test/ci-switch-to-pytest-with-coverage branch from 5a5ac06 to 0e64db5 Compare December 3, 2025 11:03
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 test to 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.

Comment thread conftest.py Outdated
Comment on lines +12 to +15

def pytest_configure(config):
"""Configure pytest for Django."""
django.setup()
Copy link

Copilot AI Dec 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
def pytest_configure(config):
"""Configure pytest for Django."""
django.setup()

Copilot uses AI. Check for mistakes.
Comment thread conftest.py Outdated
from django.conf import settings

# Ensure Django settings are configured for tests
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings")
Copy link

Copilot AI Dec 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings")

Copilot uses AI. Check for mistakes.
Comment thread config/tests/test_views.py Outdated
Comment on lines +7 to +10
@pytest.fixture
def client():
"""Provide a Django test client."""
return Client()
Copy link

Copilot AI Dec 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
@pytest.fixture
def client():
"""Provide a Django test client."""
return Client()

Copilot uses AI. Check for mistakes.
Comment thread Makefile
Comment on lines +17 to +21
@uv run pytest --cov-report=term --cov-report=html

.PHONY: test-cov-xml
test-cov-xml:
@uv run pytest --cov-report=xml
Copy link

Copilot AI Dec 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

  1. Remove the redundant --cov-report flags and rely on pyproject.toml defaults, or
  2. Use --no-cov to disable coverage and then re-enable specific reports if customization is needed.

The current implementation may produce duplicate reports.

Suggested change
@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

Copilot uses AI. Check for mistakes.
Comment thread pyproject.toml
Comment on lines +29 to +36
"--cov=config",
"--cov-report=term-missing",
"--cov-report=html",
"--cov-report=xml",
]

[tool.coverage.run]
source = ["."]
Copy link

Copilot AI Dec 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

  1. Using --cov=. and keeping source = ["."], or
  2. Removing the source setting from [tool.coverage.run] and relying on the --cov=config argument.

For comprehensive coverage of the entire project, option 1 is recommended.

Copilot uses AI. Check for mistakes.
Comment thread conftest.py Outdated

import os
import django
from django.conf import settings
Copy link

Copilot AI Dec 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Import of 'settings' is not used.

Suggested change
from django.conf import settings

Copilot uses AI. Check for mistakes.
@histrio histrio force-pushed the test/ci-switch-to-pytest-with-coverage branch from 0e64db5 to d80d3ff Compare December 3, 2025 11:22
@histrio histrio merged commit b4e33a3 into main Dec 3, 2025
5 checks passed
@histrio histrio deleted the test/ci-switch-to-pytest-with-coverage branch December 3, 2025 11:28
Copy link
Copy Markdown
Member

@astynax astynax left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants