Opened 10 years ago

Last modified 10 years ago

#23930 closed New feature

Context manager for capturing output — at Version 5

Reported by: Wojtek Ruszczewski Owned by: nobody
Component: Testing framework Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Wojtek Ruszczewski)

I've recently written something like this (for #23929, where the output is generated by a signal handler):

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

Change History (5)

comment:1 by Wojtek Ruszczewski, 10 years ago

Description: modified (diff)

comment:2 by Berker Peksag, 10 years ago

What about stderr and stdin? CPython has similar helper functions for standard streams: https://hg.python.org/cpython/file/default/Lib/test/support/__init__.py#l1361

comment:3 by Tim Graham, 10 years ago

I am tempted to use those functions from Python although their use is discouraged: "The test package is meant for internal use by Python only. It is documented for the benefit of the core developers of Python. Any use of this package outside of Python’s standard library is discouraged as code mentioned here can change or be removed without notice between releases of Python."

We could easily copy them into Django if they went away at some point.

comment:4 by Aymeric Augustin, 10 years ago

test.support doesn't appear to be available on Python 2.

comment:5 by Wojtek Ruszczewski, 10 years ago

Description: modified (diff)

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 test.test_support.

Note: See TracTickets for help on using tickets.
Back to Top