Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capturing output of subprocess.Popen() with nose

I'm using nose to test an application that uses subprocess.Popen() to call a script. Neither the capture or logcapture plugin seem to capture the output of this script. Is there an easy way to pipe this output to nose?

Here's what I've attempted so far; note "Capture me" isn't captured:

example.py:

if __name__ == '__main__':
    print 'Capture me'

test.py:

import subprocess
import sys


def test_popen():
    # nose's capture plugin replaces sys.stdout with a StringIO instance.
    # subprocess.Popen() expects stdout to have a fileno property, which
    # StringIO doesn't, so fake it.
    sys.stdout.fileno = lambda: 1

    subprocess.Popen(['python', 'example.py'], stdout=sys.stdout)
    assert False # Force test to fail so we see nose output

output:

$ nosetests test.py
F
======================================================================
FAIL: test.test_popen
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/pmdarrow/Envs/example/lib/python2.7/site-packages/nose/case.py", line 197, in runTest
    self.test(*self.arg)
  File "/Users/pmdarrow/Code/example/API/test.py", line 8, in test_popen
    assert False
AssertionError

----------------------------------------------------------------------
Ran 1 test in 0.004s

FAILED (failures=1)
Capture me
like image 728
pmdarrow Avatar asked May 15 '14 13:05

pmdarrow


1 Answers

There are several issues that you have to be aware. I might be repeating @arbunet answer, but bear with me a little here. So if you wait for the process to finish, and redirect process' stdout to the test's stdout, everything will be captured properly with nose:

import subprocess
import sys

def test_popen():
    p = subprocess.Popen(['python', 'example.py'], stdout=subprocess.PIPE)
    out, err = p.communicate()
    print out

    assert False

Here the PIPE is created and redirected into stdout using print statement. Alternatively, you could pass in sys.__stdout__ (unmolested stdout of the test) or other file descriptor to the Popen call and later flush it into sys.stdout (StringIO.StringIO instance created by nose), but it looks less clean (compared to a simple print statement).

like image 98
Oleksiy Avatar answered Oct 26 '22 13:10

Oleksiy