-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpytest_django_sqlcount.py
63 lines (48 loc) · 1.99 KB
/
pytest_django_sqlcount.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import sys
import pytest
from _pytest.runner import pytest_runtest_makereport as prm
from _pytest.terminal import TerminalReporter
def pytest_addoption(parser):
group = parser.getgroup("django")
group._addoption(
'--sqlcount', action="store_true", dest="sqlcount", default=False,
help="Outputs the number of SQLs per test on default DB connection.")
@pytest.mark.trylast
def pytest_configure(config):
if hasattr(config, 'slaveinput'):
return
if config.option.sqlcount:
config._sql = SQLCountTerminalReporter(config, sys.stdout)
standard_reporter = config.pluginmanager.getplugin('terminalreporter')
config.pluginmanager.unregister(standard_reporter)
config.pluginmanager.register(config._sql, 'terminalreporter')
def pytest_unconfigure(config):
sqlcount = getattr(config, '_sql', None)
if sqlcount:
del config._sql
config.pluginmanager.unregister(sqlcount)
class SQLCountTerminalReporter(TerminalReporter):
def __init__(self, config, file=None):
self.sqlcount = config.getvalue('sqlcount')
if self.sqlcount and not config.option.verbose:
config.option.verbose = 1
TerminalReporter.__init__(self, config, file)
def pytest_runtest_logreport(self, report):
TerminalReporter.pytest_runtest_logreport(self, report)
res = self.config.hook.pytest_report_teststatus(report=report)
cat, letter, word = res
if not letter and not word:
# probably passed setup/teardown
return
if self.sqlcount:
self._tw.write(" %d" % report.sqlcount, blue=True)
def pytest_runtest_makereport(item, call):
report = prm(item, call)
report.sqlcount = len(getattr(item, 'sqlcount', []))
return report
def pytest_runtest_call(item):
from django.db import connection
from django.test.utils import CaptureQueriesContext
with CaptureQueriesContext(connection) as cqc:
item.runtest()
item.sqlcount = cqc