Django: is there a way to count SQL queries from an unit test?

前端 未结 8 917
广开言路
广开言路 2021-01-31 13:55

I am trying to find out the number of queries executed by a utility function. I have written a unit test for this function and the function is working well. What I would like to

8条回答
  •  无人及你
    2021-01-31 14:46

    If you are using pytest, pytest-django has django_assert_num_queries fixture for this purpose:

    def test_queries(django_assert_num_queries):
        with django_assert_num_queries(3):
            Item.objects.create('foo')
            Item.objects.create('bar')
            Item.objects.create('baz')
    

提交回复
热议问题