1

After running into trouble trying to set the python unittest discover pathway for my project, I'm trying again with nose. I am trying to print out verbose output which is supposedly captured by default by Nose (according to http://nose.readthedocs.org/en/latest/plugins/capture.html)

I have:

arg =sys.argv[:1]
arg.append('--verbosity=2')
out = nose.run(module=ft1.test_y1, argv=arg)

but 'out' is a boolean

How can I get it working?

user1592380
  • 34,265
  • 92
  • 284
  • 515

1 Answers1

1

Your best bet would be to disable the plugin and capture standard output with some other means, for example as described here:

import sys
import nose
from cStringIO import StringIO    

def basic_test():
    print "hello"

if __name__=="__main__":
    module_name = sys.modules[__name__].__file__

    old_stdout = sys.stdout
    sys.stdout = mystdout = StringIO()
    result = nose.run(argv=[sys.argv[0],
                            module_name,
                            '-s'])
    sys.stdout = old_stdout
    print mystdout.getvalue()

When running it like this you will get:

.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
hello
Community
  • 1
  • 1
Oleksiy
  • 6,337
  • 5
  • 41
  • 58
  • Thanks for looking at it. This helped a lot. The other point is to realize that nose seems to send all its output to STDERR, not STDOUT. As an aside I have to ask : I am confused. Why are you importing Capture if its not used in the code? - Regards, Bill – user1592380 Feb 07 '14 at 15:49
  • No need for Capture - I was trying to see if I could pass an instance of Capture plugin to nose.run() and access it's buffer after the fact, but that did not work. I think the errors in your tests will go into stderr, but a print out will be in stdout. BTW, you can always copy nose plugins to make your own "specialized" plugin that does exactly what you want - it should not be too hard. – Oleksiy Feb 07 '14 at 18:16