Skip to content

Commit e17c524

Browse files
conftest: Add pytest.traced for debugging
1 parent 9c26b16 commit e17c524

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

tests/conftest.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
import pytest
88
import textwrap
99
import difflib
10+
import functools
1011
import re
1112
import sys
13+
import trace
1214
import contextlib
1315
import platform
1416
import gc
@@ -190,6 +192,25 @@ def gc_collect():
190192
gc.collect()
191193

192194

195+
def traced(f):
196+
"""Decorator to trace the output of a given function (or test case).
197+
198+
Example:
199+
200+
@pytest.traced
201+
def test_something():
202+
print("Hello")
203+
204+
"""
205+
tracer = trace.Trace(trace=1, count=0, ignoredirs=["/usr", sys.prefix])
206+
207+
@functools.wraps(f)
208+
def wrapped(*args, **kwargs):
209+
return tracer.runfunc(f, *args, **kwargs)
210+
211+
return wrapped
212+
213+
193214
def pytest_configure():
194215
"""Add import suppression and test requirements to `pytest` namespace"""
195216
try:
@@ -218,6 +239,7 @@ def pytest_configure():
218239
pytest.unsupported_on_py2 = skipif(sys.version_info.major < 3,
219240
reason="unsupported on Python 2.x")
220241
pytest.gc_collect = gc_collect
242+
pytest.traced = traced
221243

222244

223245
def _test_import_pybind11():

0 commit comments

Comments
 (0)