Skip to content

Instantly share code, notes, and snippets.

@czue
Created November 20, 2024 14:18
Show Gist options
  • Save czue/38762d58588627f57e23a1535a7ad6c8 to your computer and use it in GitHub Desktop.
Save czue/38762d58588627f57e23a1535a7ad6c8 to your computer and use it in GitHub Desktop.
assertNumQueries example
from django.test import TransactionTestCase
from .services import my_function_that_hits_the_db
class MyTest(TransactionTestCase):
def test_db_performance(self):
# called directly
self.assertNumQueries(7, my_function_that_hits_the_db)
# used as a context manager
with self.assertNumQueries(2):
Person.objects.create(name="Aaron")
Person.objects.create(name="Beth")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment