[Python] How to assert output with nosetest/unittest in python?

Discussão em 'Python' iniciado por Stack, Março 30, 2025.

  1. Stack

    Stack Membro Participativo

    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?

    Continue reading...

Compartilhe esta Página