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

Idea: Ability to capture queries but not fail the test #1034

Open
MichalPodeszwa opened this issue Oct 20, 2022 · 1 comment
Open

Idea: Ability to capture queries but not fail the test #1034

MichalPodeszwa opened this issue Oct 20, 2022 · 1 comment

Comments

@MichalPodeszwa
Copy link

Let's say we've got a following test:

# models.py
from django.db import models


class Post(models.Model):
    title = models.CharField(max_length=255)


class PostHistory(models.Model):
    post = models.ForeignKey(Post, on_delete=models.CASCADE)


def create_post(title):
    post = Post.objects.create(title=title)
    PostHistory.objects.create(post=post)


# test_models.py
def test_saving_element_works(django_assert_num_queries):
    with django_assert_num_queries(2):
        create_post("foo")

    post = Post.objects.first()
    assert post
    assert post.title == 'foo'
    assert PostHistory.objects.filter(post=post).count() == 1

This test would pass of course just fine.

Let's say For some reason I remove the line PostHistory.objects.create(post=post).

Now the test fails with the number of queries being incorrect (1 instead of 2). But in order to debug this using the following test, I'd have to either comment out the num_queries assertion, or have a separate test for just testing the query count, but that's a solution we don't really want, as there'll always be a test case that tests the logic, but doesn't really check if the queries match (you could have n-queries inside if-statement for example).

As a potential solution for this I was thinking of having a way to not fail the test (only for development), but rather only display the message as a warning.

I think ideally, we could add a CLI flag in the lines of --no-fail-on-django-query-count or something similar.

I'm happy to work on this, just wanted to see if there's any interest in adding something like that, or if there's a better solution to this issue.

@bluetech
Copy link
Member

pytest-django uses Django's assertNumQueries, which doesn't support this, so you'd need to propose this to Django.

As for my own opinion, I think this usecase is a bit too niche to be worth the extra mental overhead. You can implement such a fixture yourself in a local conftest.py file without too much effort using Django's CaptureQueriesContext. Adding support for --no-fail-on-django-query-count in a custom fixture is very easy too using a pytest_addoption hook.

If you implement this, make sure to post the snippet here for anyone else who might want this :)

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

No branches or pull requests

2 participants