#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
-----------------------------------+--------------------------------------
Changes (by wrwrwr):

 * needs_docs:   => 0
 * needs_tests:   => 0
 * needs_better_patch:   => 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 it such cases at all?

New 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?

--

--
Ticket URL: <https://code.djangoproject.com/ticket/23930#comment:1>
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.a21463cfbbbead35e427e0b7d273bdb6%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to