|
| 1 | +How to capture |
| 2 | +============== |
| 3 | + |
| 4 | +Default stdout/stderr/stdin capturing behavior |
| 5 | +---------------------------------------------- |
| 6 | + |
| 7 | +During task execution any output sent to ``stdout`` and ``stderr`` is captured. If a |
| 8 | +task its according captured output will usually be shown along with the failure |
| 9 | +traceback. |
| 10 | + |
| 11 | +In addition, ``stdin`` is set to a "null" object which will fail on attempts to read |
| 12 | +from it because it is rarely desired to wait for interactive input when running |
| 13 | +automated tasks. |
| 14 | + |
| 15 | +By default capturing is done by intercepting writes to low level file descriptors. This |
| 16 | +allows to capture output from simple print statements as well as output from a |
| 17 | +subprocess started by a task. |
| 18 | + |
| 19 | + |
| 20 | +Setting capturing methods or disabling capturing |
| 21 | +------------------------------------------------ |
| 22 | + |
| 23 | +There are three ways in which ``pytask`` can perform capturing: |
| 24 | + |
| 25 | +* ``fd`` (file descriptor) level capturing (default): All writes going to the operating |
| 26 | + system file descriptors 1 and 2 will be captured. |
| 27 | + |
| 28 | +* ``sys`` level capturing: Only writes to Python files ``sys.stdout`` and ``sys.stderr`` |
| 29 | + will be captured. No capturing of writes to file descriptors is performed. |
| 30 | + |
| 31 | +* ``tee-sys`` capturing: Python writes to ``sys.stdout`` and ``sys.stderr`` will be |
| 32 | + captured, however the writes will also be passed-through to the actual ``sys.stdout`` |
| 33 | + and ``sys.stderr``. |
| 34 | + |
| 35 | +You can influence output capturing mechanisms from the command line: |
| 36 | + |
| 37 | +.. code-block:: console |
| 38 | +
|
| 39 | + pytask -s # disable all capturing |
| 40 | + pytask --capture=sys # replace sys.stdout/stderr with in-mem files |
| 41 | + pytask --capture=fd # also point filedescriptors 1 and 2 to temp file |
| 42 | + pytask --capture=tee-sys # combines 'sys' and '-s', capturing sys.stdout/stderr |
| 43 | + # and passing it along to the actual sys.stdout/stderr |
| 44 | +
|
| 45 | +
|
| 46 | +Using print statements for debugging |
| 47 | +------------------------------------ |
| 48 | + |
| 49 | +One primary benefit of the default capturing of stdout/stderr output is that you can use |
| 50 | +print statements for debugging: |
| 51 | + |
| 52 | +.. code-block:: python |
| 53 | +
|
| 54 | + # content of task_capture.py |
| 55 | +
|
| 56 | +
|
| 57 | + def task_func1(): |
| 58 | + assert True |
| 59 | +
|
| 60 | +
|
| 61 | + def task_func2(): |
| 62 | + print("Debug statement") |
| 63 | + assert False |
| 64 | +
|
| 65 | +and running this module will show you precisely the output of the failing function and |
| 66 | +hide the other one: |
| 67 | + |
| 68 | +.. code-block:: console |
| 69 | +
|
| 70 | + $ pytask -s |
| 71 | + ========================= Start pytask session ========================= |
| 72 | + Platform: win32 -- Python 3.x.x, pytask 0.x.x, pluggy 0.13.x |
| 73 | + Root: . |
| 74 | + Collected 2 task(s). |
| 75 | +
|
| 76 | + F. |
| 77 | + =============================== Failures =============================== |
| 78 | + _________________ Task task_capture.py::task_func2 failed ______________ |
| 79 | +
|
| 80 | + Traceback (most recent call last): |
| 81 | + File "task_capture.py", line 7, in task_func2 |
| 82 | + assert False |
| 83 | + AssertionError |
| 84 | +
|
| 85 | + ---------------------- Captured stdout during call --------------------- |
| 86 | + Debug statement. |
| 87 | +
|
| 88 | + ==================== 1 succeeded, 1 failed in 0.01s ==================== |
0 commit comments