pytest-dev / pytest-django

A Django plugin for pytest.
https://pytest-django.readthedocs.io/
Other
1.37k stars 344 forks source link

Support for django-admin check and django-admin makemigrations --check #1006

Open n1ngu opened 2 years ago

n1ngu commented 2 years ago

As part of my test scripts I'll usually run django-admin check and django-admin makemigrations --check --dry-run to assert nobody missed something.

Am I missing a way to hook those administrative commands with this library? Wouldn't it be interesting to have some kind of cli flag or fixture to trigger them during a pytest run?

n1ngu commented 2 years ago

The tests can be reduced so something like

from django.core.management import call_command

def test_migrations(db):
    call_command("makemigrations", "--check", "--dry-run")

def test_check(db):
    call_command("check", "--fail-level", "WARNING")

I am somewhat bored of copypasting this in every project

AFAIU, pytest collection hooks could be used to extend the users test suite with this two unit tests.

https://docs.pytest.org/en/latest/reference/reference.html#collection-hooks

samamorgan commented 1 year ago

I personally don't think a library should be handling this. While it's certainly common and good to do this, the library shouldn't necessarily be implementing this for the user.

That being said, here's my version of these tests that I think are a bit more explicit than what you've shown.

import pytest
from django.core import management

@pytest.mark.django_db
def test_no_migrations(capsys: pytest.CaptureFixture):
    management.call_command("makemigrations", dry_run=True)

    captured: str = capsys.readouterr().out
    assert "No changes detected" in captured

def test_no_issues(capsys: pytest.CaptureFixture):
    management.call_command("check", fail_level="WARNING")

    captured: str = capsys.readouterr().out
    assert "System check identified no issues" in captured