Günther Jena
Günther Jena

Reputation: 3776

Unittesting a function containing an infinite loop

Given a function handling requests on a connection the body of the function is a infinite loop:

def handle_connection():
    # initialize stuff
    ...

    while True:
        # stuff to get the request
        ...

        # stuff to handle the request
        ...

How would I unittest this function?

Upvotes: 0

Views: 1296

Answers (1)

Uriel
Uriel

Reputation: 16174

You can limit it to run only once while testing, like:

a = 0
while True and not a:
    # do your stuff
    a = 1

that will not require you to change indentation,

or output specific content while running to make sure it gets the right values into the variables while running:

while True:
    # get request
    print(request)
    # interact with request
    print(data_achieved)

which will save you adding a variable.

Upvotes: 1

Related Questions