-1

How could I test an infinite loop? For example:

func.py

def func():
    while True:
        char = input().lower()
        if char == 'q':
            break 
        elif char.isalpha():
            print("It's not a number")
        if int(char) == #some_number
            break
        else:
            print('Try again')
        

test.py

def test_func():
    ???

Is there any solution for testing this?

7
  • en.wikipedia.org/wiki/Halting_problem Commented Oct 13, 2022 at 20:10
  • This will surely help you: stackoverflow.com/questions/46446394/… Commented Oct 13, 2022 at 20:10
  • What exactly do you want to test? Whether the function works, or whether it is infinite? Commented Oct 13, 2022 at 20:12
  • If you are testing whether the function works, then these questions show how to test a function that takes user input: stackoverflow.com/questions/63520890/… stackoverflow.com/questions/21875141/… Commented Oct 13, 2022 at 20:15
  • Stuart, I want to write tests for a function. If the value is 'a', I'll get assert func() == "It's not a number" If it's '65', I'll get it: assert func() == "Try again" If I get '50', for example, I will exit the loop Commented Oct 13, 2022 at 20:25

1 Answer 1

3

You can use unittest to mock the user's input:

import builtins
from unittest.mock import patch

def func():
    while True:
        char = input().lower()
        if char == 'q':
            break 
        elif char.isalpha():
            print("It's not a number")
        if int(char) == 3:
            break
        else:
            print('Try again')

def test_func(inputs):
    with patch("builtins.input") as input_mock:
        input_mock.side_effect = inputs
        func()

test_func(["q"])
test_func(["3"])  
test_func(["4", "5", "6"])  # StopIteration error indicates this input is not sufficient for the function to return
test_func(["a", "b", "c", "q"])  # ValueError indicates a bug in the function
test_func(["4", "5", "6", "q"])  # Try again 3 times as expected

EDIT: You could also use unittest to capture the printed output and return it, so that you can check this output systematically against the expected output.

import builtins
from unittest.mock import patch
import io

def test_func(inputs):
    with patch("builtins.input") as input_mock, \
         patch("sys.stdout", new_callable=io.StringIO) as output_mock:
        input_mock.side_effect = inputs
        try:
            func()
        except StopIteration:
            print("FUNCTION DID NOT RETURN")
        return output_mock.getvalue().strip().split("\n")

print(test_func(["q"]) == [""]) # True
print(test_func(["4", "5", "6", "q"]) == ['Try again', 'Try again', 'Try again']) # True
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.