pytest-django icon indicating copy to clipboard operation
pytest-django copied to clipboard

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

Open n1ngu opened this issue 2 years ago • 2 comments

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 avatar Apr 11 '22 12:04 n1ngu

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

n1ngu avatar Sep 02 '22 09:09 n1ngu

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

samamorgan avatar Jan 26 '23 20:01 samamorgan