[Django]-Django unittest output

2👍

You need to redirect stderr to stdout. Here is the fixed code:

args_list = ['python', '/path/to/manage.py', 'test', 'myapp']
process=subprocess.Popen(args_list, stderr=subprocess.STDOUT, stdout=subprocess.PIPE)
output, errors = process.communicate()
print output

Alternatively in the shell, you can type something like this:

python manage.py test myapp > stdout.txt 2> stderr.txt

1👍

You can process test results with the unittest.TestResult object. See https://stackoverflow.com/a/284192/579461 for an example.

1👍

If you truly want to redirect stdout to a variable for examination, then you can do it using the following code:

from cStringIO import StringIO
import sys

output = StringIO()
sys.stdout = output
print "Hello World!"
print 42
sys.stdout = sys.__stdout__
print output.getvalue()
>>>
Hello World!
42

0👍

How can all of the output be stores to a variable.

Redirect stderr to stdout:

from subprocess import check_output, STDOUT

output = check_output(args_list, stderr=STDOUT)
print output,
👤jfs

Leave a comment