Adding the --trace option.
This commit is contained in:
		
							parent
							
								
									8680dfc939
								
							
						
					
					
						commit
						54d3cd587d
					
				|  | @ -5,6 +5,8 @@ import sys | ||||||
| import os | import os | ||||||
| from doctest import UnexpectedException | from doctest import UnexpectedException | ||||||
| 
 | 
 | ||||||
|  | from _pytest.config import hookimpl | ||||||
|  | 
 | ||||||
| try: | try: | ||||||
|     from builtins import breakpoint  # noqa |     from builtins import breakpoint  # noqa | ||||||
| 
 | 
 | ||||||
|  | @ -12,7 +14,7 @@ try: | ||||||
| except ImportError: | except ImportError: | ||||||
|     SUPPORTS_BREAKPOINT_BUILTIN = False |     SUPPORTS_BREAKPOINT_BUILTIN = False | ||||||
| 
 | 
 | ||||||
| 
 | immediately_break = False | ||||||
| def pytest_addoption(parser): | def pytest_addoption(parser): | ||||||
|     group = parser.getgroup("general") |     group = parser.getgroup("general") | ||||||
|     group._addoption( |     group._addoption( | ||||||
|  | @ -28,6 +30,12 @@ def pytest_addoption(parser): | ||||||
|         help="start a custom interactive Python debugger on errors. " |         help="start a custom interactive Python debugger on errors. " | ||||||
|         "For example: --pdbcls=IPython.terminal.debugger:TerminalPdb", |         "For example: --pdbcls=IPython.terminal.debugger:TerminalPdb", | ||||||
|     ) |     ) | ||||||
|  |     group._addoption( | ||||||
|  |         "--trace", | ||||||
|  |         dest="trace", | ||||||
|  |         action="store_true", | ||||||
|  |         help="Immediately break when running each test.", | ||||||
|  |     ) | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| def pytest_configure(config): | def pytest_configure(config): | ||||||
|  | @ -63,6 +71,23 @@ def pytest_configure(config): | ||||||
|     config._cleanup.append(fin) |     config._cleanup.append(fin) | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  | 
 | ||||||
|  | @hookimpl(hookwrapper=True) | ||||||
|  | def pytest_pyfunc_call(pyfuncitem): | ||||||
|  |     if immediately_break: | ||||||
|  |         pytestPDB.set_trace(set_break=False) | ||||||
|  |         testfunction = pyfuncitem.obj | ||||||
|  |         pyfuncitem.obj = pdb.runcall | ||||||
|  |         if pyfuncitem._isyieldedfunction(): | ||||||
|  |             pyfuncitem.args = [testfunction, pyfuncitem._args] | ||||||
|  |         else: | ||||||
|  |             pyfuncitem.funcargs['func'] = testfunction | ||||||
|  |             new_list = list(pyfuncitem._fixtureinfo.argnames) | ||||||
|  |             new_list.append('func') | ||||||
|  |             pyfuncitem._fixtureinfo.argnames = tuple(new_list) | ||||||
|  |     outcome = yield | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
| class pytestPDB(object): | class pytestPDB(object): | ||||||
|     """ Pseudo PDB that defers to the real pdb. """ |     """ Pseudo PDB that defers to the real pdb. """ | ||||||
| 
 | 
 | ||||||
|  | @ -71,7 +96,7 @@ class pytestPDB(object): | ||||||
|     _pdb_cls = pdb.Pdb |     _pdb_cls = pdb.Pdb | ||||||
| 
 | 
 | ||||||
|     @classmethod |     @classmethod | ||||||
|     def set_trace(cls): |     def set_trace(cls, set_break=True): | ||||||
|         """ invoke PDB set_trace debugging, dropping any IO capturing. """ |         """ invoke PDB set_trace debugging, dropping any IO capturing. """ | ||||||
|         import _pytest.config |         import _pytest.config | ||||||
| 
 | 
 | ||||||
|  | @ -84,6 +109,7 @@ class pytestPDB(object): | ||||||
|             tw.line() |             tw.line() | ||||||
|             tw.sep(">", "PDB set_trace (IO-capturing turned off)") |             tw.sep(">", "PDB set_trace (IO-capturing turned off)") | ||||||
|             cls._pluginmanager.hook.pytest_enter_pdb(config=cls._config) |             cls._pluginmanager.hook.pytest_enter_pdb(config=cls._config) | ||||||
|  |         if set_break: | ||||||
|             cls._pdb_cls().set_trace(frame) |             cls._pdb_cls().set_trace(frame) | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue