pytest-dev / pytest-dev/pytest-django

Idea: Ability to capture queries but not fail the test

Open
#1,034 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
1.5k
Forks
367
PR merge metrics
No merged PRs in 30d

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing the django_assert_num_queries entry point and the existing pytest-django CLI option registration. Explore how query-count failures are reported, then determine how a development-only flag would turn that failure into a warning without changing the default behavior. Done means the example can inspect its assertions while still displaying the query mismatch, with tests covering both modes.

Written by the indexing model from the issue text.

Assessment

Tech stack
django, python
Domain
testing
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.