Skip to content

Commit 1415718

Browse files
committed
add warning for run pytest xdist with capture queries
1 parent 6fcb4a9 commit 1415718

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@
3838
- `explain_opts`: Parameters for explain. *(for more information about the parameters for explain, see the documentation for your DBMS).*
3939
- `connection`: Connecting to your database, by default: django.db.connection
4040

41+
42+
### > WARNING: If you use `pytest-xdist` and run the test with the `-n <workers>` flag, the results will not be reflected in the terminal. Remove the `-n <workers>` flag to display them.
43+
44+
4145
## Usage examples
4246

4347
```python
@@ -124,5 +128,5 @@ settings.PRINTER_HANDLERS.append("path.to.your.handler.SomeHandler")
124128
```
125129
126130
## TODO:
127-
1. Add support for async loop and async func decorator, __acall__, __aiter__, __anext__
131+
1. Add support for async loop and async func decorator, __call__, __aiter__, __anext__
128132
2. Add support for other ORM's, SQLAlchemy, etc.

src/capture_db_queries/decorators.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import functools
44
import statistics
5+
import sys
56
import time
67
import warnings
78
from functools import wraps
@@ -34,6 +35,24 @@
3435
__all__ = ('CaptureQueries', 'capture_queries', 'ExtCaptureQueriesContext')
3536

3637

38+
def _detect_pytest_xdist():
39+
try:
40+
is_pytest = sys.argv[0].endswith('pytest')
41+
except IndexError:
42+
pass
43+
else:
44+
is_xdist = '-n' in sys.argv
45+
is_spot_test_launch = any('.py::' in arg for arg in sys.argv)
46+
47+
if is_pytest and is_xdist and is_spot_test_launch:
48+
warnings.warn(
49+
'If you want to see the result of CaptureQueries, then remove the '
50+
'-n <workers> parameter when starting pytest or use --capture=tee-sys -rP parameters.',
51+
category=UserWarning,
52+
stacklevel=2,
53+
)
54+
55+
3756
class CaptureQueries:
3857
"""
3958
#### Class to simplify the search for slow and suboptimal sql queries\
@@ -70,6 +89,10 @@ class CaptureQueries:
7089
7190
---
7291
92+
#### WARNING: If you use `pytest-xdist` and run the test with the `-n <workers>` flag, the results will not be reflected in the terminal. Remove the `-n <workers>` flag to display them.
93+
94+
---
95+
7396
#### Usage examples::
7497
7598
for ctx in CaptureQueries(number_runs=2, advanced_verb=True):
@@ -92,6 +115,7 @@ def test_request():
92115
# OR
93116
94117
# NOTE: The with context manager does not support multi-launch number_runs > 1
118+
# NOTE: Also you can use `async with` if you capture queries in async context.
95119
with CaptureQueries(number_runs=1, advanced_verb=True) as ctx:
96120
response = self.client.get(url)
97121
@@ -140,6 +164,7 @@ def __init__(
140164
explain_opts: dict[str, Any] | None = None,
141165
connection: BaseDatabaseWrapper | None = None,
142166
) -> None:
167+
_detect_pytest_xdist()
143168
log.debug('')
144169

145170
self.assert_q_count = assert_q_count

0 commit comments

Comments
 (0)