TestingΒΆ

Use richer_prompt.testing.simulate_keys() to test code that runs prompts. Prompts inside the block read the given keys instead of the real keyboard, so no interactive terminal is required.

from richer_prompt import keys, Select
from richer_prompt.testing import simulate_keys

select_color = Select("Choose a color:", ["Red", "Green", "Blue"])

with simulate_keys(keys.DOWN, keys.ENTER):
    assert select_color() == "Green"

If the keys run out while a prompt is still waiting for input, the key read raises AssertionError. Control keys behave like the real keyboard, for example, keys.CTRL_C raises KeyboardInterrupt and keys.CTRL_D raises EOFError.