We've built cli-app with Python. Some part need ncurses, so we use npyscreen. We've successfully tested most part of app using pytest (with the help of mock and other things). But we stuck in 'how to test the part of ncurses code'
Take this part of our ncurses code that prompt user to answer:
"""
Generate text user interface:
example :
fields = [
{"type": "TitleText", "name": "Name", "key": "name"},
{"type": "TitlePassword", "name": "Password", "key": "password"},
{"type": "TitleSelectOne", "name": "Role",
"key": "role", "values": ["admin", "user"]},
]
form = form_generator("Form Foo", fields)
print(form["role"].value[0])
print(form["name"].value)
"""
def form_generator(form_title, fields):
def myFunction(*args):
form = npyscreen.Form(name=form_title)
result = {}
for field in fields:
t = field["type"]
k = field["key"]
del field["type"]
del field["key"]
result[k] = form.add(getattr(npyscreen, t), **field)
form.edit()
return result
return npyscreen.wrapper_basic(myFunction)
We have tried many ways, but failed:
- stringIO to capture the output: failed
- redirect the output to file: failed
- hecate: failed
- I think it's only work if we run whole program
- pyautogui
- I think it's only work if we run whole program
This is the complete steps of what I have tried
So the last thing I use is to use patch
. I patch those
functions. But the cons is the statements inside those functions are
remain untested. Cause it just assert the hard-coded return value.
I find npyscreen docs for writing test. But I don't completely understand. There is just one example.
Thank you in advance.