Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to test workflows involving InquirerPy? #62

Open
MaxG87 opened this issue Apr 12, 2023 · 0 comments
Open

How to test workflows involving InquirerPy? #62

MaxG87 opened this issue Apr 12, 2023 · 0 comments

Comments

@MaxG87
Copy link

MaxG87 commented Apr 12, 2023

I am using InquirerPy in a small project to build the user interface ther. The project involves some non-trivial validators and workflows and I would like to ensure that these work by writing a unit test that mimics some key presses and checks the result somehow.

I am aware of this FAQ entry but find it not satisfactory. It resorts to mock InquirerPy to receive the correct answer, but it is exactly the process of getting the right answer I want to test. For instance, I might want to restructure the questions list. Maybe I want to refactor the validators. Whether these cases work or not cannot be tested by mocking InquirerPy.

I tried to look up how prompt-toolkit does it. Unfortunately this causes the tests to hang badly. I need to pkill pytest to shut down the test suite. Please find a MWE attached.

Could you please explain how I could test my workflows?

from typing import Any

from InquirerPy.resolver import prompt
from prompt_toolkit.application import create_app_session
from prompt_toolkit.input import create_pipe_input
from prompt_toolkit.output import DummyOutput


def test_something():
    questions: list[dict[str, Any]] = [
        {
            "type": "expand",
            "name": "command",
            "message": "Please press 'q'!",
            "default": "s",
            "choices": [
                {"key": "q", "name": "Quit Program", "value": "quit"},
            ],
        },
        {
            "type": "confirm",
            "name": "confirmation",
            "message": "Press 'y' to confirm!",
            "when": lambda answers: answers["command"] in {"delete", "quit"},
            "default": False,
        },
    ]
    with create_pipe_input() as pipe_input:
        pipe_input.send_text("q\ny\n")
        with create_app_session(input=pipe_input, output=DummyOutput()):
            prompt(questions)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant