Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/josef-friedrich/stdout_stderr_capturing
Capture the stdout or stderr output as a list in a context manager block (with).
https://github.com/josef-friedrich/stdout_stderr_capturing
Last synced: about 1 month ago
JSON representation
Capture the stdout or stderr output as a list in a context manager block (with).
- Host: GitHub
- URL: https://github.com/josef-friedrich/stdout_stderr_capturing
- Owner: Josef-Friedrich
- License: mit
- Created: 2022-07-15T14:08:35.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-04-13T09:12:35.000Z (9 months ago)
- Last Synced: 2024-11-19T17:58:44.193Z (about 2 months ago)
- Language: Python
- Homepage:
- Size: 51.8 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.rst
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
.. image:: http://img.shields.io/pypi/v/stdout-stderr-capturing.svg
:target: https://pypi.org/project/stdout-stderr-capturing
:alt: This package on the Python Package Index.. image:: https://github.com/Josef-Friedrich/stdout_stderr_capturing/actions/workflows/tests.yml/badge.svg
:target: https://github.com/Josef-Friedrich/stdout_stderr_capturing/actions/workflows/tests.yml
:alt: Testsstdout_stderr_capturing
=======================Capture the stdout or stderr output as a list in a context manager block (with).
Maybe better alternatives:
* `capturer `_ https://github.com/xolox/python-capturer
* `stdio-mgr `_
* `OutputCatcher `_
* `wurlitzer `_With Python 3:
.. code:: python
from io import StringIO
from contextlib import redirect_stdout, redirect_stderrstdout = StringIO()
stderr = StringIO()
with redirect_stdout(stdout), redirect_stderr(stderr):
print('Test')
stdout.getvalue()
stderr.getvalue()Using `pytest `_
.. code:: python
def test_myoutput(capsys): # or use "capfd" for fd-level
print("hello")
sys.stderr.write("world\n")
captured = capsys.readouterr()
assert captured.out == "hello\n"
assert captured.err == "world\n"
print("next")
captured = capsys.readouterr()
assert captured.out == "next\n"Capture stdout:
.. code:: python
with Capturing() as output:
print('line 1')print(output[0])
is equivalent to
.. code:: python
with Capturing(stream='stdout') as output:
print('line 1')Capture stderr:
.. code:: python
with Capturing(stream='stderr') as output:
print('line 1', file=sys.stderr)