In one of my functions I'm calling an external program, using subprocess.check_call
, which will produce output. How could I use doctest to make sure the output it's producing is the one I'm expecting?
Maybe this can help:
import sys
import tempfile
import subprocess
def example(output):
r""" Do something ...
>>> output = example('Processing file ...')
>>> print output # doctest:+ELLIPSIS
'Processing file ...'
Check how many file was processed.
>>> [line.startswith('Processing file')
... for line in output.splitlines()].count(True)
1
"""
cmd = "print '%s'" % (output, )
with tempfile.TemporaryFile() as output:
subprocess.check_call([sys.executable, '-c', cmd], stdout=output)
output.seek(0)
res = output.read()
return res
if __name__ == '__main__':
import doctest
doctest.testmod()
As you can see i used the argument stdout
of the subprocess.check_call
function so to be able to get the output of the command , beside that if you are not using the stdout
argument (which i assume that is your case) i think it very hard to capture the command output.
Hope this was hopeful :)