0

I am creating a Full-Screen TUI app using python-prompt-toolkit

(https://github.com/prompt-toolkit/python-prompt-toolkit)

  1. I don't know how to make a unittests for it.

Example Code:

https://github.com/prompt-toolkit/python-prompt-toolkit/blob/master/examples/full-screen/full-screen-demo.py

Hopa
  • 1
  • 1
  • Please provide enough code so others can better understand or reproduce the problem. – Community Aug 05 '22 at 01:34
  • I just added the full-screen demo code from the prompt-toolkit git repo examples. I want to make unittests for that – Hopa Aug 05 '22 at 09:22

1 Answers1

0

I managed to do so using unittest making two sperate files >> test.py , run_tests.py

1- putting the prompt-toolkit output in a function like so in test.py

def test_getTitledText():
title, value, height, read_only, width = prompt('input')
wid = getTitledText(
            title=title,
            name='',
            height=height,
            value=value,
            read_only=read_only,
            width=width
            )
return wid.get_children()[0].content.text() + wid.get_children()[1].content.buffer.text == title + ': '+value   ## dont remove this space

In this way, I am accessing the real output of the prompt-toolkit lib for my custom widget (can be done for any type of widget)

2- test it by the expected output in run_tests.py

class Test_GetTitledText(unittest.TestCase):
def test_getTitledText1(self):   ### valid values
    with patch('test.prompt', return_value = ("Title","value",1,False,0)) as prompt : ## return_value >> title, value, height, read_only, width
        self.assertEqual(test_getTitledText(), True)
        prompt.assert_called_once_with('input')

def test_getTitledText2(self):   ### valid values with hight=3, and read_only
    with patch('test.prompt', return_value = ("Title","value",3,True,2)) as prompt : ## return_value >> title, value, height, read_only, width
        self.assertEqual(test_getTitledText(), True)
        prompt.assert_called_once_with('input')

I dont know if there is an other valid way to do so, but this way worked for me.

Hopa
  • 1
  • 1