跳转到主要内容

从您的Django数据库性能测试中生成性能报告。

项目描述

pytest-django-queries

从您的Django数据库性能测试中生成性能报告(受coverage.py启发)。

Requirement Status Coverage Status Documentation Status Version Latest Unstable on pypi

Commits since latest release Supported versions Supported implementations

使用方法

安装pytest-django-queries,编写您的pytest测试,并标记任何需要计数或使用count_queries固定装置的测试。

注意:要使用最新开发版本,请使用pip install --pre pytest-django-queries

import pytest


@pytest.mark.count_queries
def test_query_performances():
    Model.objects.all()


# Or...
def test_another_query_performances(count_queries):
    Model.objects.all()

每个测试文件和/或包被视为一个类别。类别内的每个测试都组成其数据,有关详细信息,请参阅可视化结果

您可以在此处找到完整文档。

使用固定装置时的建议

您可能会遇到想要添加生成您不想计数的查询的固定装置的情况,或者您只想与pytest-django-queries一起使用pytest-django插件,这将生成您不需要的结果中的查询。

为此,您需要将count_queries固定装置放在最后一个执行。

但与此同时,您可能还想使用pytest标记的强大功能,将查询计数测试与其他测试分开。在这种情况下,您可能想这样做来告诉标记不要自动将count_queries固定装置注入到测试中

import pytest


@pytest.mark.count_queries(autouse=False)
def test_retrieve_main_menu(fixture_making_queries, count_queries):
    pass

请注意关键字参数autouse=False的使用,以及count_queries测试夹具放在最后。

将pytest-django与pytest-django-queries一起使用

当使用pytest-django时,我们建议您执行以下操作

import pytest


@pytest.mark.django_db
@pytest.mark.count_queries(autouse=False)
def test_retrieve_main_menu(any_fixture, other_fixture, count_queries):
    pass

集成GitHub

待定。

本地测试

只需通过pip安装pytest-django-queries,然后使用pytest运行您的测试。一个报告应该已经生成在您的当前工作目录中,文件名为.pytest-queries

注意:要覆盖保存路径,请将--django-db-bench PATH选项传递给pytest。

可视化结果

您可以使用show命令从测试结果生成表格

django-queries show

您将得到如下所示的表示结果

+---------+--------------------------------------+
| Module  |          Tests                       |
+---------+--------------------------------------+
| module1 | +-----------+---------+------------+ |
|         | | Test Name | Queries | Duplicated | |
|         | +-----------+---------+------------+ |
|         | |   test1   |    0    |     0      | |
|         | +-----------+---------+------------+ |
|         | |   test2   |    1    |     0      | |
|         | +-----------+---------+------------+ |
+---------+--------------------------------------+
| module2 | +-----------+---------+------------+ |
|         | | Test Name | Queries | Duplicated | |
|         | +-----------+---------+------------+ |
|         | |   test1   |   123   |     0      | |
|         | +-----------+---------+------------+ |
+---------+--------------------------------------+

导出结果(HTML)

为了更好的展示,使用html命令导出结果为HTML。

django-queries html

它将生成如下

比较结果

在运行测试后,您可以运行django-queries backup(可以指定路径,可选)然后重新运行。之后,您可以运行django-queries diff以生成类似以下的结果

screenshot

开发

首先,在本地克隆项目。然后,使用以下命令安装它。

./setup.py develop

之后,您需要安装开发和测试需求。为此,请运行以下命令。

pip install -e .[test]

项目详情


下载文件

下载适合您平台的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。

源分发

pytest-django-queries-1.2.0.tar.gz (14.7 kB 查看哈希值)

上传时间

由以下支持

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误记录 StatusPage StatusPage 状态页面