Python Nose: Log tests results to a file with Multiprocess Plugin

loggingmultiprocessingnosepython

Im trying to log my tests output to a file as well as running them concurrently.
For this Im trying to use the multiprocess plugin and the xunit plugin.

Im aware that they dont work together, xunit doesnt log anything because mutiprocess doesn't send the output directly.

https://github.com/nose-devs/nose/issues/2

What Im looking for is any alternative that allows me to write down the output to a file.
The reason being is that Im running Selenium Tests, and everytime I get an error, the stacktrace is so large that stdout is basically completely filled out.
Something that alleviates might help out as well, the selenium documentation is pretty scarce about how to configure logging output.

I also tried a pretty basic redirection of stdout:

#nosetests > file.txt

But that doesnt work either.

Best Answer

If you want to use basic redirection from the shell you can do

nosetests &> output.txt

But based on your question it seems you'd rather do something like:

$nosetests --processes 4 --with-xunit --xunit-file=test_output.xml

Full example:

$ls
test_nose.py    test_nose.pyc

$cat test_nose.py

import sys
import os
import time

def setUp():
    pass

def test_1():
    time.sleep(5)
    with open('test_pid_' + str(os.getpid()), 'w') as f:
        f.write(str(os.getpid()) + '\n')

def test_2():
    time.sleep(5)
    with open('test_pid_' + str(os.getpid()), 'w') as f:
        f.write(str(os.getpid()) + '\n')

def test_3():
    time.sleep(5)
    with open('test_pid_' + str(os.getpid()), 'w') as f:
        f.write(str(os.getpid()) + '\n')

def test_4():
    time.sleep(5)
    with open('test_pid_' + str(os.getpid()), 'w') as f:
        f.write(str(os.getpid()) + '\n')

def tearDown():
    pass

$ nosetests --processes 4 --with-xunit --xunit-file=test_output.xml
....
----------------------------------------------------------------------
Ran 4 tests in 5.223s

OK

$ ls
test_nose.py    test_output.xml test_pid_55247  test_pid_55249
test_nose.pyc   test_pid_55246  test_pid_55248

$ cat test_pid*
55246
55247
55248
55249

$ xmllint -format test_output.xml 
<?xml version="1.0" encoding="UTF-8"?>
<testsuite name="nosetests" tests="0" errors="0" failures="0" skip="0"/>

Looks like it does not work as you said :)

But

$nosetests --processes 4 &> output.txt

And

$nosetests --with-xunit --xunit-file=test_output.xml

Do.

References:

Redirect stderr and stdout in a Bash script

$man xmllint

$nosetests -h