Proposal: Numeric difference assertion

When writing tests, I often write assertions to test that objects are created or deleted (or not). I wrote a custom assertion to reduce the boilerplate and make the test more readable.

class TestHelpers(TestCase):
    @contextmanager
    def assert_difference(self, queryset, difference=1, expression="count"):
        """
        Test the difference between the return value of a queryset before and after
        the yielded block.
        
        Example:

        ```
        with self.assert_difference(User.objects, difference=1):
            User.objects.create_user("john", "john@beatles.com", "pwd")
        ```
        """
        func = getattr(queryset, expression)
        initial_count = func()
        yield
        new_count = func()
        self.assertEqual(new_count, initial_count + difference)

I’d love to get your feedback if this should be added to the framework.

Hmmmmm this is my thinking…
I often use assert_max_num_queries which is in pytest-django:
https://pytest-django.readthedocs.io/en/latest/helpers.html#django-assert-max-num-queries

I think this has quite a good case to come to core.

Do you want to first get this added to pytest-django (or any other testing helper package)?
Then you can have a stronger case to add it to core if it’s well used?

I think this helper is probably a bit niche to add to either Django or pytest-django. To me, this is a perfect example of “my tests make these kinds of assertions over and over, I’ll write a short function to help”.