Package: pytest-django-queries
pytest-django-queriesGenerate performance reports from your django database performance tests
  (inspired bycoverage.py).UsageInstallpytest-django-queries, write your pytest tests and mark any
test that should be counted or use thecount_queriesfixture.Note: to use the latest development build, usepip install --pre pytest-django-queriesimportpytest@pytest.mark.count_queriesdeftest_query_performances():Model.objects.all()# Or...deftest_another_query_performances(count_queries):Model.objects.all()Each test file and/or package is considered as a category. Each test inside a "category"
compose its data, seeVisualising Resultsfor more details.You will find thefull documentation here.Recommendation when Using FixturesYou might end up in the case where you want to add fixtures that are generating queries
that you don't want to be counted in the results–or simply, you want to use thepytest-djangoplugin alongside ofpytest-django-queries, which will generate
unwanted queries in your results.For that, you will want to put thecount_queriesfixture as the last fixture to execute.But at the same time, you might want to use the the power of pytest markers, to separate
the queries counting tests from other tests. In that case, you might want to do something
like this to tell the marker to not automatically inject thecount_queriesfixture into
your test:importpytest@pytest.mark.count_queries(autouse=False)deftest_retrieve_main_menu(fixture_making_queries,count_queries):passNotice the usage of the keyword argumentautouse=Falseand thecount_queriesfixture
being placed last.Using pytest-django alongside of pytest-django-queriesWe recommend you to do the following when usingpytest-django:importpytest@pytest.mark.django_db@pytest.mark.count_queries(autouse=False)deftest_retrieve_main_menu(any_fixture,other_fixture,count_queries):passIntegrating with GitHubTBA.Testing LocallySimply installpytest-django-queriesthrough pip and run your
tests usingpytest. A report should have been generated in your
current working directory in a file called with.pytest-queries.Note: to override the save path, pass the--django-db-bench PATHoption to pytest.Visualising ResultsYou can generate a table from the tests results by using theshowcommand:django-queriesshowYou will get something like this to represent the results:+---------+--------------------------------------+|Module|Tests|+---------+--------------------------------------+|module1|+-----------+---------+------------+||||TestName|Queries|Duplicated||||+-----------+---------+------------+||||test1|0|0||||+-----------+---------+------------+||||test2|1|0||||+-----------+---------+------------+|+---------+--------------------------------------+|module2|+-----------+---------+------------+||||TestName|Queries|Duplicated||||+-----------+---------+------------+||||test1|123|0||||+-----------+---------+------------+|+---------+--------------------------------------+Exporting the Results (HTML)For a nicer presentation, use thehtmlcommand, to export the results as HTML.django-querieshtmlIt will generate somethinglike this.Comparing ResultsYou can rundjango-queries backup(can take a path, optionally) after
running your tests then rerun them. After that, you can rundjango-queries diffto generate results looking like this:DevelopmentFirst of all, clone the project locally. Then, install it using the below command../setup.pydevelopAfter that, you need to install the development and testing requirements. For that,
run the below command.pipinstall-e.[test]