How to assert output with nosetest/unittest in python?

StackOverflow

I"m writing tests for a function like next one:

def foo():
    print "hello world!"

So when I want to test this function the code will be like this:

import sys
from foomodule import foo
def test_foo():
    foo()
    output = sys.stdout.getline().strip() # because stdout is an StringIO instance
    assert output == "hello world!"

But if I run nosetests with -s parameter the test crashes. How can I catch the output with unittest or nose module?





Get Solution for free from DataCamp guru