Using doctest and executing a process

Tim Peters tim.one at comcast.net
Mon Mar 11 18:06:19 EST 2002


[Douglas Garstang]
> I'm trying to integrate execution of an external process output into
> doctest.
>
> The following snippet of code:
>
> def tester3():
>     """
>     >>> import os
>     >>> os.system ('/bin/ls')
>     example.py   example.pyc  tester.py
>     0
>     """
>
> produces this output:
>
> (dougg at ultra5-4:)%python example.py
> example.py   example.pyc  tester.py
> *****************************************************************
> Failure in example: os.system ('/bin/ls')
> from line #2 of example.tester3
> Expected:
> example.py   example.pyc  tester.py
> 0
> Got: 0
> *****************************************************************
> 1 items had failures:
>    1 of   2 in example.tester3
> ***Test Failed*** 1 failures.
>
> WHY?

doctest captures output written to Python's sys.stdout.  /bin/ls doesn't
know anything about Python's sys.stdout, and Python's sys.stdout doesn't
know anything about /bin/ls <wink>.

> ...
> Where did the other output from the external program go?

Probably to your terminal.

You may have better luck with, e.g.

>>> os.open('/bin/ls').read()

That gives Python a chance to capture the output /bin/ls sends to *its* idea
of "stdout", and then echoes it back to Python's sys.stdout.





More information about the Python-list mailing list