File: faq.md

package info (click to toggle)
python-inquirerpy 0.3.4-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,088 kB
  • sloc: python: 9,463; makefile: 15
file content (79 lines) | stat: -rw-r--r-- 2,107 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# FAQ

## Can I change how the user answer is displayed?

```{seealso}
{ref}`pages/dynamic:transformer`
```

Yes, especially for list type prompts with multiple selection, printing selection
as a list is not ideal in a lot of scenarios. Use `transformer` parameter to customise it.

## How can I do unittest when using `InquirerPy`?

```{tip}
Since `InquirerPy` module itself is tested, there's no need to mock any futher/deeper than the API entrypoint (`prompt` and `inquirer`).
```

For {ref}`index:Classic Syntax (PyInquirer)` user, it would be just a direct mock on the {ref}`pages/prompt:prompt` function.

```{code-block} python
---
caption: Module/somefunction.py
---
from InquirerPy import prompt

def get_name():
    return prompt({"type": "input", "message": "Name:"})
```

```{code-block} python
---
caption: tests/test_somefunction.py
---
import unittest
from unittest.mock import patch

from Module.somefunction import get_name

class TestPrompt(unittest.TestCase):
    @patch("Module.somefunction.prompt")
    def test_get_name(self, mocked_prompt):
        mocked_prompt.return_value = "hello"
        result = get_name()
        self.assertEqual(result, "hello")
```

For {ref}`index:Alternate Syntax` user, you'd have to mock 1 level deeper to the prompt class level.

```{code-block} python
---
caption: Module/somefunction.py
---
from InquirerPy import inquirer

def get_name():
    return inquirer.text(message="Name:").execute()
```

```{code-block} python
---
caption: tests/test_somefunction.py
---
import unittest
from unittest.mock import patch

from Module.somefunction import get_name

class TestPrompt(unittest.TestCase):
    @patch("Module.somefunction.inquirer.text")
    def test_get_name(self, mocked_prompt):
        mocked_prompt.return_value = "hello"
        result = get_name()
        self.assertEqual(result, "hello")
```

## Can I navigate back to the previous question?

No. With the current implementation this is not possible since the control of the prompt is terminated after it is answered.
This may be supported in the future but not a priority at the moment.