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

Documentation: add documentation on how to test an application using questionary #355

Open
lewoudar opened this issue Jan 29, 2024 · 8 comments
Labels
Enhancement New feature or request

Comments

@lewoudar
Copy link

Describe the problem

Hello everyone,
first of all, thanks to the author for this amazing library. I like to write cli and this project is what I have wanted for some time.
But we all know that code that is not tested is difficult to maintain. So it will be interesting to add a section in the documentation explaining how to test our prompts

Describe the solution

I don't have a proper solution, but probably some utilities used in tests can be added in the library with an explanation / examples on how to use them ^^

Alternatives considered

No response

@lewoudar lewoudar added the Enhancement New feature or request label Jan 29, 2024
@alexlatif
Copy link

try using pyprompttoolkits create_pipe_input

with create_pipe_input() as inp:
    pipe = [
        "answer 1",
        "answer 2",
    ]
    for p in pipe:
        inp.send_text(p)
        inp.send_text("\n")
    
    run_prompt_session( )

as it is the core prompting library being abstracted

this isnt perfect either as if your input is too short it will hang and so you must thread tests to timeout when tests are ready so that they error

if you find a way to better handle this on pytests pls lmk

@alexlatif
Copy link

sorry one caveat

you basically need to pass in

    input: Optional[Input] = None,
    output: Optional[Output] = None,

confusing as f@#% as they are magically appended kwargs

with create_pipe_input() as inp:
pipe = []
for p in pipe:
inp.send_text(p)
inp.send_text("\n")

vars = questionary.prompt(
    questions,
    input=inp,
    output=DummyOutput(),
)

@lewoudar
Copy link
Author

lewoudar commented Feb 11, 2024

Thank you @alexlatif
oosh! it is a bit more complicated than I thought, I need to change how I created my code. In my tutorial, I use wrapper functions to questionary prompts without giving the possibility to pass additional data :D

Another question, I saw in your tests, you prefer use \r instead for \n for Enter, why?

@lewoudar
Copy link
Author

Hum, I am struggling with this test. I don't want I'm doing wrong, but the confirm input just return True all the time. I try Enter with \n and \r without success. Do you have any idea?

def test_complicated():
    def complicated_prompt(*, inputs, output) -> list[str]:
        fruits = []
        c = True
        while c:
            fruit = questionary.text("Fruit:", input=inputs[0], output=output).ask()
            fruits.append(fruit)
            c = questionary.confirm('add another fruit', default=True, input=inputs[1], output=output).ask()
        return fruits

    with create_pipe_input() as confirm_input, create_pipe_input() as text_input:
        confirm_input.send_text('Y\nn\n')
        text_input.send_text('apple\npineapple\n')
        fruits = complicated_prompt(inputs=[text_input, confirm_input], output=DummyOutput())
        assert fruits == ['apple', 'pineapple']

@alexlatif
Copy link

sorry for the delay. it actually depends on the question. so when its choices "" an empty string worked, but then i had to simulate the arrow keys and could not just pass in the value of the choice as it is obvs reading from the stdin.

it was so much trial and error for me too that i just gave up on testing it all as i would have liked. in reality it isn't this package's fault as much as it is a terribly handled feature of prompt_toolkit.

i've give up on python as a whole tbh and am is the magic land of Rust where annoyances like this are a thing of the past

@lewoudar
Copy link
Author

I think I will just give up on testing. Effectively prompt_toolkit didn't well manage the testing part :(
Thank you for your time and I'm sad you're giving up on Python, it may have its quirks (like every programming language) but I think overall, it is still a pleasant language to use. Hope you will change your mind :)

@stdedos
Copy link

stdedos commented May 8, 2024

Not an official documentation, but documented somehow? #253 (comment)

@lewoudar
Copy link
Author

Thanks @stdedos . I already checked the tests, but I was still unable to make my test case work. Honestly, I didn't look at it recently, may be I will give it another try...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants