Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added assertMessages() from django.contrib.messages. #1109

Merged
merged 2 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ Donald Stufft <donald@stufft.io>
Nicolas Delaby <ticosax@free.fr>
Hasan Ramezani <hasan.r67@gmail.com>
Michael Howitz
Mark Gensler
9 changes: 9 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
Changelog
=========

Pending
-------

Improvements
^^^^^^^^^^^^

* Added `pytest.asserts.assertMessages()` to mimic the behaviour of the
`django.contrib.messages.test.MessagesTestMixin` function for Django versions >= 5.0.

v4.7.0 (2023-11-08)
-------------------

Expand Down
28 changes: 27 additions & 1 deletion pytest_django/asserts.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,22 @@
from functools import wraps
from typing import TYPE_CHECKING, Any, Callable, Sequence

from django import VERSION
from django.test import LiveServerTestCase, SimpleTestCase, TestCase, TransactionTestCase


test_case = TestCase("run")
USE_CONTRIB_MESSAGES = VERSION >= (5, 0)

if USE_CONTRIB_MESSAGES:
from django.contrib.messages import Message
from django.contrib.messages.test import MessagesTestMixin

Check warning on line 17 in pytest_django/asserts.py

View check run for this annotation

Codecov / codecov/patch

pytest_django/asserts.py#L16-L17

Added lines #L16 - L17 were not covered by tests

class MessagesTestCase(MessagesTestMixin, TestCase):
pass

Check warning on line 20 in pytest_django/asserts.py

View check run for this annotation

Codecov / codecov/patch

pytest_django/asserts.py#L19-L20

Added lines #L19 - L20 were not covered by tests

test_case = MessagesTestCase("run")

Check warning on line 22 in pytest_django/asserts.py

View check run for this annotation

Codecov / codecov/patch

pytest_django/asserts.py#L22

Added line #L22 was not covered by tests
else:
test_case = TestCase("run")


def _wrapper(name: str):
Expand All @@ -31,6 +43,11 @@
{attr for attr in vars(TransactionTestCase) if attr.startswith("assert")},
)

if USE_CONTRIB_MESSAGES:
assertions_names.update(
{attr for attr in vars(MessagesTestMixin) if attr.startswith("assert")},
)

for assert_func in assertions_names:
globals()[assert_func] = _wrapper(assert_func)
__all__.append(assert_func) # noqa: PYI056
Expand Down Expand Up @@ -213,6 +230,15 @@
):
...

# Added in Django 5.0.
def assertMessages(
response: HttpResponseBase,
expected_messages: Sequence[Message],
*args,
ordered: bool = ...,
) -> None:
...

# Fallback in case Django adds new asserts.
def __getattr__(name: str) -> Callable[..., Any]:
...
11 changes: 10 additions & 1 deletion tests/test_asserts.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,18 @@
"""
from unittest import TestCase as DefaultTestCase

from django import VERSION
from django.test import TestCase as DjangoTestCase

obj = DjangoTestCase("run")
if VERSION >= (5, 0):
from django.contrib.messages.test import MessagesTestMixin

Check warning on line 24 in tests/test_asserts.py

View check run for this annotation

Codecov / codecov/patch

tests/test_asserts.py#L24

Added line #L24 was not covered by tests

class MessagesTestCase(MessagesTestMixin, DjangoTestCase):
pass

Check warning on line 27 in tests/test_asserts.py

View check run for this annotation

Codecov / codecov/patch

tests/test_asserts.py#L26-L27

Added lines #L26 - L27 were not covered by tests

obj = MessagesTestCase("run")

Check warning on line 29 in tests/test_asserts.py

View check run for this annotation

Codecov / codecov/patch

tests/test_asserts.py#L29

Added line #L29 was not covered by tests
else:
obj = DjangoTestCase("run")

def is_assert(func) -> bool:
return func.startswith("assert") and "_" not in func
Expand Down