I have a function that should not hang. Specifically, it detects the condition that would cause it to hang and raises an error instead. Simplified, my function is similar to this one:
def chew_repeated_leading_characters(string: str, leader: str):
    if len(leader) == 0:
        # Without this, will hang on empty string
        raise ValueError('leader cannot be empty')
    while True:
        if string.startswith(leader):
            string = string[len(leader):]
        else:
            break
    return string
I would like to write a unit test in pytest to ensure this behavior. I know how to assert that a test should raise an exception. However, in this case, if the test fails, it does not actually fail; the entire test apparatus simply hangs.
How do I generate a test failure on endless looping?
 
     
     
    