pythonstdoutdoctest

How to run doc-tests without printing output


I want to run doc-tests and get the number of failures, but not print any output. For example, I tried this:

    with open(os.devnull, 'w') as sys.stdout:
        tests_failed, tests_run = doctest.testmod(some_module,
                                                  optionflags=doctest.ELLIPSIS)

but this does not play nice with the test runner suite, which requires sys.stdout to write to a JSON file.

How can I run doc-tests without printing any output?


Solution

  • To capture (and thus silence) stdout, I use contextlib.redirect_stdout:

    import io
    import contextlib
    
    [...]
        
        # in function running tests:
        f = io.StringIO()
        with contextlib.redirect_stdout(f):
           # run code that outputs stuff to stdout
    
        # stdout contents are now in f.get_value(), which you can use 
        # for further assertions, to output to a file, etc.
    

    I did some experiments, to make sure this plays nice with doctest, and it seems to:

    >>> f = io.StringIO()
    >>> with contextlib.redirect_stdout(f):
    ...     doctest.testmod(my_project.utils)
    ...
    >>> f.getvalue()
    'TestResults(failed=0, attempted=10)\n'