#23930: Context manager for capturing output
-----------------------------------+--------------------------------------
Reporter: wrwrwr | Owner: nobody
Type: New feature | Status: new
Component: Testing framework | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------------+--------------------------------------
Old description:
> I've recently something like this (for #23929, where the output is
> generated by a signal handler):
>
> {{{#!python
> @contextmanager
> def capture_stdout():
> """
> Captures the ``sys.stdout`` stream, making anything written to it
> available in the stream returned as the manager's context.
>
> For example:
>
> with capture_stdout() as out:
> # Print something.
> self.assertIn("something", out.getvalue())
> """
> old_stdout = sys.stdout
> sys.stdout = out = StringIO()
> try:
> yield out
> finally:
> sys.stdout = old_stdout
> }}}
>
> Would it be better to make it public, leave it buried for the test only
> or don't mind testing the output in such cases at all?
New description:
I've recently written something like this (for #23929, where the output is
generated by a signal handler):
{{{#!python
@contextmanager
def capture_stdout():
"""
Captures the ``sys.stdout`` stream, making anything written to it
available in the stream returned as the manager's context.
For example:
with capture_stdout() as out:
# Print something.
self.assertIn("something", out.getvalue())
"""
old_stdout = sys.stdout
sys.stdout = out = StringIO()
try:
yield out
finally:
sys.stdout = old_stdout
}}}
Would it be better to make it public, leave it buried for the test only or
don't bother testing the output in such cases at all?
--
Comment (by wrwrwr):
Thanks for the link, wasn't aware of the `test.support` module. It seems
to be available for Python 2 also (at least `captured_stdout()`), but
under a slightly different name
[https://docs.python.org/2.7/library/test.html#module-test.test_support
test.test_support].
--
Ticket URL: <https://code.djangoproject.com/ticket/23930#comment:5>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/064.05d5e0c1fd93d66ea3b0dd8938a95153%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.