erikbstack
erikbstack

Reputation: 13254

Mockup for sys.stdin?

Suppose, you want to write unittests for functions like this one:

def test_me(instream):
    out = ""
    for line in instream:
       out += foo(line)
    return out

which are normally used like this:

test_me(sys.stdin)

Now, in your unittests you don't want to use sys.stdin but an object that mimics sys.stdin's behaviour and that object should be fully controlled by you. This way you can test the function as it is and you can insert whatever fake-input you like. What kind of object would you need to create to do that?

Upvotes: 2

Views: 184

Answers (2)

glglgl
glglgl

Reputation: 91049

Try StringIO and/or cStringIO modules.

Upvotes: 1

xtofl
xtofl

Reputation: 41509

In this case, the instream argument is used as a generator; implementing a generator will thus do. Probably just providing a function with a list of lines will do.

Upvotes: 1

Related Questions